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 }
View Code

 

转载于:https://www.cnblogs.com/mengchunchen/p/4598963.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值