UVALive - 3644 X-Plosives (并查集)
2017-07-27 13:55
375 查看
思路:每一个product都可以作一条边,每次添加一条边,如果这边的加入使得某个集合构成环,就应该refuse,那么就用并查集来判断。
AC代码:
如有不当之处欢迎指出!
AC代码:
//#define LOCAL #include <stdio.h> #include <string.h> const int maxn = 1e5 + 5; int par[maxn], rank[maxn]; void init() { memset(rank, 0, sizeof(rank)); for(int i = 0; i <= maxn; i++) { par[i] = i; } } int findRoot(int x) { return x != par[x] ? par[x] = findRoot(par[x]) : x; } //启发式合并 void unionSet(int x, int y) { if(rank[x] > rank[y]) { par[y] = x; } else { par[x] = y; if(rank[x] == rank[y]) rank[y]++; } } int main() { #ifdef LOCAL freopen("data.in", "r", stdin); freopen("data.out", "w", stdout); #endif // LOCAL int x, y, refuse = 0; init(); while(scanf("%d", &x) == 1) { if(x == -1) { printf("%d\n", refuse); init(); refuse = 0; }else { scanf("%d", &y); x = findRoot(x); y = findRoot(y); if(x == y) { //refuse it refuse++; } else { //union unionSet(x, y); } } } return 0; }
如有不当之处欢迎指出!
相关文章推荐
- UVALive(LA) 3644 X-Plosives (并查集)
- UVALive - 3644 X-Plosives 并查集
- UVALive(LA) 3644 X-Plosives (并查集)
- UVALive 3644 X-Plosives (并查集)
- UVALive - 3644 X-Plosives(并查集判断是否有环)
- 指南 第三章 例题5 UVALive 3644 X-Plosives(并查集)
- UVALive - 3644 - X-Plosives (并查集!!)
- UVALive 3644 X-Plosives 并查集
- UVALive - 3644 X-Plosives 并查集
- uvalive 3644 X-Plosives(并查集)
- UVALive - 3644X-Plosives(并查集)
- UVALIVE 3644 X-Plosives
- UVa Live (LA) 3644 典型并查集
- 【暑假】[实用数据结构]UVAlive 3644 X-Plosives
- UVALive 3644 X-Plosives
- UVALive - 3644 并查集
- 【UVALive3644】X-Plosives
- UVALive 3644 X-Plosives
- UVALive 3644 X-Plosives
- UVALive - 3644 X-Plosives