Find them, Catch them(并查集)
Problem 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:
-
D [a] [b]
where [a] and [b] are the numbers of two criminals, and they belong to different gangs. -
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.
一开始,我使用的是2个数组来解决这个问题,思路很简单,代码量也不长,但是,时间会超限,所以,本题还是得用并查集来快速解决,先贴出时间超限的代码。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
int n, m;
char a;
int a1[100005], a2[100005];
memset(a1, -1, sizeof(a1));
memset(a2, -1, sizeof(a2));
int b1, b2;
b1 = b2 = 0;
scanf("%d%d", &n,&m);
while (m--)
{
int x, y;
int flag = -1, flog = -1;
cin >> a;
if (a == 'D')
{
scanf("%d%d", &x, &y);
for (int i = 0; i < n; i++)
{
if (a1[i] == x)
flag = 0;
if (a1[i] == y)
flog = 0;
if (a2[i] == x)
flag = 1;
if (a2[i] == y)
flog = 1;
}
if (flag == -1 || flog == -1)
{
a1[b1++] = x;
a2[b2++] = y;
}
if (flag == 0)
a2[b2++] = y;
if (flag == 1)
a1[b1++] = y;
if (flog == 0)
a2[b2++] = x;
if (flog == 1)
a1[b1++] = x;
}
if (a == 'A')
{
scanf("%d%d", &x,&y);
for (int i = 0; i < n; i++)
{
if (a1[i] == x)
flag = 0;
if (a1[i] == y)
flog = 0;
if (a2[i] == x)
flag = 1;
if (a2[i] == y)
flog = 1;
}
if (flag == -1 || flog == -1)
printf("Not sure yet.\n");
else
if (flag == flog)
printf("In the same gang.\n");
else
printf("In different gangs.\n");
}
}
}
return 0;
}
思路来源:https://blog.csdn.net/qq_38570571/article/details/81052388
有个很严重的问题,如果使用cin读取,本题目会TEL。只能老老实实的使用scanf存值。
先贴出使用cin超时代码,再贴出scanf代码。
#include<iostream>
#include<cstdio>
using namespace std;
int f[200005];
int find(int x)
{
if (f[x] == x)
return x;
return(f[x] = find(f[x]));
}
void bing(int x, int y)
{
int f1 = find(x);
int f2 = find(y);
if (f1 != f2)
f[f1] = f2;
}
int main()
{
ios::sync_with_stdio(false);
int T;
cin >> T;
while (T--)
{
int n, m;
cin >> n >> m;
for (int i = 0; i < 2 * n; i++)
f[i] = i;
while (m--)
{
char c;
int x, y;
cin >> c >> x >> y;
if (c == 'A')
{
if (find(x) == find(y + n) && find(x + n) == find(y))
printf("In different gangs.\n");
else
if (find(x) == find(y))
printf("In the same gang.\n");
else
printf("Not sure yet.\n");
}
if (c == 'D')
{
bing(x, y + n);
bing(x + n, y);
}
}
}
}
AC代码。思路:2人要么是朋友,要么是敌人,前半段n和后半段n,分别储存对于x和y的朋友和敌人。若x,y的老大是同一人,代表他们是朋友,代码中来看就是头节点相同。若x与y+n和x+n与y相同,则代表他们是敌人,因为2人的敌人相同。其他情况,一律判断为不确定。
ps,一定要注意读取数据时,使用scanf而不是cin,否则一直是TLE。
#include<iostream>
#include<cstdio>
using namespace std;
int f[200005];
int find(int x)
{
if (f[x] == x)
return x;
return(f[x] = find(f[x]));
}
void bing(int x, int y)
{
int f1 = find(x);
int f2 = find(y);
if (f1 != f2)
f[f1] = f2;
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i <=2 * n; i++)
f[i] = i;
getchar();
while (m--)
{
char c;
int x, y;
scanf("%c %d %d", &c, &x, &y);
getchar();
if (c == 'A')
{
if (find(x) == find(y + n) && find(x + n) == find(y))
printf("In different gangs.\n");
else
if (find(x) == find(y))
printf("In the same gang.\n");
else
printf("Not sure yet.\n");
}
if (c == 'D')
{
bing(x, y + n);
bing(x + n, y);
}
}
}
}