A Bug's Life
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 28121 | Accepted: 9142 |
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.
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! 傳送門:點擊打開鏈接 大致題意: 解釋一下第一個樣例吧: 3,3表示3個數(1,2,3),接下來3行表示每兩個數之間的關係; 1 2表示1,2是戀愛關係(就當成這個意思理解吧); 2 3表示2,3是戀愛關係; 1 3表示1,3是戀愛關係; 所以這3個人中存在同性戀,輸出“Suspicious bugs found!”; 對於不存在同性戀的情況輸出“No suspicious bugs found!”; 現在,題目就是要求我們判斷是否存在同性戀。 解題思路: 分組並查集(種類並查集)。我們可以這樣思考a b存在戀愛關係,表示他們是異性,之間相互矛盾,用集合A表示與a是相同性別 的元素,集合B表示與a是不同性別的元素,這樣的話a放在A集合,a+n(表示與a相異的性別)放在B集合,b放在B集合,b+n放在 A集合。如果在統計時發現,a和a+n或是b和b+n在相同的集合裏面就是出現了矛盾的情況,即存在同性戀,反之,不存在。 代碼:
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 2010;
int set[MAXN<<1];
int find(int p)
{
if(set[p] < 0) return p;
return set[p] = find(set[p]);
}
void join(int p, int q)
{
p = find(p); q = find(q);
if(p != q) set[p] = q;
}
int main()
{
int t, m, n, w = 1;
scanf("%d", &t);
while(t--)
{
memset(set, -1, sizeof(set));
scanf("%d%d" , &n, &m);
bool flag = false;//沒有矛盾情況
while(m--)
{
int a, b;
scanf("%d%d", &a, &b);
join(a, b+n);
join(b, a+n);
if(find(a)==find(a+n) || find(b)==find(b+n))
flag = true;
}
if(1 != w) printf("\n");
printf("Scenario #%d:\n", w++);
printf("%s\n", flag ? "Suspicious bugs found!" : "No suspicious bugs found!");
}
return 0;
}