POJ 1703 POJ 2492 并查集 和 食物链差不多
2014-03-22 18:17
218 查看
用 a 表示 a 在组织 1 ,a+MAX 表示 a 在组织2
遇到 D,就把 a 在组织1 和 b在组织2 放入一个集合,把 a 在组织2 与 b 在组织 1 放入一个集合。
若查相同,则 same(a,b) 为true
若查不同,则 same(a,a+MAX) 为 true
否则就是不确定
这个题在查的时候是使用 || 来查 和使用 && 来查是一样滴,因为只给出 a b 不同是无法只出现一种答案的。
写并查集的时候。。。join 能不能别写SB 了。。。要两个父亲之间合并。。。
POJ 1703
POJ 2492
和上一道题目基本完全一样。
遇到 D,就把 a 在组织1 和 b在组织2 放入一个集合,把 a 在组织2 与 b 在组织 1 放入一个集合。
若查相同,则 same(a,b) 为true
若查不同,则 same(a,a+MAX) 为 true
否则就是不确定
这个题在查的时候是使用 || 来查 和使用 && 来查是一样滴,因为只给出 a b 不同是无法只出现一种答案的。
写并查集的时候。。。join 能不能别写SB 了。。。要两个父亲之间合并。。。
POJ 1703
#include <stdio.h> #include <iostream> #include <queue> #include <algorithm> #include <map> #include <vector> #include <cmath> #include <string.h> #include <stdlib.h> #include <time.h> #include <fstream> #include <set> #include <stack> using namespace std; #define READ freopen("acm.in","r",stdin) #define WRITE freopen("acm.out","w",stdout) #define ll long long #define ull unsigned long long #define PII pair<int,int> #define PDI pair<double,int> #define PDD pair<double,double> #define MII map<int,int>::iterator #define fst first #define sec second #define MS(x,d) memset(x,d,sizeof(x)) #define INF 0x3f3f3f3f #define ALL(x) x.begin(),x.end() #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define ROOT 0,n-1,1 #define PB push_back #define FOR(a,b,c) for(int a=b;a<c;a++) #define MOD 1000000007 #define keyTree (ch[ ch[root][1] ][0]) #define MAX 111111 int pre[MAX<<1]; void init() { for(int i=0;i<(MAX<<1);i++) pre[i]=i; } int find(int x) { return x==pre[x]?x:pre[x]=find(pre[x]); } bool same(int x,int y) { return find(x)==find(y); } void join(int x,int y) { int fx=find(x),fy=find(y);// Notice pre[fx]=pre[fy]; } int main() { READ; int cas; scanf("%d",&cas); while(cas--) { init(); int n,m; scanf("%d%d",&n,&m); for(int i=0;i<m;i++) { char op[3]; int a,b; scanf("%s%d%d",op,&a,&b); //cout<<same(a,b)<<endl; if(op[0]=='A') { if(same(a,b)&&same(a+MAX,b+MAX)) puts("In the same gang."); else if(same(a+MAX,b)&&same(a,b+MAX)) puts("In different gangs."); else puts("Not sure yet."); } if(op[0]=='D') { join(a,b+MAX); join(b,a+MAX); } } } return 0; }
POJ 2492
和上一道题目基本完全一样。
#include <stdio.h> #include <iostream> #include <queue> #include <algorithm> #include <map> #include <vector> #include <cmath> #include <string.h> #include <stdlib.h> #include <time.h> #include <fstream> #include <set> #include <stack> using namespace std; #define READ freopen("acm.in","r",stdin) #define WRITE freopen("acm.out","w",stdout) #define ll long long #define ull unsigned long long #define PII pair<int,int> #define PDI pair<double,int> #define PDD pair<double,double> #define MII map<int,int>::iterator #define fst first #define sec second #define MS(x,d) memset(x,d,sizeof(x)) #define INF 0x3f3f3f3f #define ALL(x) x.begin(),x.end() #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define ROOT 0,n-1,1 #define PB push_back #define FOR(a,b,c) for(int a=b;a<c;a++) #define MOD 1000000007 #define keyTree (ch[ ch[root][1] ][0]) #define MAX 111111 int pre[MAX<<1]; void init() { for(int i=0;i<(MAX<<1);i++) pre[i]=i; } int find(int x) { return x==pre[x]?x:pre[x]=find(pre[x]); } bool same(int x,int y) { return find(x)==find(y); } void join(int x,int y) { int fx=find(x),fy=find(y); pre[fx]=pre[fy]; } int main() { READ; int cas; scanf("%d",&cas); for(int T=1;T<=cas;T++) { printf("Scenario #%d:\n",T); init(); int n,m; scanf("%d%d",&n,&m); bool flag=true; for(int i=0;i<m;i++) { int a,b; scanf("%d%d",&a,&b); if(true&&same(a,b)) flag=false; join(a,b+MAX); join(a+MAX,b); } if(flag) puts("No suspicious bugs found!\n"); else puts("Suspicious bugs found!\n"); } return 0; }
相关文章推荐
- POJ 1703 1182 2492 一类并查集题目-(类别偏移量)
- poj 2492 A Bug's Life (并查集同食物链)
- 并查集初学(4)向量思维 POJ1182(食物链)&& POJ1703
- POJ A Bug's Life POJ 2492(和食物链差不多的一道题)
- hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them
- poj--1703/2492 (经典并查集)
- poj 1703 poj 2492 并查集 桥梁判AB组
- POJ 1703 1182 2492 并查集 偏移
- POJ 1703 Find them, Catch them 第一次做关系并查集....真真是入门神题
- POJ 1703 - 并查集..注意细节..
- 带权并查集 向量并查集 poj1182 食物链
- POJ 1703 Find them, Catch them 二分图 并查集
- POJ 1703 Find them, Catch them【并查集】
- POJ - 1182 食物链(并查集)
- poj 1182 食物链 && nyoj 207(种类并查集)
- POJ 1703 Find them, Catch them 种类并查集
- poj2492 A Bug's Life【基础种类并查集】
- poj 1703 Find them, Catch them(种类并查集)
- HDU1829&POJ 2492 a bug's life [并查集||二分图染色]
- poj 1182 食物链(种类并查集)