A Bug ' s life
Time limit:15000/5000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 7507 Accepted Submission (s): 2417
Problem Description
Background
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature, different genders and that they only interact with bugs of the opposite gender. In he experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their BAC Ks.
problem
Given A list of bugs interactions, decide whether the experiment supports his assumption of both 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) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction was given in the form of both distinct bug numbers separated by a single space. Bugs is numbered consecutively starting from one.
Output the output for every scenario are a line containing "scenario #i:", where I am the number of the scenario starting a T 1, followed by one line saying either "No suspicious bugs found!" if the experiment are consistent with his assumption AB 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.
Given a series of pairs, such as a and B, indicating that A and B are not the same sex, and then constantly give such a number of pairs, asking whether there is a situation of gender wrong. For example, given: 1 2 3 4 1 3 So here is 1 and 2 are not the same sex, 3 and 4 are not the same sex, 1 and 3 are not the same sex, that is to say 1 and 3 are the same sex, 2 and 4 are the same sex, so there is no ambiguity, this time output no suspicious bugs found!. But for example, (this author: CSDN: Lingfeng) 1 2 2 3 1 3 1 and 2 different genders, 2 and 3 different genders, then 1 and 3 were of the same, but the third group of pairs also showed 1 and 3 different genders, so there was a discrepancy in the gender of 3 or 1, that is, the condition is contradictory, This time output suspicious bugs found!.
It is obvious that the group and the topic of the collection can be considered in two ways: (The author: CSDN: Lingfeng) 1. Open Two and check the set, and then merge the time to merge two times, so that before the merger to determine whether the conflict, if not conflict to merge, otherwise do not need to continue to merge. 2. Open a and check the set, but to add an offset vector array, to record each node distance from the root node, but it is best to take the remainder, because after all, only two groups. Here I have two ways to achieve the following:
The implementation of method one:
#include <cstdio> #include <cstdlib> #include <climits> #include <cstring> #include <
Algorithm> using namespace std;
const int MAX = 2000;
int pre[2*max+5];
BOOL Mark;
void init (int n) {int i;
(author:csdn: Lingfeng) for (i=1;i<=max+n;++i) pre[i] = i;
Mark = true;
} int root (int x) {if (x!=pre[x]) {pre[x] = root (Pre[x]);
} return pre[x];
} void Merge (int x,int y) {int fx,fy;
FX = root (x);
FY = root (Y-max);
if (fx==fy) {mark = false;
Return
} fy = root (y);
if (fx!=fy) {PRE[FX] = Pre[fy];
}} int main () {//freopen ("In.txt", "R", stdin);
(author:csdn: Lingfeng) int t,i,n,m,x,y,k;
scanf ("%d", &t);
for (i=1;i<=t;++i) {scanf ("%d%d", &n,&m);
Init (n);
for (k=1;k<=m;++k) {scanf ("%d%d", &x,&y);
if (Mark) {merge (X,y+max);
Merge (Y,x+max);
}} printf ("Scenario #%d:\n", i);
if (Mark) {printf ("No suspicious Bugs found!\n");
}else{printf ("Suspicious bugs found!\n");
} printf ("\ n"); } return 0;
}
Code for method Two:
#include <cstdio> #include <cstdlib> #include <climits> #include <cstring> #include <
Algorithm> using namespace std;
const int MAX = 2000;
int pre[max+5];
int offset[max+5];
BOOL Mark;
void init (int n) {int i;
(author:csdn: Lingfeng) for (i=1;i<=n;++i) {pre[i] = i;
Offset[i] = 0;
} mark = true;
} int root (int x) {int px;
if (X!=pre[x]) {px = pre[x];
Pre[x] = root (Pre[x]);
OFFSET[X] = (OFFSET[PX] + offset[x])%2;
} return pre[x];
} void Merge (int x,int y) {int fx = root (x);
int fy = root (y);
if (fx!=fy) {pre[fx] = FY;
OFFSET[FX] = (1 + offset[y]-offset[x])%2;
} root (x);
} int main () {//freopen ("In.txt", "R", stdin);
(author:csdn: Lingfeng) int t,i,n,m,x,y,k;
scanf ("%d", &t);
for (i=1;i<=t;++i) {scanf ("%d%d", &n,&m);
Init (n);
for (k=1;k<=m;++k) {scanf ("%d%d", &x,&y);
if (Mark) {merge (x, y);
} if (Offset[x]==offset[y]) {mark = false;
}} printf ("Scenario #%d:\n", i); if (Mark) {printf ("No suspicious Bugs found!\n");
}else{printf ("Suspicious bugs found!\n");
} printf ("\ n");
} return 0; }