• 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
    ") ;
    52             else
    53                 printf("pfz
    ") ;
    54         }
    55 
    56     }
    57     return 0;
    58 }
    View Code
  • 相关阅读:
    PHP 输出true false
    code::blocks 注释快捷键
    GDAL 网址
    wine qq2011安装
    C++ ACM解题
    C++内存分配秘籍—new,malloc,GlobalAlloc详解(Zhuan)
    grub4dos初级教程-入门篇(Z)
    GDAL 编译(转)
    ubuntu双系统安装
    shapfile格式说明(转)
  • 原文地址:https://www.cnblogs.com/mengchunchen/p/4598963.html
Copyright © 2020-2023  润新知