Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 34687 Accepted: 11344
Description
Background
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs.
Problem
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.
Input
The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.
Output
The output for every scenario is a line containing “Scenario #i:”, where i is the number of the scenario starting at 1, followed by one line saying either “No suspicious bugs found!” if the experiment is consistent with his assumption about the bugs’ sexual behavior, or “Suspicious bugs found!” if Professor Hopper’s assumption is definitely wrong.
Sample Input
2
3 3
1 2
2 3
1 3
4 2
1 2
3 4
Sample Output
Scenario #1:
Suspicious bugs found!
Scenario #2:
No suspicious bugs found!
Hint
Huge input,scanf is recommended.
Source
TUD Programming Contest 2005, Darmstadt, Germany
【题解】
每个测试点之间要多输出一行空行。
因为有多组数据,所以不能看到有抵触关系就break掉。而应该继续读完这组数据。不然可能我们输入的东西还是上组数据的。。
相当于给你m句话。
x和y不是同一性别。
问有没有假话。
在并查集里面加一维r代表该节点和父亲节点的关系。
如果为0表示性别相同。为1表示性别不同。
可以得到以下两个带权向量关系
t就是r[x]+r[y];
t再%2;
然后路径压缩的时候就可以靠这个来获取儿子和爷爷的关系了。
然后是合并两个集合。
设x的根节点为a,y的根节点为b;
此时并查集的高度为2(经过路径压缩);
我们执行的时候是执行f[b] = a;
即
x和y的关系知道。但是b和a的关系不知道啊;
我们仍可用向量关系退出a和b的关系;
可以看到1+r[x] == t+r[y];
那么t = 1+[rx]-r[y];
t要再%2;
这样就求出a和b的关系t了;
最后是判断一句话是否为真的方法;
如果f[x]==f[y]则判断x和y是不是异性;
用向量的运算,可以得到x和y的关系就是r[x]-r[y]
这里r[x]-r[y]可能会小于0,所以加上2再%2;
#include <cstdio>
const int MAXN = 2500;
int n, m;
int f[MAXN], relation[MAXN];
void input(int &r)
{
char t;
t = getchar();
while (t<'0' || t>'9')
t = getchar();
r = 0;
while (t >= '0' && t <= '9')
{
r = r * 10 + t - '0';
t = getchar();
}
}
int findfather(int x)
{
if (f[x] == x)
return x;
int olfa = f[x];
f[x] = findfather(f[x]);
relation[x] = (relation[x] + relation[olfa]) % 2;
return f[x];
}
int main()
{
//freopen("F:\rush.txt", "r", stdin);
int T;
input(T);
for (int TT = 1; TT <= T; TT++)
{
input(n); input(m);
for (int i = 1; i <= n; i++)
f[i] = i, relation[i] = 0;
bool wrong = false;
for (int i = 1; i <= m; i++)
{
int x, y;
input(x); input(y);
if (wrong)
continue;
int a = findfather(x), b = findfather(y);
if (a != b)
{
f[b] = a;
relation[b] = (1 + relation[x] - relation[y])%2;
}
else
{
int temp = (relation[x] - relation[y] + 2) % 2;
if (temp != 1)
wrong = true;
}
}
printf("Scenario #%d:
", TT);
if (wrong)
printf("Suspicious bugs found!
");
else
printf("No suspicious bugs found!
");
}
return 0;
}