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 }

时间: 2024-10-08 22:19:34

hdu 2545 求当前结点到根节点的距离的相关文章

LA 3027 Corporative Network(并查集,求某个节点到根节点的距离)

A very big corporation is developing its corporative network. In the beginning each of the N enterprisesof the corporation, numerated from 1 to N, organized its own computing and telecommunication center.Soon, for amelioration of the services, the co

C#中treeview的问题,如何区分根节点和子节点以及根节点和根节点的兄弟节点?

根节点的Level属性为0,一级子节点Level属性为1,二级子节点Level属性为2,以此类推:同级节点可以用索引.名称.文本来区分.用索引区分根节点时,TreeView.Nodes[0]就是第一个根节点,TreeView.Nodes[1]就是第二个根节点,以此类推:用索引区分一级子节点时,TreeView.Nodes[0].Nodes[0]为第一个根节点的第一个子节点,TreeView.Nodes[0].Nodes[1]是第一个根节点的第二个子节点,以此类推: 2.如何获取TreeView点

(DS 《算法竞赛入门经典》)LA 3027 Corporative Network(查询某一个节点到根节点之间的距离)

题目大意: 查询某一个节点到根节点之间的距离 解题思路: 加权并查集问题.之前做的题目是"查看两个或多个节点是否在同一个集合下",现在的题目是"查询某个节点到 根节点之间的距离".之前只需要使用到father[x]这个数组,用来表示x的父亲节点是谁.现在引入dist[x]数组,用来记录 x节点到根节点的距离 1)在并查集中,根节点不懂,其他节点都可以动. A very big corporation is developing its corporative net

(hdu step 5.1.5)Dragon Balls(求并查集的根节点、节点数和个结点的移动次数)

题目: Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 562 Accepted Submission(s): 239   Problem Description Five hundred years later, the number of dragon balls will increase unexpected

求二叉树的高度,叶子节点个数,第K层结点个数,求祖先结点问题

一.求二叉树的高度 类似归并排序的思想.先求最底层结点的高度,再分别比较生成更高的结点的高度.最后递归至根结点,求出根结点的高度. //求二叉树的高度 int Height() { return GetHeight(_root); } int GetHeight(Node<T> *root) { int left = 0; int right = 0; if (root->_leftchild != NULL) left += GetHeight(root->_leftchild)

HDU 1890 - Robotic Sort - [splay][区间反转+删除根节点]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description Somewhere deep in the Czech Technical University buildings, there are laboratories for examining

二叉树中所有的路径(从根节点到叶子结点)

1 import java.util.ArrayList; 2 3 /** 4 * 寻找最短的二叉搜索的路径,从根节点到叶子结点 5 * 6 * @author jinfeng 7 * 8 */ 9 public class FindShortestBTPath { 10 11 // 用来记录所有的路径 12 private ArrayList<ArrayList<Integer>> allPaths = new ArrayList<ArrayList<Integer&

Dragon Balls HDU杭电3635 【并查集,递归的方法找根节点】

Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficult for Monkey King(WuKong) to gather all of the dragon balls together. His country has N cities and there are exactly N dragon bal

J 判断二叉树每个结点的权值是否关于根节点完全对称

如果二叉树每个结点的权值关于根节点完全对称 就输出Yes Sample Input 27 //结点1 2 3 //结点1的左孩子是结点2 右孩子是结点32 4 53 6 74 0 05 0 06 0 07 0 01 2 2 3 4 4 3 //权值51 2 32 0 43 0 54 0 05 0 01 2 2 3 3 Sample Output YesNo 1 # include <cstdio> 2 # include <cstring> 3 # define LL long l