TAP2013C - Little Red-Cap - SPOJ Solution C++

  Problem Link : TAP2013C  


👉 Hint : edit please

 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;

#define ll long long int

vector<int> adj[30001];
bool visited[30001];
pair<ll,ll> dp[30001];
ll dpval[30001];
ll value=0;
pair<ll,ll> dfs(int v,int t)
{
	ll ans=0,maxi=0;
	if(v==t)
	{
		return dp[v]=make_pair(1,1);
	}	
	visited[v]=1;
	for(int i=0;i<adj[v].size();i++)
	{
		if(!visited[adj[v][i]])
		    dp[adj[v][i]]=dfs(adj[v][i],t);
		maxi=max(maxi,dp[adj[v][i]].second);
	    ans=ans+dp[adj[v][i]].first;
	}
	dp[v].first=ans;
    dp[v].second=maxi+ans;
	return dp[v];
}

int main()
{
	value=0;
	int n,s,x,y;
	cin>>n>>s;
	for(int i=1;i<=n;i++)
		adj[i].clear();
	for(int i=1;i<=s;i++)
	{
		cin>>x>>y;
		adj[x].push_back(y);
	}
	memset(visited,0,sizeof(visited));

	
	dfs(1,n);
	cout<<dp[1].second<<endl;

}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!