[NWPU][2014][TRN][12]并查集D - A Bug's Life POJ 2492

来源:互联网 发布:seo h1标签 编辑:程序博客网 时间:2024/06/05 09:58
D - A Bug's Life
Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 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

23 31 22 31 34 21 23 4

Sample Output

Scenario #1:Suspicious bugs found!Scenario #2:No suspicious bugs found!

Hint

Huge input,scanf is recommended.

#include <iostream>#include<cstdio>using namespace std;const int MAX_N = 2005;int par[MAX_N];//父亲int rank[MAX_N];//树的高度//初始化n个元素void init(int n){    for(int i = 0; i < n; i++)    {        par[i] = i;        rank[i] = 0;    }}//查询树的根int find(int x){    if(par[x] == x)        return x;    else        return par[x] = find(par[x]);}//合并x和y所属的集合void unite(int x, int y){    x = find(x);    y = find(y);    if(x == y)        return ;    if(rank[x] < rank[y] )        par[x] = y;    else    {        par[y] = x;        if(rank[x] == rank[y])            rank[x]++;    }}//判断x和y是否属于同一个集合bool same(int x, int y){    return find(x) == find(y);}int main(){    //freopen("input.txt", "r", stdin);    int cas, n, k, ans = 0;    //cin >> cas;    scanf("%d", &cas);    for(int i = 1; i <= cas; i++)    {        ans = 0;        //cin >> n >> k;        scanf("%d%d", &n, &k);        init(n  * 4);        while(k--)        {            int a, b;            //cin >> a >> b;            scanf("%d%d", &a, &b);            a -= 1; b -= 1;            if(same(a, b))            {                ans++;            }            else            {                unite(a, b + n);                unite(a + n, b);     //此处为啥要这样写?不懂            }        }        if(ans == 0)        //cout << "Scenario #" << i << ":\n" << "No suspicious bugs found!" << endl;        //else cout << "Scenario #" << i << ":\n" << "Suspicious bugs found!" << endl;        printf("Scenario #%d:\nNo suspicious bugs found!\n\n", i);        else printf("Scenario #%d:\nSuspicious bugs found!\n\n", i);    }    return 0;}


0 0
原创粉丝点击