UVa 1160 X-Plosives (并查集)
2014-03-04 12:51
423 查看
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=497&page=show_problem&problem=3601
要安全,这个“图”至多是一颗树(n个点,n-1条边),所以每次判断新加入的两个元素a,b是否都在树上,若是,则加入后树变图(n个点,n条边),此时应拒绝,否则合并a,b
完整代码:
要安全,这个“图”至多是一颗树(n个点,n-1条边),所以每次判断新加入的两个元素a,b是否都在树上,若是,则加入后树变图(n个点,n条边),此时应拒绝,否则合并a,b
完整代码:
/*0.015s*/ #include <cstdio> #include<cstring> const int mx = 100005; int fa[mx], rk[mx]; int find(int x) {return ~fa[x] ? fa[x] = find(fa[x]) : x;} ///其实不用启发式合并也行,毕竟数据量超小 void merge(int x, int y) { if (rk[x] < rk[y]) fa[x] = y; else { fa[y] = x; if (rk[x] == rk[y]) ++rk[x]; } } int main() { int x, y; while (~scanf("%d", &x)) { memset(fa, -1, sizeof(fa)); memset(rk, 0, sizeof(rk)); int refusals = 0; while (x != -1) { scanf("%d", &y); x = find(x), y = find(y); if (x == y) ++refusals; /// 如果x和y在同一个集合,则拒绝 else merge(x, y); /// 否则合并 scanf("%d", &x); } printf("%d\n", refusals); } return 0; }
相关文章推荐
- Uva 1160 X-Plosives ——并查集
- UVa 1160 X-Plosives(并查集)
- uva 1160 - X-Plosives(并查集)
- UVA 1160 X-Plosives【并查集】
- UVA 1160 X-Plosives(并查集)
- UVA 1160 X-Plosives(并查集)
- 并查集uva1160 - X-Plosives
- UVA 1160 - X-Plosives(并查集)
- uva1160 - 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(并查集)
- uva 1160 - X-Plosives(并查集)
- UVA 1160 X-Plosives
- UVa 1160 X-Plosives
- 指南 第三章 例题5 UVALive 3644 X-Plosives(并查集)
- uva 1160 x plosives