Link:
http://acm.hdu.edu.cn/showproblem.php?pid=1829
Original title:
Problem Description
Background
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes this they feature two different genders and that they only interact with bugs of the opposite. In he experiment, individual bugs and their interactions were easy to identify, because numbers-were on printed BAC Ks.
Problem
Given A list of bugs 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 ' The ' input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (in least one, and up to) and the number of interactions (up to 1000000) separated by a. In the following lines, the interaction are given in the form of two distinct bugs numbers by a. Bugs are numbered consecutively starting from one.
Output
The output for every scenario was a line containing "scenario #i:", where I was the number of the scenario starting at 1, fo Llowed by one line saying either "No suspicious bugs found!" if the experiment was 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.
Analysis and Summary:
Did this problem to find himself again to the understanding of the collection is really just some fur, and check the collection of a lot of advanced applications I do not understand, and this problem is only the kind and search the simplest = =| | |.
Suppose there
1,2
2,3
That is, 1--2--3, obviously adjacent to the two can not be the same sex, if the adjacent two are same-sex, then the explanation may be homosexual existence.
This column more highlights: http://www.bianceng.cn/Programming/sjjg/
The above assumes that 1 is a male, instead of false, the order is false, true, False
Let's say we add a relationship 3, 1.
So since 1 and 3 already have values, all are false, which means there may be homosexuality.
The key to the species and the collection is the distance from the node to the root node, if the distance is odd then the gender is the opposite of the node, if the even number is the same as the node's gender.
Code:
#include <cstdio> #define N-using namespace std;
int F[n],rank[n], N, K;
BOOL Flag;
inline void init () {flag=false;
for (int i=0; i<=n; ++i) f[i]=i, rank[i]=0;
int find (int x) {if (x==f[x]) return f[x];
int T=find (f[x]);
RANK[X] = (rank[f[x]]+rank[x]) &1;
f[x]=t;
return f[x];
} void Union (int x, int y) {int a=find (x), B=find (y);
if (a==b) {if (rank[x]==rank[y]) flag=true;
Return
} f[a]=b;
Rank[a] = (rank[x]+rank[y]+1) &1;
int main () {int t,a,b,cas=1;
scanf ("%d", &t);
while (t--) {scanf ("%d%d", &n,&k);
Init ();
for (int i=0; i<k; ++i) {scanf ("%d%d", &a,&b);
if (flag) continue;
Union (A,B);
printf ("Scenario #%d:\n", cas++);
if (flag) printf ("Suspicious bugs found!\n"); Else priNTF ("No suspicious Bugs found!\n");
printf ("\ n");
return 0; }