Friends (Flip Tree Edge Weight Game)-Still thinking

Jun B is watching a group of boys and a group of girls playing games. Specifically, the game is like this:
given a tree of n nodes, each edge of this tree has a weight. This weight can only be 0 or 1. At the beginning of a game, a node will be determined as the root. Next, starting from the girl, the two sides take turns to operate.
When one party operates, they need to first select a point that is not a root, satisfying that the edge weight from that point to its father is 1; then find a simple path from this point to the root node, and flip the weight of all edges on the path That is, 0 becomes 1, and 1 becomes 0).
When one side is unable to operate (that is, all the side weights are 0), the other side wins.
If both sides adopt the optimal strategy, the girl will win, then output "Girls win!", Otherwise output "Boys win!"
In order to make the game more interesting, there may be operations to modify the border power between each round, and the root node specified for each round of the game may also be different.
Specifically, there are m operations for modifying the border weight and playing the game, as follows:
"0 x" means to ask which party will win if the current tree starts with x as the root node.

"1 xyz" means to modify the edge weight of the edge between x and y to z.
Of course, Jun B knows how to do it! But he wants to test you.
Input contains up to 5 sets of test data.
The first line has a positive integer indicating the number of data groups.
Next, in the first line of each group of data, there are two positive integers n and m separated by two spaces, which respectively represent the number of points and the number of operations. Ensure that n, m <40000.
The next n-1 rows, each row of three integers x, y, z, represents an edge of the tree. Ensure that 1 <x <n, 1 <y <n, 0 <= z <= 1.
The next m lines, one operation per line, the meaning is as described above. It is guaranteed that only the two formats mentioned above will appear.
For operation 0, ensure 1 <= x <= n; for operation 1, ensure 1 <= x <= n, 1 <= y <= n, 0 <= z <= 1, and ensure that there is an edge connection x on the tree And y.
Output For each query operation of each set of data, output a line of "Boys win!" Or "Girls win!". Sample Input
2
2 3
1 2 0
0 1
1 2 1 1
0 2
4 11
1 2 1
2 3 1
3 4 0
0 1
0 2
0 3
0 4
1 2 1 0
0 1
0 2
0 3
1 3 4 1
0 3
0 4
Sample Output
Boys win! 
Girls win! 
Girls win! 
Boys win! 
Girls win! 
Boys win! 
Boys win! 
Girls win! 
Girls win! 
Boys win! 
Girls win! 
Ideas:
For this kind of similar problem, we know that there must be hidden rules.
The main thing is to look at the edge of the tree directly connected to the root node.
①If there is only one edge connected to the root node , we call this edge X.
  1. Then we think that if the weight of the X side is 1: the
    first hand can ignore the side behind X and only turn X to 0, even if the next person turns the back to be able to turn the edge, then X becomes 1 again , So that no matter which side the other person turns behind, the first hand can only turn X, consuming the other person, until the back of X is all 0, and then turn X, the first hand wins.
  2. On the contrary, the X side weight is 0: the
   first hand can only turn the side behind X, and the other person (back hand) can imitate the first hand of the previous situation, and the back hand wins.
②Similarly, we will directly connect to the root node, and the edge weight is 1 to expand to multiple edges , collectively referred to as X:
  1. If the number of X is odd, first and second hand (one chain and one chain) ) Consumption of the opponent in turn, in the end a chain with X still turns X first and wins first (simulated ①1.)
  2. Similarly, the number of X is even, the first and second hand (one chain and one chain) consume each other in turn, In the end, a chain with X is that the back hand turns X first, and the back hand wins (imitate ① 2.)
  Note: (In ②, we do not discuss the edge directly connected to the root node as ①, the edge weight is 0, because if the edge behind this edge has a value of 1, after the first hand flip,
  this one is directly connected to The edge of the root node becomes 1, and the backhand can use it to consume the opponent. When the backhand wins, it is equivalent to when X is 0, that is, the number of X is even, the same as ② 2. Conclusion)
Summary:
  We only need to see If there are several edge weights directly connected to the root node, the winner who starts the game with this node as the root node can be judged, odd-> first hand wins even-> last hand wins .
Code:
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <algorithm>
 5 using namespace std;
 6 const int maxn=4e4+3;
 7 int head[maxn],n,m,cnt,key;
 8 int x,y,z;
 9 struct Edge{bool wei;int to,next;}e[2*maxn];
10 void Add(int x,int y,int z){
11     e[++cnt].to=y;
12     if(z)e[cnt].wei=1;else e[cnt].wei=0;
13     e[cnt].next=head[x];
14     head[x]=cnt;
15 }
16 void Xiugai(int x,int y,int z){
17     for(int i=head[x];i;i=e[i].next){
18         if(y==e[i].to){
19             if(z)e[i].wei=1;else e[i].wei=0;
20             break;
21         }
22     }
23     for(int i=head[y];i;i=e[i].next){
24         if(x==e[i].to){
25             if(z)e[i].wei=1;else e[i].wei=0;
26             return;
27         }
28     }
29 }
30 void Sta(int root){
31     int ans=0;
32     for(int i=head[root];i;i=e[i].next){
33         if(e[i].wei)ans++;
34     }
35     if(ans%2==1)printf("Girls win!\n");
36     else printf("Boys win!\n");
37 }
38 int main(){
39 //    freopen("1.in","r",stdin);
40     int t;    
41     scanf("%d",&t);
42     while(t--){
43         cnt=0;memset(head,0,sizeof(head));
44         scanf("%d%d",&n,&m);
45         for(int i=1;i<n;++i){
46             scanf("%d%d%d",&x,&y,&z);
47             Add(x,y,z);Add(y,x,z);
48         }
49         for(int i=1;i<=m;++i){
50             scanf("%d",&key);
51             if(!key){
52                 scanf("%d",&x);
53                 Sta(x);
54             }else{
55                 scanf("%d%d%d",&x,&y,&z);
56                 Xiugai(x,y,z);
57             }
58         }
59     }
60     return 0;
61 } 

 

Guess you like

Origin www.cnblogs.com/Lour688/p/12688771.html