TopSort


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <bits/stdc++.h>
using namespace std;
stack<int>s;
map<int,int>seen;
vector<int>v[1000];
void dfs(int x)
{
    if(seen[x])return; /// jodi already visited hoye thake tahole return kore back a chole jabe.
    seen[x]=1; /// jei node nicchi seta ke seen kore dicchi mane visited kore dicchi .
    for(int i=0;i<v[x].size();i++) /// x node er sathe kara kara connected seta loop er maddhome check korte hoy .
    {
        int top=v[x][i]; /// eta mane x node er sathe ke connected seta .
        dfs(top);
    }
    s.push(x); /// stack a dhukiye dichi . stack a ending time onusare dhukbe.
}
int main()
{
    int n,e,x,y;
    cin>>n>>e; /// n= node, e= edges
    for(int i=0;i<e;i++)
    {
        cin>>x>>y;
        v[x].push_back(y); /// x er sathe y connected. eta directed graph .
    }
    for(int i=1;i<=n;i++) /// ei code a node 1 theke shuru korte hobe.
    {
        if(seen[i]==0) /// node visited kina check korchi ..
        {
            dfs(i); /// visited na hole dfs chalailam.
        }
    }
    while(!s.empty())
    {
        cout<<s.top()<<" "; /// stack er top a thakbe jar ending time beshi . mane je sobar pore finish hoiche .
        s.pop(); /// top neowar por pop kora lage .
    }

}
/*
input
8 7
1 2
2 5
1 6
2 7
2 4
1 3
5 8

output: 1 3 6 2 4 7 5 8
*/

No comments

Theme images by Jason Morrow. Powered by Blogger.