码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1829

时间:2018-06-19 16:17:53      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:输出   col   并查集   rip   port   target   ssi   sea   about   

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1829

A Bug‘s Life

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18563    Accepted Submission(s): 5931


Problem 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
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.
 

 

Source
 ps:
第一次写种类并查集,但不是很懂,但还是先把ac代码贴出来吧
很伤啊,有点看不懂呃(模仿的别人的,但写代码不就是在模仿中学习吗?)
注意:
输出有换行
#include<bits/stdc++.h>
using namespace std;
#define max_v 2005
int pa[max_v];
int re[max_v];
int flag;
void make_set(int x)
{
    pa[x]=x;
    re[x]=0;
}
int find_set(int x)
{
    if(x==pa[x])
        return pa[x];
    int t=find_set(pa[x]);
    re[x]=(re[pa[x]]+re[x])%2;
    pa[x]=t;
    return pa[x];
}
void union_set(int x,int y)
{
    int a=find_set(x);
    int b=find_set(y);
    if(a==b)
    {
        if(re[x]==re[y])
            flag=1;
        return ;
    }
    pa[a]=b;
    re[a]=(re[x]-re[y]+3)%2;
}
int main()
{
    int t,j=1;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d %d",&n,&m);
        flag=0;
        for(int i=1;i<=n;i++)
            make_set(i);
        for(int i=0;i<m;i++)
        {
            int a,b;
            scanf("%d %d",&a,&b);
            if(flag==1)
                continue;
            union_set(a,b);
        }
        printf("Scenario #%d:\n",j++);
        if(flag==1)
             printf("Suspicious bugs found!\n");
        else
           printf("No suspicious bugs found!\n");
        printf("\n");
    }
    return 0;
}
/*
种类 :2种
*/#include<bits/stdc++.h>
using namespace std;
#define max_v 2005
int pa[max_v];
int re[max_v];
int flag;
void make_set(int x)
{
    pa[x]=x;
    re[x]=0;
}
int find_set(int x)
{
    if(x==pa[x])
        return pa[x];
    int t=find_set(pa[x]);
    re[x]=(re[pa[x]]+re[x])%2;
    pa[x]=t;
    return pa[x];
}
void union_set(int x,int y)
{
    int a=find_set(x);
    int b=find_set(y);
    if(a==b)
    {
        if(re[x]==re[y])
            flag=1;
        return ;
    }
    pa[a]=b;
    re[a]=(re[x]-re[y]+3)%2;
}
int main()
{
    int t,j=1;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d %d",&n,&m);
        flag=0;
        for(int i=1;i<=n;i++)
            make_set(i);
        for(int i=0;i<m;i++)
        {
            int a,b;
            scanf("%d %d",&a,&b);
            if(flag==1)
                continue;
            union_set(a,b);
        }
        printf("Scenario #%d:\n",j++);
        if(flag==1)
             printf("Suspicious bugs found!\n");
        else
           printf("No suspicious bugs found!\n");
        printf("\n");
    }
    return 0;
}
/*
种类 :2种
*/

 

HDU 1829

标签:输出   col   并查集   rip   port   target   ssi   sea   about   

原文地址:https://www.cnblogs.com/yinbiao/p/9198599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!