Anushi1998's blog

By Anushi1998, history, 2 years ago, In English,

I have implemented this solution for problem

I can't understand why I am getting runtime error in 2nd case and if I avoid that I am getting TLE in 6th instead of the fact that I am getting output for both cases on my IDE.

Any help is appreciated :)

Read more »

 
 
 
 
  • Vote: I like it
  • -6
  • Vote: I do not like it

By Anushi1998, history, 3 years ago, In English,

Usually, topological sort is implemented like

void dfs(int x) {
    vis[x] = true;
    for(int u = 0; u < n; u++) {
        if(!vis[u] && graph[x][u] == 1) {
            dfs(u);
        }
    }
    order.push_back(x);
}

And then printed in reverse order But if I implement this way

void dfs(int x) {
    order.push_back(x);
    vis[x] = true;
    for(int u = 0; u < n; u++) {
        if(!vis[u] && graph[x][u] == 1) {
            dfs(u);
        }
    }
}

And print in same order.

Can someone provide me a test case where 2nd approach will fail

Read more »

 
 
 
 
  • Vote: I like it
  • -7
  • Vote: I do not like it