树上战争-想法
Time Limit: 4000MS | Memory Limit: 65535KB | 64bit IO Format: %lld & %llu |
Description
给一棵树,如果树上的某个节点被某个人占据,则它的所有儿子都被占据,lxh和pfz初始时分别站在两个节点上,谁当前所在的点被另一个人占据,他就输了比赛,问谁能获胜
Input
输入包含多组数据
每组第一行包含两个数N,M(N,M<=100000),N表示树的节点数,M表示询问数,N=M=0表示输入结束。节点的编号为1到N。
接下来N-1行,每行2个整数A,B(1<=A,B<=N),表示编号为A的节点是编号为B的节点的父亲
接下来M行,每行有2个数,表示lxh和pfz的初始位置的编号X,Y(1<=X,Y<=N,X!=Y),lxh总是先移动
Output
对于每次询问,输出一行,输出获胜者的名字
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
Source
电子科技大学第六届ACM程序设计大赛 初赛
1 #include<iostream> 2 #include<algorithm> 3 #include<queue> 4 #include<cstdio> 5 #include<cstdlib> 6 #include<cstring> 7 #include<cmath> 8 using namespace std; 9 #define sr(x) scanf("%d",&x) 10 #define sc(x) printf("%d",x) 11 #define hh printf("\n") 12 #define mod 2011 13 int main() 14 { 15 int n,m; 16 while(scanf("%d%d",&n,&m)) 17 { 18 if(n==0&&m==0)break; 19 int i,a,b,jj[100005]; 20 for(i=0;i<=n;i++)jj[i]=0; 21 for(i=1;i<n;i++) 22 { 23 sr(a);sr(b); 24 jj[b]=a; 25 } 26 while(m--) 27 { 28 sr(a);sr(b); 29 for(;;) 30 { 31 if(jj[a]!=0)a=jj[a]; 32 else {printf("lxh\n");break;} 33 if(jj[b]!=0)b=jj[b]; 34 else {printf("pfz\n");break;} 35 } 36 } 37 } 38 return 0; 39 }