UVa 1160 X-Plosives(并查集)
来源:互联网 发布:linux 目录权限递归 编辑:程序博客网 时间:2024/05/17 23:20
题目链接:UVa 1160 X-Plosives
把元素当成顶点,把化合物当成边,当整个图存在环的时候,组成环的边对应的化合物是a powerful explosive,使用并查集判断加入一条边后是否存在环,如果存在,那么这条边不能加入。并查集一个集合中的点两两连通,如果两个元素已经属于一个集合,那么这两个元素间再加一条边肯定会出现环。
#include <iostream>#include <cstdio>using namespace std;const int MAX_N = 100000 + 100;int p[MAX_N];int _find(int x){ return x == p[x] ? x : (p[x] = _find(p[x]));}int main(){ int a, b, u, v, res; while(scanf("%d", &a) != EOF) { res = 0; for(int i = 0; i < MAX_N; i++) p[i] = i; while(a != -1) { scanf("%d", &b); u = _find(a); v = _find(b); if(u == v) res++; else p[u] = v; scanf("%d", &a); } printf("%d\n",res); } return 0;}
0 0
- UVa 1160 X-Plosives (并查集)
- UVa 1160 X-Plosives(并查集)
- uva 1160 X-Plosives (并查集)
- UVA 1160 - X-Plosives(并查集)
- uva 1160 - X-Plosives(并查集)
- UVA 1160X-Plosives【并查集】
- UVA 1160 X-Plosives(并查集)
- X-Plosives - UVa 并查集
- Uva 1160 X-Plosives ——并查集
- uva 1160 - X-Plosives(并查集)
- uva 1160 - X-Plosives(并查集)
- UVA 1160 X-Plosives(并查集)
- uva 1160 X-Plosives(并查集:基础运用)
- UVA 1160 - X-Plosives(并查集)
- UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环
- UVA 1160 - X-Plosives 并查集判断是否存在环
- LA 3644 - X-Plosives,并查集
- LA 3644 X-Plosives / 并查集
- OC语言自学《十二》---- OC(block、@property)
- C++ Primer 3.2.2
- vcastr 引用代码
- this指针
- codes
- UVa 1160 X-Plosives(并查集)
- OC语言自学《十一》---- OC(内存管理知识总结)
- codes2
- [IOS] 设置UIImagView显示圆角
- Win7环境下VS2010配置Cocos2d-x-2.1.4最新版本
- Linux基础 - 张松
- 最长递增子序列
- 串口调试时出现乱码
- 网站权重是网站优化的根本