POJ - 1703 - Find them, Catch them (并查集)
2015-05-12 00:04
441 查看
题目传送:Find them, Catch them
思路:用一个关系数组记录当前结点与其父亲的关系,0表示同类,1表示不同类
AC代码:
思路:用一个关系数组记录当前结点与其父亲的关系,0表示同类,1表示不同类
AC代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <vector> #include <map> #include <set> #include <deque> #include <cctype> #define LL long long #define INF 0x7fffffff using namespace std; const int maxn = 100005; int T; int n, m; int pa[maxn]; int rela[maxn];//±íʾ½áµãÓëÆ丸Ç׵ĹØϵ£¬Í¬ÀàΪ0£¬²»Í¬ÀàΪ1£¬³õʼʱͬÀà int find(int x) { int t = pa[x]; if(pa[x] == x) { return x; } else { pa[x] = find(pa[x]); rela[x] = (rela[x] == rela[t]) ? 0 : 1;//µÈÓÚ0±íʾÊôÓÚͬһÀà return pa[x]; } } int main() { scanf("%d", &T); while(T --) { scanf("%d %d", &n, &m); for(int i = 1; i <= n; i ++) { //³õʼ»¯ pa[i] = i; rela[i] = 0; } for(int i = 0; i < m; i ++) { int a, b; char op[5]; scanf("%s %d %d", op, &a, &b); int fa = find(a), fb = find(b); if(op[0] == 'A') { if(fa != fb) { printf("Not sure yet.\n"); } else if(rela[a] == rela[b]){ cout << "In the same gang." << endl; } else { cout << "In different gangs." << endl; } } else { if(fa != fb) { pa[fa] = fb; rela[fa] = (rela[a] == rela[b]) ? 1 : 0;//a,b¿Ï¶¨²»ÔÚͬһ¸ö°ïÅÉ } } } } 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 二分图 并查集
- POJ1703 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【并查集】