POJ - 1703 Find them, Catch them (并查集2)
来源:互联网 发布:淘宝直通车调价技巧 编辑:程序博客网 时间:2024/06/15 13:06
Find them, Catch them
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 27836 Accepted: 8486
Description
The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is, given two criminals; do they belong to a same clan? You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.)
Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:
1. D [a] [b]
where [a] and [b] are the numbers of two criminals, and they belong to different gangs.
2. A [a] [b]
where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.
Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:
1. D [a] [b]
where [a] and [b] are the numbers of two criminals, and they belong to different gangs.
2. A [a] [b]
where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.
Input
The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case begins with a line with two integers N and M, followed by M lines each containing one message as described above.
Output
For each message "A [a] [b]" in each case, your program should give the judgment based on the information got before. The answers might be one of "In the same gang.", "In different gangs." and "Not sure yet."
Sample Input
15 5A 1 2D 1 2A 1 2D 2 4A 1 4
Sample Output
Not sure yet.In different gangs.In the same gang.
Source
POJ Monthly--2004.07.18
——————————————————————————————————————————————
方法1:
将10^5看成10000……Runtime Error 一次……
这次并查集的对象是事件,而不仅仅是单一元素。在同一集合里表示该事件同时发生或同时不发生
D(x, y): //分别合并两种集合,即(x在势力1,y在势力2)或(x在势力2, y在势力1)
unite(x, y+N);unite(x+N, y);
A(x, y):
same(x, y+N) || same(x+N, y)表示: x在势力1,y在势力2 || x在势力2, y在势力1 。即x,y在不同势力
same(x, y) || same(x+N, y+N)表示: x,y都在势力1 || x,y都在势力2。 即x,y在相同势力
same(x, y+N)表示在不同的势力same(x, y)表示在相同的势力
其他情况为 Not Sure.
#include <iostream>#include <cstdio>using namespace std;#define MAX 100010int cas, N, M;char sans[3][30] = {"In different gangs.\n","In the same gang.\n", "Not sure yet.\n"};//并查集int pa[MAX*2], rk[MAX*2];void init(int n) {for(int i = 0; i < n; i++){pa[i] = i; rk[i] = 0;}}int find(int x) { return pa[x] == x ? x : pa[x] = find(pa[x]); }void unite(int x, int y){x = find(x), y = find(y);if(x == y)return ;if(rk[x] < rk[y])pa[x] = y;else{pa[y] = x;if(rk[x] == rk[y])rk[x]++;}}int same(int x, int y){ return find(x) == find(y); }int main(){//freopen("in.txt","r",stdin); scanf("%d", &cas);while(cas--){scanf("%d %d", &N, &M);init(2*N);for(int i = 0; i < M; i++){char s[10];int x, y;scanf("%s%d%d", s, &x, &y);x--, y--;if(*s == 'A'){int ans = 2;if(same(x, y+N) /*|| same(x+N, y)*/)ans = 0;else if(same(x, y) /*|| same(x+N, y+N)*/) ans = 1;printf(sans[ans]);}else{unite(x, y+N);unite(x+N, y);}}}}
————————————————————————————————————————————————————
方法2:
A查询:先找出他们的老大:1、在同一集合,势力相同。 2、x的老大是y老大的对立面,不同势力。3、未确定。
D操作:若尚无敌对势力则记录,然后各自合并入敌对势力。
#include <iostream>#include <cstdio>using namespace std;#define MAX 100010int cas, N, M;int opp[MAX];char sans[3][30] = {"In different gangs.\n","In the same gang.\n", "Not sure yet.\n"};//并查集int pa[MAX*2], rk[MAX*2];void init(int n) {for(int i = 0; i < n; i++){pa[i] = i; rk[i] = 0; opp[i] = -1;}}int find(int x) { if(x==-1)return -1; return pa[x] == x ? x : pa[x] = find(pa[x]); }void unite(int x, int y){x = find(x), y = find(y);if(x == y)return ;if(rk[x] < rk[y])pa[x] = y;else{pa[y] = x;if(rk[x] == rk[y])rk[x]++;}}int same(int x, int y){ return find(x) == find(y); }int main(){//freopen("in.txt","r",stdin); scanf("%d", &cas);while(cas--){scanf("%d %d", &N, &M);init(N);for(int i = 0; i < M; i++){char s[10];int x, y;scanf("%s%d%d", s, &x, &y);x--, y--;int x1 = find(x), y1 = find(y);//先找各势力的老大(父节点)if(*s == 'A'){int ans = 2;if(x1 == find(opp[y1]))ans = 0;else if(x1 == y1) ans = 1;printf(sans[ans]);}else{//若尚无敌对势力则记录。if(opp[x] == -1)opp[x] = y;if(opp[y] == -1)opp[y] = x;//与敌对势力合并unite(x, opp[y1]);unite(y, opp[x1]);}}}}
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 (并查集)
- Find them, Catch them.(POJ-1703)(并查集)
- 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 并查集
- Linux 上设置共享(学习整理)
- linux里的CPU负载
- HttpRequest使用POST/GET方法
- Struts2保存登陆信息和注销功能例子
- 学习Datatable增删改
- POJ - 1703 Find them, Catch them (并查集2)
- 拆机&清灰&重装系统&数据恢复——心得汇总
- AIX内存查看命令
- VS2008编译boost库
- POJ 2582 Mayor's posters 线段树入门题+离散化
- android 获取SD、ROM容量
- 关于如何实现“拒绝从网络访问这台计算机”中添加guest的办法
- 几个有用内核函数封装(进程、注册表路径)
- jsp运行原理