Poj1703
题意:Tadu City里面有两个黑帮团伙Gang Dragon和Gang Snake,一共有n名团伙成员(还不知道属于这两个黑帮的哪一个)。现在警察局有一些信息,每条信息包含2个人编号,表示这2个人属于不同的帮派。问给你2个人的编号,能否确定他们是否属于同一个帮派。
思路:并查集的应用。关键:用opp[x]表示一个与x处于不同帮派的人的编号,只需要1个就够了,因为只需要这么1个编号就能找出它的集合代表,若y的集合代表与之相同,则x与y为不同帮派。
Find them, Catch them
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 48627 | Accepted: 14965 |
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
1 5 5 A 1 2 D 1 2 A 1 2 D 2 4 A 1 4
Sample Output
Not sure yet. In different gangs. In the same gang.
网上说: 如果用cin的话会TLE,浪费我不少时间试图优化算法。我还是第一次遇到会让cin TLE的数据量。换用scanf就轻松AC:
这题真的很简单,是食物链的弱化版,使用相似的思路来做即可——
定义并查集为:
并查集里的元素i-x表示i属于帮派x
同一个并查集的元素同时成立
可见所有元素个数为2 * N,如果i表示属于帮派A,那么i + N表示属于帮派B,每次输入两个家伙不在同一帮派的时候,就合并他们分属两个帮派的元素。我认为这是最简单最好懂的算法,那些利用复杂节点带权重接着依靠大量if-else维护并查集的做法都不够美
//#include <bits/stdc++.h>
#include <cstring>
#include <stdio.h>
#include <iostream>
using namespace std;
const int MAX_N=100000;
int father[MAX_N];
int ran[MAX_N];
char question1[100000];
int question2[100000];
int question3[100000];
int N,M; //N个罪犯,M个询问
void init(int n){
memset(father,-1,sizeof(father));
for(int i=0;i<=n;i++){
father[i]=i;
ran[i] = 0;
}
}
int find(int x){
if( father[x]==x ) {
return x;
}
else{
int t = father[x];
return father[x] = find(t);
}
}
bool same(int x ,int y){
return ( find(x)== find(y));
}
void request(int x,int y){
if( same(x , y)) cout << "In the same gang."<<endl;
else if( same(x , y+N) ) cout << "In different gangs."<<endl;
else cout << "Not sure yet."<<endl;
}
void unite(int x,int y){
int f1 = find(x);
int f2 = find(y);
if(f1!=f2){
if( ran[f1] < ran[f2]) {
father[f1]= f2 ;
}
else{
father[f2]= f1;
if( ran[f1] == ran[f2] ) ran[f1] ++; //ÕâÀïдµÄ²»´í
}
}
}
int main()
{
int T;
cin >> T;
for(int t=1;t<=T;t++){
cin >> N >> M;
init(N*2);
for(int i=0;i<M;i++){
// char a;
// int b,c;
// scanf("%c", &a);
// scanf("%d", &b);
// scanf("%d", &c);
// question1[i]=a; question2[i]=b; question3[i]=c;
cin >> question1[i]>> question2[i]>>question3[i];
}
for(int i=0;i<M;i++){
if(question1[i]=='D'){
unite( question2[i] , question3[i] + N );
unite( question3[i] , question2[i] + N );
}
if(question1[i]=='A'){
request( question2[i] ,question3[i]);
}
}
}
return 0;
}
样例通过了,poj没有通过。超时了!