hdu1829——A Bug's Life

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.

带权并查集特别是这种种类并查集还是不怎么看的懂….

代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=2005;
int p[N];
//与根节点的关系 0异性 1同性
int re[N];
int n,k;
bool flag;
void init(){
    flag=false;
    for(int i=0;i<=n;i++){
        p[i]=i;
        re[i]=0;
    }
}
int find(int x){
    if(x==p[x]){
        return x;
    }
    //递归找到根节点,(上面的re关系数组也已经更新)
    int t=find(p[x]);
    printf("递归到%d %d\n",x,t);
    //这里这个p[x]还没更新的的
    //比如 -4 递归更新的时候,1的父节点2已经接上了4,
    // - 3 但是返回到最上面一层的时候,p[1]仍然是2
    // -2
    // 1
    printf("更新re%d %d\n",re[x],re[p[x]]);
    re[x]=(re[x]+re[p[x]])%2;
    printf("更新完毕%d\n",re[x]);
    printf("%d\n",re[x]);
    //压缩路径
    return p[x]=t;
}
void join(int a,int b){
    int fa=find(a);
    int fb=find(b);
    printf("%d--%d\n",fa,fb);
    printf("%d %d %d\n",re[1],re[2],re[3]);
    if(fa==fb){
        //出现同性矛盾
        if(re[a]==re[b]){
            flag=1;
        }
    }
    else{
        printf("unit\n");
        p[fa]=fb;
        printf("%d %d\n",re[a],re[b]);
        //第一次合并时,本来两个都是0,表示到根节点(本身)为同性,以fb为根合并后,就应该将re[fa]更新为1
        //因为这两个是异性了
        //re[fa]=(re[a]+re[b]+1)%2;
        //异或 再0取1 1取0
        re[fa]=!(re[a]^re[b]);
        printf("%d %d %d\n",re[fa],re[a],re[b]);
    }
}
int main(void){
    freopen("data.txt","r",stdin);
    int t,a,b;
    int c=1;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&k);
        init();
        while(k--){
            scanf("%d%d",&a,&b);
            if(flag){
                continue;
            }
            join(a,b);
        }
        printf("Scenario #%d:\n",c++);
        if(flag){
             printf("Suspicious bugs found!\n");
         }
        else{
             printf("No suspicious bugs found!\n");
         }
        printf("\n");
    }
    return 0;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-02 17:28
25届每天都在焦虑找工作的事情0offer情绪一直很低落硬撑着面了一个岗位岗位有应酬的成分面试的时候hr给我出各种场景题问的问题比较犀利&nbsp;有点压力面的感觉感觉有点回答不上来本来就压抑的情绪瞬间爆发了呢一瞬间特别想哭觉得自己特别没用没绷住掉眼泪了事后想想觉得自己挺有病的&nbsp;真的破大防了
喜欢唱跳rap小刺猬...:我觉得没关系吧,之前有一次面试leader给我压力面,我顶住了压力,结果入职的时候发现组里氛围很差,果断跑路。其实从面试就能大概看出组的情况,面试体验好的组倒是不一定好,但是面试体验不好的组。。。就很难说
面试尴尬现场
点赞 评论 收藏
分享
酷酷我灵儿帅:这去不去和线不线下面说实话没啥关系
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务