VasilM

topologichno_sortirane

Jan 10th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. #define MAXN 1000
  4.  
  5. int U[MAXN+1],S[MAXN],P[MAXN],G[MAXN][MAXN],t;
  6. int N,M;
  7.  
  8. void DFS_R(int r)
  9. {   int y;
  10.     while(G[r][0]>0)
  11.     {  y=G[r][G[r][0]--];
  12.        if(!U[y]) {U[y]=1; P[y]=r; DFS_R(y); }
  13.     } S[t++]=r;
  14. }
  15.  
  16. void first_call()
  17. { int i; for(i=1;i<=N;i++) U[i]=0; t=0;
  18.   for(i=1;i<=N;i++) if(U[i]==0) DFS_R(i);
  19. }
  20.  
  21. int main()
  22. {   int x,y,i;
  23.     cin >> N >> M;
  24.     for(i=1;i<=N;i++) G[i][0]=0;
  25.     for(i=1;i<=M;i++)
  26.     {  cin >> x >> y;
  27.        G[x][++G[x][0]]=y;
  28.        // в ориентирания няма ребро (y,x)
  29.     }
  30.     first_call();
  31.     for(i=0;i<N;i++) cout << S[i] <<'\t';
  32.     cout << endl;
  33.     return 0;
  34. }
  35.  
  36. /*  11 17
  37.     1 2
  38.     1 9
  39.     1 7
  40.     2 4
  41.     2 6
  42.     9 10
  43.     7 6
  44.     7 3
  45.     4 8
  46.     4 5
  47.     6 5
  48.     6 10
  49.     3 10
  50.     8 11
  51.     5 11
  52.     10 5
  53.     10 11
  54. */
Advertisement
Add Comment
Please, Sign In to add comment