Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2545
Tree war
Time Limit: 10000/4000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 564 accepted submission (s): 305
Problem description is provided to a tree. If a node on the tree is occupied by someone, all its sons are occupied. lxh and pfz are standing on two nodes respectively at the beginning, when another person occupies the current point, he loses the game and asks who can win.
Input contains multiple groups of data
The first row of each group contains two numbers n, m (n, m <= 100000), N indicates the number of nodes in the tree, and M indicates the number of queries, N = m = 0 indicates that the input is complete. The node number is 1 to n.
Next to the N-1 line, each line has 2 integers A, B (1 <= A, B <= N), indicating that the node numbered a is the father of the node numbered B
In the next m row, there are two numbers in each row, indicating the numbers x, y (1 <= X, Y <= n, x! = Y), lxh always moves first
Output: For each query, output a row and output the name of the winner.
Sample input 2 1 1 2 2 5 2 2 1 3 3 4 5 4 2 5 0
Sample outputlxh pfz lxh prompt: There are many inputs and outputs in this question. Use scanf and printf to replace CIN and cout. The master node wins. When it comes to nodes, it is easy to think of using and querying the set to form a diagram. However, when I started the application and checked the set, I used the most common method to compare whether it was a node or not, and timed out by 0.0. So I thought of a very simple method, is to compare the time when two points arrive at the node. To determine who can win, wow Haha ~ For details, see the code.
1 #include <iostream> 2 #include <cstdio> 3 4 using namespace std; 5 int father[100010],n,m; 6 7 int find(int a) 8 { 9 int flag=0;10 while(a!=father[a])11 {12 a=father[a];13 flag++;14 }15 return flag;16 }17 18 int main ()19 {20 while (~scanf("%d%d",&n,&m))21 {22 for (int i=1; i<=n; i++)23 father[i]=i;24 if (n==0&&m==0)25 break;26 for (int i=1; i<=n-1; i++)27 {28 int a,b;29 scanf("%d%d",&a,&b);30 father[b]=a;31 }32 for (int i=1; i<=m; i++)33 {34 int startl,startp;35 scanf("%d%d",&startl,&startp);36 int t1=find(startl);37 int t2=find(startp);38 if (t1>t2)39 printf ("pfz\n");40 else41 printf ("lxh\n");42 }43 }44 return 0;45 }
War on the HDU 2545 tree (query set)