VasilM

DFS_reverse

Dec 13th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. const int MAXN = 100;
  5. int U[MAXN+1],S[MAXN],P[MAXN],G[MAXN][MAXN],N;
  6.  
  7. void DFS_R(int r)
  8. {   int y;
  9.     while(G[r][0]>0)
  10.     {  y=G[r][G[r][0]--];
  11.        if(!U[y]) {U[y]=1; P[y]=r; DFS_R(y); }
  12.     }
  13. }
  14.  
  15. void first_call()
  16. { for(int i=1;i<=N;i++) U[i]=0;
  17.   for(int i=1;i<=N;i++) if(U[i]==0)
  18.       DFS_R(i);
  19. }
  20.  
  21. int main(){
  22.  
  23.     int x,y;
  24.    
  25.     cin >> N;
  26.     while( cin >> x >> y ){
  27.     G[x][++G[x][0]] = y;
  28.     G[y][++G[y][0]] = x;
  29.     }
  30.  
  31.     DFS_R(1);
  32.  
  33.     for(int i=1; i<=10; i++ ) cout << i << " " << P[i] << endl;
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment