A Bug‘s Life
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 28882 | Accepted: 9423 |
题目链接:http://poj.org/problem?id=2492
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.
解题思路:
题目大意抽象为n个点m个操作,每次操作两个对象a和b,代表a和b可以进行交配,那么根据professer的假设,a和b一定是异性。如果是同性的话,那么就会出现同性恋,则professer的假设不成立,输出Suspicious bugs found!;成立输出No suspicious bugs found!。
并查集的模板题,不过与以往做的不同的是,要开一个rea数组记录a和b之间的关系。rea[ i ]存储 i 的异性,具体见代码。
最后注意下每次后面都要有一个空行,描述里没说,但是样例里是那么显示的。
完整代码:
#include <functional> #include <algorithm> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <numeric> #include <cstring> #include <climits> #include <cassert> #include <complex> #include <cstdio> #include <string> #include <vector> #include <bitset> #include <queue> #include <stack> #include <cmath> #include <ctime> #include <list> #include <set> #include <map> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") typedef long long LL; typedef double DB; typedef unsigned uint; typedef unsigned long long uLL; /** Constant List .. **/ //{ const int MOD = int(1e9)+7; const int INF = 0x3f3f3f3f; const LL INFF = 0x3f3f3f3f3f3f3f3fLL; const DB EPS = 1e-9; const DB OO = 1e20; const DB PI = acos(-1.0); //M_PI; const int maxn = 1100000; int n , m; int f[maxn]; int rea[maxn]; void init() { for(int i = 0 ; i < maxn ; i++) { f[i] = i; rea[i] = 0; } } int find(int x) { return x == f[x] ? x : find(f[x]); } void unin(int a , int b) { int x = find(a); int y = find(b); if(x != y) f[x] = y; } int main() { #ifdef DoubleQ freopen("in.txt","r",stdin); #endif int T; scanf("%d",&T); int cas = 1; while(T--) { scanf("%d%d",&n,&m); init(); int a , b; int flag = 0; for(int i = 0 ; i < m ; i ++) { scanf("%d%d" , &a , &b); int x = find(a); int y = find(b); if(x == y) flag = 1; if(rea[x] == 0 && rea[y] == 0) // x和y没有建立性别关系 { rea[x] = y; // x的异性是y rea[y] = x; // y的异性是x } else if(rea[x] == 0) // y建立起了性别关系,x没建立 { rea[x] = y; // x的异性是y unin(x , rea[y]); // 根据假设,那么x一定应该和y的原有异性进行合并,下面同理 } else if(rea[y] == 0) { rea[y] = x; unin(y , rea[x]); } else { unin(x , rea[y]); unin(y , rea[x]); } } printf("Scenario #%d:\n" , cas++); if(flag) printf("Suspicious bugs found!\n"); else printf("No suspicious bugs found!\n"); printf("\n"); } }