[HDU 1272] 小希的迷宫 并查集
来源:互联网 发布:java web get post 编辑:程序博客网 时间:2024/06/05 19:12
http://acm.hdu.edu.cn/showproblem.php?pid=1272
题意:中文题辣。。。
思路:并查集,需要处理森林。。。
#include <cstdio>#include <cstring>#include <iostream>using namespace std;typedef long long ll;int pre[100005];bool vis[100005];int sta[100005], top;int Init(){ memset(vis, false, sizeof(vis)); for(int i = 0; i < 100005; i++){ pre[i] = i; } return 0;}int Find(int rt){ int top = 0; while(pre[rt] != rt){ sta[top++] = rt; rt = pre[rt]; } while(top){ pre[sta[--top]] = rt; } return rt;}int Union(int x, int y){ if((x ^ y) & 1) pre[x] = y; else pre[y] = x; return 0;}int main(){ int x, y, rt; while(~scanf("%d%d", &x, &y) && ( x != -1 || y != -1)){ Init(); rt = x; pre[y] = rt; bool flag = true; vis[x] = vis[y] = true; int maxn = x > y ? x : y; while((x || y) && scanf("%d%d", &x, &y) && (x || y)){ if(flag){ vis[x] = vis[y] = true; maxn = x > maxn ? x : maxn; maxn = y > maxn ? y : maxn; x = Find(x); y = Find(y); } if(x == y) flag = false; if(flag){ Union(x, y); } } int cnt = 0; for(int i = 1; i <= maxn && flag; i++){ if(!vis[i]) continue; if(pre[i] == i){ cnt++; if(cnt >= 2){ flag = false; break; } } } if(flag){ printf("Yes\n"); } else{ printf("No\n"); } } return 0;}
0 0
- hdu 1272 小希的迷宫 并查集
- hdu 1272 小希的迷宫(并查集)
- hdu 1272 小希的迷宫(并查集)
- hdu 1272 小希的迷宫 (并查集)
- hdu 1272 小希的迷宫(并查集)
- hdu 1272 小希的迷宫(并查集)
- HDU 1272 小希的迷宫 并查集
- HDU 1272小希的迷宫(简单并查集)
- HDU-1272 小希的迷宫 并查集
- HDU 1272-小希的迷宫 并查集初级
- hdu 1272 小希的迷宫 (并查集)
- HDU 1272小希的迷宫 并查集
- 【并查集】 HDU 1272 小希的迷宫
- 并查集 HDU 1272 小希的迷宫
- hdu 1272 小希的迷宫(并查集)
- 【并查集】hdu 1272 小希的迷宫
- hdu 1272小希的迷宫 并查集
- HDU-#1272 小希的迷宫(并查集)
- nexus启动报错----->错误 1067: 进程意外终止。
- Ubuntu wifi已通过硬件禁止解决方案
- 九度oj-1155-鸡兔同笼
- IOS-笔记3(Optional,Array,Dictionary,Range等)
- 页面提交错误,页面间参数传递java.lang.NumberFormatException: null
- [HDU 1272] 小希的迷宫 并查集
- JMS笔记
- oracle11g 安装及plsql连接在win8 64位系统上
- Little Zu Chongzhi's Triangles 5135(数学)
- 单片机C语言code关键字
- 负载均衡VIP /LVS 的健康检测方式
- Socket编程的初步理解
- Angularjs的简单使用
- map的4种常见的插入元素的方式及区别