Program4_S

来源:互联网 发布:java中的垃圾回收机制 编辑:程序博客网 时间:2024/06/05 07:19

 我现在做的是第四专题编号为1019的试题,具体内容没如下所示:

Problem S

Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 19   Accepted Submission(s) : 7
Problem Description
<b>Background </b><br>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. <br><br><b>Problem </b><br>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.<br>
 

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!

简单题意:

给定一系列数对,例如a和b,表示a和bb不是同一种性别,然后不断的给出这样的数对,问有没有性别不对的情况。

解题思路:

很明显的分组并查集的题目,可以考虑用两种方法来做: (本文作者:CSDN:凌风)
1.开两个并查集,然后合并的时候要合并两次,这样在合并之前判断是否冲突,如果不冲突就进行合并,否则不需要继续合并。
2.开一个并查集,但是要加个偏移向量数组,来记录每个节点距离根节点的距离,但这里最好取一下余,因为毕竟只有两个分组。
在这里我用两种方法分别实现了一下

编写代码:

    #include <cstdio>
    #include <cstdlib>
    #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;
        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(){
        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;
    }



0 0
原创粉丝点击