POJ1308 Is It A Tree?
来源:互联网 发布:mac apache php7配置 编辑:程序博客网 时间:2024/05/01 22:08
1. 并查集,有点忘了,看维基百科回顾起来了,三个操作,路径压缩、合并、查找父节点。代码很简单,具体参考http://zh.wikipedia.org/wiki/%E5%B9%B6%E6%9F%A5%E9%9B%86
2. WA了好多次,最后发现是当空树的时候也是一棵树,fuck。。。
#include <iostream>#include <fstream>#include <string>#include <algorithm>#include <cstring>#include <stack>#include <queue>#define maxn 1000using namespace std;int p[maxn], flag[maxn];int getfather(int x){ return p[x] == x ? x : p[x] = getfather(p[x]);}int main(){ int t = 1, a, b; while (cin >> a >> b && a != -1) { if (!a) { cout << "Case " << t++ << " is a tree." << endl; continue; } int is = 1, tail = 0; for (int i = 1; i < maxn; ++i) { p[i] = i; flag[i] = 0; } while (a) { if (is) { int ra = getfather(a), rb = getfather(b); if (ra == rb) { is = 0; } else { p[rb] = ra; flag[a] = flag[b] = 1; tail = max(tail, max(a, b)); } } cin >> a >> b; } if (!is) { cout << "Case " << t++ << " is not a tree." << endl; } else { int roots = 0; for (int i = 1; i <= tail; i++) { if (flag[i] && p[i] == i) { roots++; if (roots > 1) break; } } if (roots == 1) cout << "Case " << t++ << " is a tree." << endl; else cout << "Case " << t++ << " is not a tree." << endl; } } return 0;}
- poj1308 Is It A Tree?
- poj1308:Is It A Tree?
- POJ1308 Is It A Tree?
- POJ1308 Is It A Tree?
- poj1308 Is It A Tree
- poj1308 Is It A Tree?
- 【POJ1308】 Is It A Tree?
- poj1308 is it a tree?
- POJ1308 Is It A Tree?
- POJ1308 Is It A Tree?
- POJ1308 Is It A Tree?
- poj1308 Is It A Tree
- Is It A Tree?(POJ1308/HDU1325)(A)
- poj1308——Is It A Tree?
- hdu 1325 &&poj1308 Is It A Tree?
- POJ1308——Is It A Tree?
- poj1308&&openjudge310:Is It A Tree?
- poj1308、HDU1325 、Is It A Tree
- SQL函数和存储过程的区别
- MultiByteToWideChar和WideCharToMultiByte用法详解
- 修改eclipse中的$(user)变量
- IE、FF padding-top兼容性问题
- 关于补码的计算
- POJ1308 Is It A Tree?
- WPF窗体
- TortoiseSVN客户端使用教程
- create()传值为空的解决办法
- Android属性动画---Property Animation(六)
- 序列化及其代理
- Ant简要介绍
- .NET中的内存管理,GC机制,内存释放过程
- 基于Django+CouchDB组合实现翻页功能