题目链接: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. MySql 创建只读账号

    GRANT Select ON *.* TO reader@192.168.1.123  IDENTIFIED BY "123456" GRANT  可以立刻生效 在mysql 5 ...

  2. 他们在军训,我在搞 OI(一)

    Day 1 理论上,我现在不应该坐在电脑前打字,因为早在今天上午 6:20 全体新高一同学就坐车前往军(无)训(尽)基(炼)地(狱)了,而今天上午 6:20 我还在被窝里呢…… 没错,我旷掉了军训,然 ...

  3. hiho一下 第九十四周 数论三·约瑟夫问题

    数论三·约瑟夫问题 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho的班级正在进行班长的选举,他们决定通过一种特殊的方式来选择班长. 首先N个候选人围成一个 ...

  4. Shell编程基础

    写之前我们先来搞清楚为什么要学shell,学习要有目的性shell简单.灵活.高效,特别适合处理一些系统管理方面的小问题shell可以实现自动化管理,让系统管理员的工作变得容易.简单.高效shell脚 ...

  5. 回调函数callback

    你到一个商店买东西,刚好你要的东西没有货,于是你在店员那里留下了你的电话,过了几天店里有货了,店员就打了你的电话,然后你接到电话后就到店里去取了货.在这个例子里,你的电话号码就叫回调函数,你把电话留给 ...

  6. Verify Preorder Serialization of a Binary Tree

    One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...

  7. eclipse对Java程序的移植

    有些Java项目可能不在同一台计算机上开发,所以程序需要平台间进行移植,方法很简单,首先有一个最简单的项目HelloJava 当我们开发完成或者要休息了,一般都会保存然后在项目上右击,选择Close ...

  8. php开发网站编码统一问题

    一个良好的网站代码整洁,注释适当是最基本的,也是好的习惯,这可以避免以后的非常乱了自己感觉都乱,一旦重构麻烦就大了耗时耗力,其中网站整个体系的编码是最重要的一个方面,为了网站的稳定性建议php程序,H ...

  9. GLSL

    变量修饰符 修饰符给出了变量的特殊含义,GLSL中有如下修饰符: ·const – 声明一个编译期常量. ·attribute– 随不同顶点变化的全局变量,由OpenGL应用程序传给顶点shader. ...

  10. July 20th, Week 30th Wednesday, 2016

    Learn from yesterday, live for today, and hope for tomorrow. 借鉴昨天,活着当下,憧憬未来. Yesterday is the past, ...