Seems like even my corrections to your DFS gets WA.
Code: Select all
int DFS(int v){
if(Deps[v] != -1)
return Deps[v];
int sum = 0;
// graph[v].size() give the size of list adyancencies for v
//graph[v][i] give the node adyacen to v that mean
// v --> graph[v][i]
// only DFS if not visited <- CORRECTION
for(int i,i<graph[v].size()){
sum+=(1+DFS( graph[v][i] ));
}
Deps[v] = sum;
return sum;
}
It seems alright to me but gets WA. My only other advice is to code it another way.
You may try a DFS that has 2 parameters.
Code: Select all
void DFS(int base,int node)
{
int i;
if(base!=node) Deps[base]++;
visited[node]=true;
for(i=1;i<=N;i++)
if( (i adj to node) && (not visited[i]) )
DFS(base,i);
}
....
for(i=1;i<=N;i++){
// reset visited array to false
DFS(i,i);// Do DFS for all the vertices
}
These were one of those problems where I didn't even consider creating critical test cases.
Creating random IO takes a while because cyclic dependencies are not allowed.