Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- #define MAXN 1000
- int U[MAXN+1],S[MAXN],P[MAXN],G[MAXN][MAXN],t;
- int N,M;
- void DFS_R(int r)
- { int y;
- while(G[r][0]>0)
- { y=G[r][G[r][0]--];
- if(!U[y]) {U[y]=1; P[y]=r; DFS_R(y); }
- } S[t++]=r;
- }
- void first_call()
- { int i; for(i=1;i<=N;i++) U[i]=0; t=0;
- for(i=1;i<=N;i++) if(U[i]==0) DFS_R(i);
- }
- int main()
- { int x,y,i;
- cin >> N >> M;
- for(i=1;i<=N;i++) G[i][0]=0;
- for(i=1;i<=M;i++)
- { cin >> x >> y;
- G[x][++G[x][0]]=y;
- // в ориентирания няма ребро (y,x)
- }
- first_call();
- for(i=0;i<N;i++) cout << S[i] <<'\t';
- cout << endl;
- return 0;
- }
- /* 11 17
- 1 2
- 1 9
- 1 7
- 2 4
- 2 6
- 9 10
- 7 6
- 7 3
- 4 8
- 4 5
- 6 5
- 6 10
- 3 10
- 8 11
- 5 11
- 10 5
- 10 11
- */
Advertisement
Add Comment
Please, Sign In to add comment