Ring ring! ! ! ! ! ! ! !

Problem Description
  N Xiaoming laboratory computers, numbers 1 ~ N. Originally had connected N-1 th data link between the computer N, form a tree network exactly. On tree network, between any two computers are connected with a unique path.


  But in a recent maintain the network, the administrator of a malfunction between two computers so that adds a data link, so that a loop network. Since the computer on the loop between any two is no longer only one path, so that data transmission occurred on these computers BUG.


  In order to restore normal transmission. Xiao Ming need to find all the computers on the loop, can you help him?
Input Format
  The first row contains an integer N.
  The following N lines of two integers a and b, is connected to a data link expressed between a and b.


  For 30% of the data, 1 <= N <= 1000
  data to 100 percent, 1 <= N <= 100000 , 1 <= a, b <= N


  input guaranteed valid.
Output Format
  By the computer from small to large numbers sequentially output on a loop, separated by an intermediate space.
Sample input
5
1 2
3 1
2 4
2 5
5 3
Sample Output
1 2 3 5
Disjoint-set + dfs
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<vector>
  4 #include<cstring>
  5  
  6 using namespace std;
  7  
  8 const int N = 100000+5;
  9  
 10 vector<int> V[2*N];
 11 int parent[N], S, E, n;
 12 int vis[N] = {0};
 13  
 14 void init()
 15 {
 16     for(int i=0; i<=N; i++){
 17         parent[i] = i;
 18     }
 19     
 20 }
 21  
 22 int Find(int a)
 23 {
 24     return parent[a] == a? a : (parent[a] = Find(parent[a]));
 25 }
 26  
 27 bool Union(int a, int b)
 28 {
 29 //    cout << a << "    par  " << b << endl;
 30     a = Find(a);
 31     b =The Find (B);
 32      
33 is      IF (A == B) { // constitutes the ring 
34 is          return  to true ;
 35      }
 36      the else {
 37 [          parent [A] = B;
 38 is          return  to false ;
 39      }
 40  } 
 41 is   
42 is  void DFS ( int U)
 43 is  {
 44 is  //         COUT E << endl << << U; 
45          
46 is          IF (U == E) { // reach the end
 47          //     COUT n-<< << "output is:";
 48             for(int i=1; i<=n; i++){
 49         //        cout << i << ":" << vis[i] << endl;
 50                 if(vis[i]){
 51                     cout << i;
 52                     printf((i == n) ? "\n" : " ");
 53                     //cout << ((i==n) ? endl : " ");
 54                 }
 55             } 
 56             return;
 57         }
 58     //    else cout << u << "!=" << E << endl;
 59         
 60         for(int i=0; i<V[u].size(); i++){
 61             int next = V[u][i];
 62             if(vis[next] == 0){
 63                 vis[next] = 1;
 64                 dfs(next);
 65                 vis[next] = 0;
 66             }
 67         }
 68     return;
 69 }
 70  
 71 int main()
 72 {
 73     cin >> n;
 74     
 75     init();
 76     
 77     for(int i=0; i<n; i++){
 78         int u, v;
 79         
 80         cin >> u >> v;
 81 //            cout << u <<  "  init   " << v << endl;
 82         if(Union(u, v)){//已构成环 
 83         
 84              S = u, E = v;
 85              break;
 86         }
 87         else {
 88             V[u].push_back(v);
 89             V[v].push_back(u);
 90         }
 91     }
 92     
 93 //    cout << "start:" << S << "    end:" << E << endl;
 94     
 95     vis[S] = 1; 
 96     dfs(S);
 97  
 98     return 0;
 99 
100 }

 

Guess you like

Origin www.cnblogs.com/RE-TLE/p/11967557.html