hdu 2545 树上战争(并查集)
来源:互联网 发布:excel2007办公软件 编辑:程序博客网 时间:2024/05/22 05:31
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2545
题 意:比较两个点与根结点的距离的大小
思 路:先将输入的点用并查集的方式处理好,再找两个点到根结点的距离,谁离的越近谁就赢,距离相等则先走的获胜。
代码如下:
#include <iostream>using namespace std;#include <string.h>#include <stdio.h>#include <queue>#include <algorithm>typedef long long LL;int father[100002];int findx( int x ){ int r = x, l = 0; while ( r != father[r] ) {r = father[r];l++;} return l;}int main(){ int n, m; while( scanf ( "%d %d", &n, &m ) != EOF ) { if( n == 0 && m == 0 ) break; for( int i = 0 ; i <= n; i ++ ) father[i]=i; for( int i = 1; i < n; i ++ ) { int x,y; scanf( "%d %d", &x, &y ); father[y] = x; } for( int i = 0; i < m; i ++ ) { int x,y; scanf ( "%d %d", &x, &y ); int fx = findx(x); int fy = findx(y); if( fx <= fy ) { printf("lxh\n"); } else printf("pfz\n"); } } return 0;}
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 树上的战争 (并查集)
- HDU2545:树上战争(并查集)
- HDU - 2545 树上战争(没有路径压缩的并查集)
- 杭电 2545 树上战争(并查集过)
- hdu2545 树上战争 (并查集)
- 树上战争_hdu_2545(并查集).cpp
- vim的基本使用
- down后数据不同步的实验
- 创建Maven创建src/main/java提示重复
- Oauth认证原理
- 在线教育遭遇瓶颈 扣丁领衔O2O时代
- hdu 2545 树上战争(并查集)
- git pro 笔记
- Java数据结构-线性表之队列
- 安装YII2
- 黑马程序员——Java交通灯管理系统&银行业务系统
- java的常量池问题
- Eclipse连接手机后打不开data目录
- zoj 2158 Truck History
- Pig的安装和使用方法