A Bug's Life
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 24837 | Accepted: 8087 |
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!
Hint
Huge input,scanf is recommended.
Source
TUD Programming Contest 2005, Darmstadt, Germany
并查集,看看在同一集合中的虫子是否同性。
我发现了某位同学的解题报告,顿时被他猥琐至极的行文风格震惊了。。。
娱乐过后,贴出我的代码:
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 2000 + 50;
const int maxw = 100 + 20;
const int MAXNNODE = 1000000 +10;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
#define mod 1000000007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
int n , p[MAXN] , r[MAXN];///0代表r[i]与i同性, 1代表i与r[i]异性
int find_fa(int x)
{
int temp = p[x];
if(x == p[x])return x;
p[x] = find_fa(p[x]);
r[x] = r[temp] == 0 ? r[x] : 1 - r[x];
return p[x];
}
void init()
{
FORR(i , 0 , n)
{
p[i] = i;
r[i] = 0;
}
}
int Union(int x , int y , int rx , int ry)
{
p[ry] = p[rx] ;
r[ry] = r[x] == r[y] ? 1 : 0;
}
int main()
{
//ios::sync_with_stdio(false);
#ifdef Online_Judge
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // Online_Judge
int i , j , m , a , b , rx , ry , flag , cnt = 1 , t;
scanf("%d" , &t);
while(t--)
{
flag = 0;
scanf("%d%d" , &n , &m);
init();
while(m--)
{
scanf("\n%d %d" , &a ,&b);
rx = find_fa(a) , ry = find_fa(b);
if(rx == ry && r[a] == r[b])flag = 1;///在同一个集合且性别相同
else Union(a , b , rx ,ry);
}
if(flag==1)
printf("Scenario #%d:\nSuspicious bugs found!\n",cnt++);
else
printf("Scenario #%d:\nNo suspicious bugs found!\n",cnt++);
printf("\n");
}
return 0;
}