题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2545

思路:dist[u]表示节点u到根节点的距离,然后在查找的时候更新即可,最后判断dist[u],dist[v]的大小即可。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define MAXN 101000 int n,m;
int parent[MAXN];
int dist[MAXN]; int Find(int x)
{
if(x==parent[x]){
return parent[x];
}
int tmp=parent[x];
parent[x]=Find(parent[x]);
dist[x]+=dist[tmp];//更新到根节点的距离
return parent[x];
} int main()
{
int u,v;
while(~scanf("%d%d",&n,&m)){
if(n==&&m==)break;
for(int i=;i<=n;i++){
parent[i]=i;
dist[i]=;
}
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
parent[v]=u;
dist[v]=;
}
while(m--){
scanf("%d%d",&u,&v);
Find(u);
Find(v);
if(dist[u]<=dist[v]){
puts("lxh");
}else
puts("pfz");
}
}
return ;
}

hdu 2545(并查集求节点到根节点的距离)的更多相关文章

  1. 树上战争(HDU 2545 并查集求解点到根节点长度)

    树上战争 Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  2. hdu 2545 并查集 树上战争

    #include<stdio.h> #include<string.h> #define N 110000 struct node {     int father,count ...

  3. HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...

  4. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  5. HDU 3926 并查集 图同构简单判断 STL

    给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...

  6. hdu 2818(并查集,带权更新)

    Building Block Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环

    D. Dividing Kingdom II   Long time ago, there was a great kingdom and it was being ruled by The Grea ...

  8. C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块

    C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  9. hdu 1116 并查集判断欧拉回路通路

    判断一些字符串能首尾相连连在一起 并查集求欧拉回路和通路 Sample Input 3 2 acm ibm 3 acm malform mouse 2 ok ok Sample Output The ...

随机推荐

  1. jquery json ajax

    当html中用script包含了不在同一个目录下的js外部文件(主要是为了通用代码的重用)时,这个js文件的 内容就如同在当前html文件中了,写jquery的时候不用考虑路径问题,可以直接引用htm ...

  2. fedora终端快捷键

    在name框中填写这个快捷键的名称,比如open terminal(打开终端窗口) 在command框中填写执行这个快捷键的shell命令,此处可填写gnome-terminal

  3. logback logback.xml常用配置详解(三)

    logback logback.xml常用配置详解 <filter> <filter>: 过滤器,执行一个过滤器会有返回个枚举值,即DENY,NEUTRAL,ACCEPT其中之 ...

  4. javascript常量

    javascript中没有常量的概念,虽然许多现代的变成环境可能为您提供了用以创建常量的const语句.对于的自己的变量,可以采用相同的命名约定,并且将他们以静态属性的方式添加到构造函数中. //构造 ...

  5. HTML中的IE条件注释

    IE条件注释是一种特殊的HTML注释,这种注释只有IE5.0及以上版本才能理解.比如普通的HTML注释是: <!--This is a comment--> 而只有IE可读的IE条件注释是 ...

  6. characterCustomezition的资源打包代码分析

    using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine; class Creat ...

  7. 修改 ~/.bashrc显示 git 当前分支

    vim ~/.bashrc # git branch show configuration PS1="\\w:\$(git branch 2>/dev/null | grep '^*' ...

  8. MFC加载皮肤 转自:http://www.cctry.com/thread-4032-1-1.html

    VC皮肤库SkinSharp 1.0.6.6的使用: SkinSharp又称Skin#,是Windows环境下一款强大的换肤组件.SkinSharp作为换肤控件,只需要在您的程序中添加一行代码,就能让 ...

  9. 详解mysql int类型的长度值问题

    我的朋友海滨问我mysql在建表的时候int类型后的长度代表什么? 是该列允许存储值的最大宽度吗? 为什么我设置成int(1), 也一样能存10,100,1000呢. 当时我虽然知道int(1),这个 ...

  10. Python egg

    http://blog.csdn.net/turkeyzhou/article/details/8876658