HDU-2545 War on the Trees

war on trees

Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1351    Accepted Submission(s): 749


Problem Description
Given a tree, if a node on the tree is occupied by a certain person, all its sons are occupied, lxh and pfz are initially standing on two nodes respectively, whose current point is occupied by another person, He lost the game and asked who could win
 

Input
The input contains multiple groups of data
. The first line of each group contains two numbers N, M (N, M<=100000), where N represents the number of nodes in the tree, M represents the number of queries, and N=M=0 represents the end of the input. Nodes are numbered 1 to N.
Next N-1 lines, each line contains 2 integers A, B (1<=A,B<=N), indicating that the node numbered A is the parent of the node numbered B
Next M lines, each line has 2 The number, indicating the number of the initial position of lxh and pfz X, Y (1<=X,Y<=N,X!=Y), lxh always moves first

 

Output
For each query, print one line with the winner's name
 

Sample Input
 
  
2 1 1 2 1 2 5 2 1 2 1 3 3 4 3 5 4 2 4 5 0 0
 

Sample Output
 
  
lxh pfz lxh Tips: This question has a lot of input and output, please use scanf and printf instead of cin and cout.
 
#include<iostream>
#include<cstdio>
using namespace std;
int f[100005];
void init(int n){
    for(int i=1;i<=n;i++){
        f[i]=i;
    }
}
int find_ans(int x){
    int cnt=0;
    while(x!=f[x]){
        cnt++;
        x=f[x];
    }
    return cnt;
}
int main(){
    int n,m,x,y,a,b;
    while(~scanf("%d%d",&n,&m)){
        if(n==0&&m==0)
            break;
        init(n);
        for(int i=1;i<n;i++){
            scanf("%d%d",&x,&y);
            f[y]=x;
        }
        for(int i=1;i<=m;i++){//Number of steps needed to go to the root node
            scanf("%d%d",&a,&b);
            int p=find_ans(a);
            int q=find_ans(b);
            if(p<=q)
                cout<<"lxh"<<endl;
            if(p>q)
                cout<<"pfz"<<endl;
        }
    }
return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325682355&siteId=291194637