树上战争 HDU-2545 【并查集】
来源:互联网 发布:淘宝店铺怎么发布微淘 编辑:程序博客网 时间:2024/05/17 02:53
题意:两个人A,B在树上的两个节点上,当一个人在一个节点的时候,节点的子树全都属于该者。 谁先占领了另一方的节点,那么该方胜利。
思路: 并查集。 谁离根节点近,谁就是胜利者
#include <bits/stdc++.h>using namespace std;typedef long long ll;int n,m;int par[100000+50];void init(){ for(int i=1; i<=n; i++) par[i]=i;}int find(int root){ int cnt=0; while(par[root]!=root) { root=par[root]; cnt++; } return cnt;}int main(void){ while(cin >> n >> m,n,m) { init(); for(int i=1; i<=n-1; i++) { int father,son; scanf("%d%d",&father,&son); par[son]=father;// 建树 } for(int i=1; i<=m; i++) { int x,y; scanf("%d%d",&x,&y); if(find(x)<=find(y))// lxh先手所以有等号 printf("lxh\n"); else printf("pfz\n"); } }}
阅读全文
0 0
- hdu 2545 并查集 树上战争
- HDU 2545 树上战争(并查集)
- HDU 2545 树上战争 (并查集)
- HDU 2545 树上战争(并查集)
- hdu 2545 树上战争 并查集
- 树上战争 HDU-2545 【并查集】
- hdu 2545 树上战争(加权并查集)
- hdu 2545 树上战争(并查集)
- HDU 2545 树上的战争 (并查集)
- hdu 2545 树上战争(并查集)
- HDU 2545 树上战争(并查集)
- hdu 2545 树上战争(并查集)
- hdu 2545 树上战争(并查集)
- HDU - 2545 树上战争(没有路径压缩的并查集)
- 杭电 2545 树上战争(并查集过)
- hdu2545 树上战争 (并查集)
- 树上战争_hdu_2545(并查集).cpp
- HDU2545:树上战争(并查集)
- python时间格式化
- [LintCode]Binary Tree Maximum Node(C++)
- Unity中 保存图片到相册
- mybatis自动生成代码generator.xml的配置
- 我个人理解的界面设计原则
- 树上战争 HDU-2545 【并查集】
- NDK的基础教程 二 c语言回调java语言
- dedecms栏目路由命名规则
- 大数据学习笔记:启动脚本分析,start-dfs.cmd与start-yarn.cmd
- 设计模式(21)--策略模式
- Android与H5交互
- 【Machine Learning】笔记:Train/Validation/Test Sets
- js实现页面显示时间
- 汉诺塔(三)