codeforces 1282 E. The Cake Is a Lie (dfs+构造)

Link: https: //codeforces.com/contest/1282/problem/E

Question is intended: to is a plan view of an n-sided polygon, each cutting knife can cut out a triangle, a final cut to n-2 triangles. Gives the title number three vertices of the triangular cut, and the number of the triangle. Ask your triangular cut out sequentially, and sequentially outputs the original number n of all polygon vertices, the reverse can also be output sequentially output.

Solution: somewhat similar topological sorting. First, the input three triangle points, a, b, c, statistics V [a] Xor b Xor c, empathy statistical V [b], V [c], which can ensure a value V [i] is 0 only Xor connected with two points i, even if there is a common triangle side, eliminates multiple points Xor public side connected. According to this property, it can be output sequence numbers of all points.

         So how does a triangle sequence output? First, if an edge is found that two common triangles, it can be connected to two triangles based on this edge, so that the triangle as a node to form a diagram that is a tree structure, we can easily find a leaf node, traverse again from the start dfs leaf node number can be output triangle.

AC Code:

. 1 #include <the iostream>
 2 #include <Vector>
 . 3 #include <CString>
 . 4 #include <Map>
 . 5 #include <algorithm>
 . 6  the using  namespace STD;
 . 7  const  int MAXN = 1E + . 5 ;
 . 8  int V [MAXN];
 . 9 Vector < int > G [MAXN];
 10  int Visit [MAXN];
 . 11  void DFS ( int X) { // search from the leaf nodes, because the leaf node always represents the outermost triangle, it is one of 
12 is      Visit [X] = . 1 ;
 13 is      for(int i = 0;i<g[x].size() ;i++){
14         int cur = g[x][i];
15         if(visit[cur] ==0) dfs(g[x][i]);
16 //        dfs(g[x][i]);
17     }
18     cout<<x<<" ";
19 }
20 int main(){
21     int t;cin>>t;
22     while(t--){
23         int n;
24         cin>>n;
25         for(int= I 0 ; I <= MAXN; I ++ ) {
 26 is              V [I] = 0 ; // initialize the array V 
27              Visit [I] = 0 ; // initialize to access the array 
28              G [I] .clear ();
 29          }
 30          Map <pair < int , int >, Vector < int >> MP;
 31 is          for ( int I = 0 ; I <N- 2 ; I ++ ) {
 32              int A, B, C;
 33 is              CIN >> A >> B >> c;
 34             IF (A> B) the swap (A, B);
 35              IF (B> C) the swap (B, C);
 36              IF (A> B) the swap (A, B);
 37 [              V [A] ^ = B, V [A] ^ = C; // Xor operation 
38 is              V [B] = A ^, V [B] = ^ C;
 39              V [C] ^ = A, V [C] ^ = B;
 40              MP [{ a, B}] push_back (I +. . 1 ); // add one side a, b, and the common triangular I +. 1 
41 is              MP [{a, C}] push_back (I +. . 1 );
 42 is              MP [{B , C}] push_back (I +. . 1 );
 43 is          }
 44 is          int A, B;
45          for (Auto H: MP) {
 46 is              IF (h.second.size () == . 1 ) { // casually looking for an edge, a triangular share only 
47                  A = h.first.first;
 48                  B = h.first .second;
 49              //     BREAK; 
50              }
 51 is          }
 52 is          COUT A << << "  " << B; // output that the edge 
53 is          for ( int I = 0 ; I <N- 2 ; I ++ ) {
 54 is              int A = V ^ T [B]; //Xor started operation. DETAILED pen can simulate what this process more clearly understood 
55              COUT << "  " << T;
 56 is              A = B, B = T;
 57 is          }
 58          COUT << endl;
 59          for (Auto H: MP) {
 60              IF (h.second.size () == 2 ) {
 61 is                  int U = h.second [ 0 ], V = h.second [ . 1 ];
 62 is                  G [U] .push_back (V), G [V] .push_back (U); // The common edge to a point triangle built FIG 
63 is              }
 64          }
 65          DFS ( . 1);
66         cout<<endl;
67     }
68     return 0;
69 }

Guess you like

Origin www.cnblogs.com/AaronChang/p/12130172.html