cdoj 树上战争(Battle on the tree) Label:并查集?
给一棵树,如果树上的某个节点被某个人占据,则它的所有儿子都被占据,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:并查集?的更多相关文章
- UESTC 32 树上战争(Battle on the tree)
这题其实很简单,每个人肯定都往上走,才能保证尽快赢,所以无非是看谁离根节点近,即深度小..用并查集中的findset思想,不断找父节点一直到根节点来找深度就可以了. 代码: #include < ...
- cdoj32-树上战争(Battle on the tree) 【记忆化搜索】
http://acm.uestc.edu.cn/#/problem/show/32 树上战争(Battle on the tree) Time Limit: 12000/4000MS (Java/Ot ...
- 【BZOJ2959】长跑(Link-Cut Tree,并查集)
[BZOJ2959]长跑(Link-Cut Tree,并查集) 题面 BZOJ 题解 如果保证不出现环的话 妥妥的\(LCT\)傻逼题 现在可能会出现环 环有什么影响? 那就可以沿着环把所有点全部走一 ...
- Codeforces 915F Imbalance Value of a Tree(并查集)
题目链接 Imbalance Value of a Tree 题意 给定一棵树.求树上所有简单路径中的最大权值与最小权值的差值的和. 首先考虑求所有简单路径中的最大权值和. 对所有点按照权值大小升 ...
- Is It A Tree?(并查集)
Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...
- POJ1308:Is It A Tree?(并查集)
Is It A Tree? 题目链接:http://poj.org/problem?id=1308 Description: A tree is a well-known data structure ...
- hdu1325 Is It A Tree? 基础并查集
#include <stdio.h> #include <string.h> ], g[]; int find(int x) //并查集的查找,找到共同的父亲 { if (f[ ...
- POJ1308/HDU1325/NYOJ129-Is It A Tree?,并查集!
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28838 Accepted: 9843 -& ...
- Aizu - 2564 Tree Reconstruction 并查集
Aizu - 2564 Tree Reconstruction 题意:一个有向图,要使得能确定每一条边的权值,要求是每个点的入权和出权相等,问你最少需要确定多少条边 思路:这题好像有一个定理之类的,对 ...
随机推荐
- ASP.NET Web API中使用GZIP 或 Deflate压缩
对于减少响应包的大小和响应速度,压缩是一种简单而有效的方式. 那么如何实现对ASP.NET Web API 进行压缩呢,我将使用非常流行的库用于压缩/解压缩称为DotNetZip库.这个库可以使用Nu ...
- [LeetCode] TwoSum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- MD5与Base64的思考
MD5加密是对任意长的数据使用MD5哈稀算法散列为4个32位组,若格式化为ASCII字符则为16字符,若格式化16进制表示,则为32字符. (MD5的具体算法请参阅相关书籍和资料) MD5广泛用于数 ...
- iphone手机不同版本兼容、横竖屏
/* 兼容问题*/ @media screen and (device-width: 320px) and (device-height: 480px) and (-webkit-device-pix ...
- MyBatis传入参数与parameterType
参考:http://openwares.net/database/mybatis_parametertype.html Mybatis的Mapper文件中的select.insert.update.d ...
- BRIEF 特征描述子
Binary Robust Independent Elementary Features www.cnblogs.com/ronny 1. BRIEF的基本原理 我们已经知道SIFT特征采用了128 ...
- 在Salesforce中避免对Trigger中Update的无限循环操作
在Salesforce中避免对Trigger中Update的无限循环操作: 处理Trigger的时候会有这么一个场景:在Trigger中想修改该Object的某些字段的值,那么如果们在程序中再用代码的 ...
- LeetCode——Single Number(找出数组中只出现一次的数)
问题: Given an array of integers, every element appears twice except for one. Find that single one. No ...
- 在苹果手机上input有内阴影怎么去除
一个input中在安卓手机上完全按照自己的样式去展示,但是在苹果手机上发现Input有内阴影,怎么去除内阴影呢? 在input样式中这样添加 #div{ .... appearance:button; ...
- SQL Case when 的使用方法(转)
Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...