POJ 1703 Find them, Catch them (并查集&利用异或的性质优化)
来源:互联网 发布:街机时代 知乎 编辑:程序博客网 时间:2024/06/06 01:42
http://poj.org/problem?id=1703
思路和食物链那题一样的。
完整代码:
/*297ms,1156KB*/#include<cstdio>#include<cstring>int fa[100005], rk[100005];int find(int x){if (fa[x]){int tmp = fa[x];fa[x] = find(fa[x]);/// 最终找到0这个根rk[x] ^= rk[tmp]; /// 用异或的性质优化return fa[x];}return x;}void merge(int x, int y){int xx = find(x), yy = find(y);if (xx != yy){fa[yy] = xx;rk[yy] = rk[x] ^ rk[y] ^ 1; /// 用异或的性质优化}}int main(){int t, n, m, x, y, i;char ch;scanf("%d", &t);while (t--){scanf("%d%d", &n, &m);memset(fa, 0, sizeof(fa));///以0为根memset(rk, 0, sizeof(rk));while (m--){getchar();scanf("%c%d%d", &ch, &x, &y);if (ch == 'D') merge(x, y);else{if (find(x) != find(y)) puts("Not sure yet.");else if (rk[x] == rk[y]) puts("In the same gang.");else puts("In different gangs.");}}}return 0;}
0 0
- POJ 1703 Find them, Catch them (并查集&利用异或的性质优化)
- POJ 1703 Find them, Catch them(并查集)
- POJ 1703 Find them, Catch them //并查集
- poj 1703 Find them, Catch them 并查集
- poj 1703 Find them, Catch them(并查集)
- poj 1703 Find them, Catch them //并查集
- POJ 1703 Find them, Catch them 并查集
- poj 1703 Find them, Catch them (并查集)
- poj 1703 Find them, Catch them(种类并查集)
- poj 1703 Find them, Catch them(种类并查集)
- poj 1703 -- Find them, Catch them(并查集)
- poj 1703 Find them, Catch them(并查集应用)
- POJ 1703 Find them, Catch them (并查集)
- poj 1703 - Find them, Catch them(并查集)
- poj 1703 Find them, Catch them(关系并查集)
- POJ 1703 Find them, Catch them 种类并查集
- POJ 1703 Find them, Catch them (关系并查集)
- POJ-1703 Find them, Catch them 经典并查集
- 12个最好的开源报表工具
- Android中回调接口的使用
- Jeckson使用技巧
- Android设计模式系列(8)--SDK源码之工厂方法模式
- 2014年八大最热门的大数据工作
- POJ 1703 Find them, Catch them (并查集&利用异或的性质优化)
- 不同统计图的使用场景
- Android设计模式系列(9)--SDK源码之适配器模式
- Android热点回顾
- cvApproxPoly的学习
- Evaluate Reverse Polish Notation 逆波兰表达式
- 10个用来处理键盘事件的JQuery插件和JS类库
- 关于linux和shell的相关书籍
- 通过java代码获取spring Context 下的bean