hdu 2545 并查集 树上战争

#include<stdio.h>
#include<string.h>
#define N 110000
struct node {
    int father,count;
}pre[N];
int find(int x) {
if(x==pre[x].father) {
pre[x].count=0;
return x;
}
        int h=pre[x].father;
       pre[x].father=find(pre[x].father);
      pre[x].count+=pre[h].count;
return pre[x].father;
}
int main() {
  int n,m,a,b,i;
  while(scanf("%d%d",&n,&m),n||m) {
      for(i=1;i<=n;i++) {
          pre[i].father=i;
          pre[i].count=1;
      }
      for(i=1;i<n;i++) {
          scanf("%d%d",&a,&b);
             pre[b].father=a;
      }
      while(m--) {
     scanf("%d%d",&a,&b);
     find(a);find(b);
     if(pre[a].count<=pre[b].count)
         printf("lxh\n");
     else
         printf("pfz\n");
      }
}
return 0;
}
posted @ 2014-03-08 17:01  HYDhyd  阅读(82)  评论(0编辑  收藏  举报