A Bug's Life
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
|
————————————————————水水的分割线————————————————————
思路:水体也贴!这是关系性并查集的水题。之前做过最经典的食物链。要想学关系性并查集,还是先学这个,学会了就可以秒很多题了。思路可以看上面的博客。
此外值得一说,这道题根本就没说每组输出后加一个空行,尼玛我就不吐槽了。
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;
/****************************************/
const int N = 2005;
const int r_sex[2][2] = {1, 0, 0, 1}, r_gra[2][2] = {0, 1, 1, 0};
int pa[N], relat[N];
int ok;
//0表示同性 1表示异性
int Find(int x)
{
int tmp;
if(x != pa[x]) {
tmp = pa[x];
pa[x] = Find(tmp);
relat[x] = r_gra[relat[x]][relat[tmp]];//根据和父亲的关系、父亲和爷爷的关系,压缩一个父亲,更新关系域
}
return pa[x];
}
void Union(int x, int y)
{
int fx = Find(x), fy = Find(y);
if(fx != fy) {
pa[fy] = fx;
relat[fy] = r_sex[relat[x]][relat[y]];//根据A和祖先的关系、B和祖先的关系、A和B异性、推出B和A的祖先关系,并以A的祖先作为B的新祖先
return ;
}
if((relat[x] + 1) % 2 != relat[y])//如果A、B在同一个集合,直接逻辑判断
ok = 0;
}
int main()
{
int cas;
scanf("%d", &cas);
for(int t = 1; t <= cas; t++) {
int n, m, x, y;
ok = 1;
scanf("%d%d", &n, &m);
for(int i = 0; i <= n; i++) {
pa[i] = i;
relat[i] = 0;
}
for(int i = 0; i < m; i++) {
scanf("%d%d", &x, &y);
Union(x, y);
}
printf("Scenario #%d:\n", t);
if(ok)
printf("No suspicious bugs found!\n\n");
else
printf("Suspicious bugs found!\n\n");
}
return 0;
}