I am getting TLE in this problem .
i am using F(source) = 1 + max(F(neighbours(n1,n2,n3))
Can anyone suggest me better approach.
function :
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll dp[100001];
ll findLongestPath(vector graph[],ll src){
if(dp[src]!=-1)
return dp[src];
ll result = INT_MIN;
bool isTrue = false;
for(ll neighbour : graph[src]){
isTrue = true;
result = max(result,findLongestPath(graph,neighbour) + 1);
}
return (isTrue)?result:0;
}
int main()
{
ios_base:: sync_with_stdio(false);
cin.tie(NULL);
ll v,e; cin>>v>>e;
memset(dp,-1,sizeof(dp));
vector<ll> graph[100001];
while(e--){
int a,b; cin>>a>>b;
graph[a].push_back(b);
}
ll result = INT_MIN;
for(ll i = 1; i<=v;i++){
result = max(findLongestPath(graph,i),result);
}
cout<<result<<endl;
}