POJ 1703 Find them, Catch them (并查集&利用异或的性质优化)
2014-01-20 10:49
351 查看
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; }
相关文章推荐
- POJ 1703 Find them, Catch them 利用并查集的长度关系(我现在不敢说是不是距离了)
- hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them
- POJ 2492 A Bug's Life and POJ 1703 Find them, Catch them
- POJ 1703 Find them, Catch them
- poj 1703 Find them, Catch them
- (用树结构支持并查集8.2.2)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 (并查集2)
- POJ 1703 Find them, Catch them
- 并查集之关系分类 POJ - 1703 Find them, Catch them
- Find them, Catch them(POJ-1703)
- poj 1703 Find them, Catch them 带权并查集OR种类并查集
- 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 (并查集)