poj 1703 - Find them, Catch them(并查集)
来源:互联网 发布:黑莓怎么改网络标识 编辑:程序博客网 时间:2024/05/25 19:56
思路:
只要两者的关系确定了,就将他们加入一个集合中,另外增加一个表示关系的数组flag,来表示该节点与其父节点的关系,0表示是同一类,1表示是不同团伙。
初始时,集合只有自己一个元素。flag设置为0.
代码如下:
const int M = 100005;int flag[M], p[M];int find(int x){ int tmp = p[x]; p[x] = (p[x]==x?x:find(p[x])); flag[x] = (flag[x]==flag[tmp]?0:1); return p[x];}char get_char(){ char c; while(1) { c = getchar(); if(c=='A'||c=='D') return c; }}int main(){ int t, n, m, a, b, c, x, y; scanf("%d", &t); while(t--) { scanf("%d%d", &n, &m); memset(flag,0,sizeof(flag)); for(int i = 0; i <= n; ++i) p[i] = i; for(int i = 0; i < m; ++i) { c = get_char(); scanf("%d %d", &a, &b); x = find(a); y = find(b); if(c=='A') { if(x!=y) puts("Not sure yet."); else if(flag[a]==flag[b]) puts("In the same gang."); else puts("In different gangs."); } else { if(x!=y) { p[x] = y; flag[x] = (flag[a]==flag[b]?1:0); } } } } return 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 并查集
- 矩形嵌套 ( 图和最长不上升子序列)
- ORACLE数据库密码忘记了的解决方法
- .net 评分计算--使用配置公式进行计算
- LAV Filter 源代码分析 2: LAV Splitter
- mahout kmeans 测试
- poj 1703 - Find them, Catch them(并查集)
- JS嵌套函数调用上下文的问题
- WOW6432Node真身
- nagiosgraph nan
- Dom4j 使用简介
- xml数据和xml文件的解析
- Visual Studio 2010挂不上Nunit断点问题
- java基础——多线程(两种传统方式)
- GCD实战一:使用串行队列实现简单的预加载