VasilM

oiler_dfs_(платка)

Apr 30th, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include <iostream>
  2. #define MAX 1000
  3. #define MAXPATH 1000000
  4. using namespace std;
  5.  
  6. int S[MAX],G[MAX][MAX];
  7. int N,M,top;
  8. int path[MAXPATH], pathSize=0;
  9.  
  10. void make_empty(){
  11.      top =-1;
  12. }
  13.  
  14. void push(int x){
  15.      S[++top]=x;
  16. }
  17.  
  18. void pop(){
  19.      path[pathSize++]=S[top--];
  20. }
  21.  
  22. int look(){
  23.     return S[top];
  24. }
  25.  
  26. bool not_empty(){
  27.      return top>-1;
  28. }
  29.  
  30. void EulerCircuit(int r)
  31. { make_empty();
  32.   push(r);
  33.   while(not_empty())
  34.   { int t=look();
  35.     if(G[t][0]>0)
  36.     {  
  37.         int v=G[t][G[t][0]--];
  38.         for(int j=1;j<=G[v][0];j++)
  39.             {
  40.                 if(G[v][j]==t) {G[v][j]=G[v][G[v][0]--];    
  41.                 break; }
  42.             }
  43.          push(v);
  44.     }
  45.     else pop();
  46.   }
  47. }
  48.  
  49.  
  50.  
  51. int main(){
  52.    
  53.     int x,y;
  54.  
  55.     cin >> N >> M;
  56.  
  57.     for( int i=0; i<M; i++ ){
  58.          cin >> y >> x;
  59.          G[x][++G[x][0]] = y;
  60.          G[y][++G[y][0]] = x;
  61.     }
  62.    
  63.     EulerCircuit(1);
  64.    
  65.     if(pathSize<M+1){
  66.              cout <<"Sorry"<<endl;
  67.              return EXIT_SUCCESS;
  68.              }
  69.              
  70.     for(int i=0;i<pathSize;i++) {
  71.             cout <<path[i]<<" ";
  72.             }
  73. }
  74.  
  75.  
  76. /*
  77. input 1:
  78. 6 10
  79. 1 2
  80. 1 3
  81. 1 4
  82. 2 3
  83. 2 5
  84. 3 4
  85. 3 5
  86. 4 5
  87. 4 6
  88. 5 6
  89.  
  90. input 2:
  91. 6 6
  92. 1 2
  93. 1 3
  94. 1 4
  95. 2 3
  96. 3 4
  97. 5 6
  98. */
Advertisement
Add Comment
Please, Sign In to add comment