Http://acm.uestc.edu.cn/#/problem/show/32
Tree Wars (Battle on the trees)Time limit:12000/4000ms (java/others) Memory limit:65535/65535kb (java/others)SubmitStatus
To a tree, if a node in the tree is occupied by someone, then all its sons are occupied, and at the lxh
pfz
initial time they stand on two nodes, and whoever is currently at the point occupied by another, he loses the game and asks who can win.
Input
Input contains multiple sets of data
The first row of each group contains two numbersN,M(N,M≤100000 ),NRepresents the number of nodes in the tree,MIndicates the number of queries, N=M=0 Indicates the end of the input. The number of nodes is 1 to N.
NextN−1 Row, per line2An integerA,B(1≤A , b≤N), indicating that the node numbered a is the father of the node numbered b .
NextMLine, each line has2Number, indicatinglxh
Andpfz
The number of the initial positionX,Y(1≤ x , < Span id= "Mathjax-element-152-frame" class= "Mathjax" >y≤ n , < Span id= "Mathjax-element-153-frame" class= "Mathjax" >x≠ y ), lxh
always moves first.
Output
For each query, output one line and output the winner's name.
Sample Input and output
Sample Input |
Sample Output |
2 11 21 25 21 21 33 43 54 24 50 0 |
Lxhpfzlxh |
The puzzle: The memory of the search. This topic, in fact, you want to find out the depth of the two nodes (that is, the distance to the root), do a comparison can be. When implemented, the son points to the father as having to the edge, because it is a tree, so the n-1 node corresponds to the N-1 bar has to the edge. Do a memory search.
Code:
1#include <fstream>2#include <cstdio>3#include <cstring>4#include <iostream>5 6 using namespacestd;7 8 Const intn=100005;9 intn,m;Ten intU[n]; One intDis[n]; A BOOLB[n]; - - intDfsinti); the - intMain () - { - //freopen ("d:\\input.in", "R", stdin); + //freopen ("D:\\output.out", "w", stdout); - intt1,t2; + while(SCANF ("%d%d",&n,&m), N) { Amemset (dis,-1,sizeof(DIS)); atmemset (b,0,sizeof(b)); - for(intI=1; i<n;i++){ -scanf"%d%d",&t1,&T2); -U[T2]=T1;//by son pointing to Father -b[t2]=1; - } in for(intI=0; i<m;i++){ -scanf"%d%d",&t1,&T2); to if(dis[t1]==-1) DFS (t1); + if(dis[t2]==-1) DFS (T2); - if(Dis[t1]<=dis[t2]) puts ("Lxh"); the ElsePuts"PFZ"); * } $ }Panax Notoginseng return 0; - } the intDfsinti) { + if(dis[i]!=-1)returnDis[i]; A if(b[i]==0)return 0; theDis[i]=dfs (U[i]) +1; + returnDis[i]; -}
cdoj32-(Battle on the tree) "Memory Search"