给一棵树,如果树上的某个节点被某个人占据,则它的所有儿子都被占据,lxh和pfz初始时分别站在两个节点上,谁当前所在的点被另一个人占据,他就输了比赛,问谁能获胜。

Input

输入包含多组数据

每组第一行包含两个数NN,MM(NN,M≤100000M≤100000),NN表示树的节点数,MM表示询问数,N=M=0N=M=0表示输入结束。节点的编号为11到NN。

接下来N−1N−1行,每行22个整数AA,BB(1≤A1≤A,B≤NB≤N),表示编号为AA的节点是编号为BB的节点的父亲。

接下来MM行,每行有22个数,表示lxh和pfz的初始位置的编号XX,YY(1≤X1≤X,Y≤NY≤N,X≠YX≠Y),lxh总是先移动。

Output

对于每次询问,输出一行,输出获胜者的名字。

Sample input and output

Sample Input

Sample Output

2 1

1 2

1 2

5 2

1 2

1 3

3 4

3 5

4 2

4 5

0 0

lxh

pfz

lxh

Source

电子科技大学第六届ACM程序设计大赛 初赛

代码

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int dis[],fa[];//dis到根节点距离
int n,m; int Find(int x){
if(dis[x]>) return dis[x]; if(x==fa[x]) return ;
else return dis[x]=Find(fa[x])+;
}
void init_(){
memset(dis,,sizeof(dis));
for(int i=;i<=n;i++) fa[i]=i;
}
int main(){
// freopen("01.in","r",stdin);
while(scanf("%d%d",&n,&m)==&&n>&&m>){
init_();
for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
fa[v]=u;
}
while(m--){
int u,v;
scanf("%d%d",&u,&v); if(Find(u)<=Find(v))puts("lxh");
else puts("pfz");
}
}
return ;
}

谁离根近谁胜利

之前写了个dfs最短路不知道为什么错了,待定!!!

结论大概是初始化有问题,待改!!!

cdoj 树上战争(Battle on the tree) Label:并查集?的更多相关文章

  1. UESTC 32 树上战争(Battle on the tree)

    这题其实很简单,每个人肯定都往上走,才能保证尽快赢,所以无非是看谁离根节点近,即深度小..用并查集中的findset思想,不断找父节点一直到根节点来找深度就可以了. 代码: #include < ...

  2. cdoj32-树上战争(Battle on the tree) 【记忆化搜索】

    http://acm.uestc.edu.cn/#/problem/show/32 树上战争(Battle on the tree) Time Limit: 12000/4000MS (Java/Ot ...

  3. 【BZOJ2959】长跑(Link-Cut Tree,并查集)

    [BZOJ2959]长跑(Link-Cut Tree,并查集) 题面 BZOJ 题解 如果保证不出现环的话 妥妥的\(LCT\)傻逼题 现在可能会出现环 环有什么影响? 那就可以沿着环把所有点全部走一 ...

  4. Codeforces 915F Imbalance Value of a Tree(并查集)

    题目链接  Imbalance Value of a Tree 题意  给定一棵树.求树上所有简单路径中的最大权值与最小权值的差值的和. 首先考虑求所有简单路径中的最大权值和. 对所有点按照权值大小升 ...

  5. Is It A Tree?(并查集)

    Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...

  6. POJ1308:Is It A Tree?(并查集)

    Is It A Tree? 题目链接:http://poj.org/problem?id=1308 Description: A tree is a well-known data structure ...

  7. hdu1325 Is It A Tree? 基础并查集

    #include <stdio.h> #include <string.h> ], g[]; int find(int x) //并查集的查找,找到共同的父亲 { if (f[ ...

  8. POJ1308/HDU1325/NYOJ129-Is It A Tree?,并查集!

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28838   Accepted: 9843 -& ...

  9. Aizu - 2564 Tree Reconstruction 并查集

    Aizu - 2564 Tree Reconstruction 题意:一个有向图,要使得能确定每一条边的权值,要求是每个点的入权和出权相等,问你最少需要确定多少条边 思路:这题好像有一个定理之类的,对 ...

随机推荐

  1. 自定义log日志

        Log.cs (这个已经不能用了,用下面的问题解决方案) using System; using System.Collections.Generic; using System.Web; u ...

  2. Windows 历史

  3. android 入门-android属性介绍

      android:visibility="gone" 不保留view控件所占有的空间 隐藏 android:visibility="invisible" 保留 ...

  4. 在linux配置NFS用于RAC的搭建

    rac的共享存储有很多种搭建方式,nfs是其中一种.生产环境一般不采用nfs,多用于测试. nfs搭建步骤大致分为如下: 1.划盘 给节点1挂载一块磁盘,并将磁盘分区,并格式化,再挂载 [root@n ...

  5. 静态/动态函数库设计,王明学learn

    静态/动态函数库设计 Linux应用程序设计中需要的外部函数主要由函数库和系统调用来提供. 两者区别: 一.函数库分类 函数库按照链接方式可分为: 1.静态链接库 对函数库的链接是放在编译时期(com ...

  6. error C2039: “bind2nd”: 不是“std”的成员

    VS2012 出现如下错误: error C2039: "bind2nd": 不是"std"的成员     头文件中加上 #include <functi ...

  7. 发送http请求get方法

    //获取网页html NSURL* url = [NSURL URLWithString:@"http://www.baidu.com"]; NSMutableURLRequest ...

  8. set[c++]

    #include <iostream> using namespace std; #include <set> int main(int argc, const char * ...

  9. Printf()输出格式控制(转)

    int printf(const char *format,[argument]); format 参数输出的格式,定义格式为: %[flags][width][.perc] [F|N|h|l]typ ...

  10. Linux编程(3) MakeFile

    1. 在Linux中,make工具可以维护程序模块关系和生成可执行程序.它可根据程序模块的修改情况重新编译链接生成中间代码或最终的可执行程序.执行make命令,需要一个名为Makefile的文本文件, ...