hdu 2545 求当前结点到根节点的距离
求当前结点到根节点的距离
Sample Input
2 1 //n m
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
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # include <queue> 7 # define LL long long 8 using namespace std ; 9 10 const int MAXN = 100010; 11 int F[MAXN]; 12 int dist[MAXN] ; 13 14 int find(int x)//找x的祖先结点 15 { 16 if(F[x]==x) return x; 17 int t = F[x] ; 18 F[x]=find(F[x]); 19 dist[x] += dist[t] ; 20 return F[x] ; 21 } 22 23 int main() 24 { 25 //freopen("in.txt","r",stdin) ; 26 int n , m ; 27 while(scanf("%d %d", &n , &m) != EOF) 28 { 29 if (n == 0 && m == 0) 30 break ; 31 int u , v ; 32 int i ; 33 for(i = 1 ; i <= n ; i++) 34 { 35 F[i] = i ; 36 dist[i] = 0 ; 37 } 38 for(i = 1 ; i < n ; i++) 39 { 40 scanf("%d %d" , &u , &v) ; 41 F[v] = u ; 42 dist[v] = 1 ; 43 } 44 45 while(m--) 46 { 47 scanf("%d %d" , &u , &v) ; 48 find(u) ; 49 find(v) ; 50 if (dist[u] <= dist[v]) 51 printf("lxh\n") ; 52 else 53 printf("pfz\n") ; 54 } 55 56 } 57 return 0; 58 }