hdu 1829 基础并查集,查同性恋

A Bug‘s Life

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

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!

题目大意:给你若干个bug,保证他们是异性,看最后有没有同性恋‘

思路分析:基础并查集,维护与根节点的关系即可

poj1703 同类型

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=2000+100;
int fa[maxn];
int relate[maxn];//记录与父节点的性别关系,1代表异性,0代表同性
int n,m;
int kase=0;
int root(int x)
{
    if(x==fa[x]) return x;
    int t=root(fa[x]);
    relate[x]=(relate[x]+relate[fa[x]])%2;
    fa[x]=t;
    return fa[x];
}
void merge(int x,int y)
{
    int fx=root(x);
    int fy=root(y);
    if(fx==fy) return;
    fa[fx]=fy;
    relate[fx]=(relate[y]+relate[x]+1)%2;
    return;
}
int main()
{
    int T;
    int a,b;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            fa[i]=i,relate[i]=0;
        bool flag=false;
        for(int j=1;j<=m;j++)
        {
            scanf("%d%d",&a,&b);
            if(root(a)==root(b)&&relate[a]==relate[b])
            {
                flag=true;
            }
            else merge(a,b);
            //printf("yes\n");
        }
        //printf("dsasd\n");
        printf("Scenario #%d:\n",++kase);
        if(flag) printf("Suspicious bugs found!\n\n");
        else printf("No suspicious bugs found!\n\n");

    }
    return 0;
}
时间: 2024-11-07 18:09:27

hdu 1829 基础并查集,查同性恋的相关文章

HDU(3560)成环,并查集

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3560 并查集查有几个块,修改了之前我的一个方法(用什么map),直接判断根节点的id是i的个数. 然后成环的判断就是一个筛选,先筛选一个每个节点的度是不是2,要不是的话直接排除(根节点),在查块的时候,进一步查看这个根节点是不是被排除了. so easy; #include <stdio.h> #include <string.h> int father[100010]; int de

PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]

题目 One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls mad

PAT Advanced 1107 Social Clusters (30) [并查集]

题目 When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A "social cluster" is a set of people who have some of their hobbies in common. You are supposed to

hdu 1829 A Bug&#39;s Life (基础并查集)

题目: 链接:点击打开链接 题意: 给定虫子的交配关系,确定实验是否支持教授的假设即没有同性恋或者不符合假设. 思路: 是一道基础的并查集题目.存在两个集合异性和同性,给出多组关系,看这两个集合有木有联系,即是否有同性恋. 定义一个数组sex[],sex[i]表示与编号i的性别相反的虫子编号.然后将和i虫子有联系的合并为同一个集合(认为是同性的).如果findset(u) == findset(v),出现了反常行为. 代码: #include <iostream> #include <c

hdu 1829 A Bug&#39;s Life (基础种类并查集)

先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组,一个存并查集内的父子关系,一个存各个节点所属的种类关系. 以这道题为例(题意在后面,如果没有读题,可以先看完题在来看这部分)—— 这道题很明显,将bug分成两类,一公一母.但是实际上我们并不关心它是公的还是母的,只关心它们之间是同性还是异性.所以,我们可以设与并查集的根节点同性的为0,反之为1.所

HDU 1829 A Bug&#39;s Life (并查集)

题目链接:请戳这里. 题目大意及思路:给定n个Bugs(shen me gui)和m对Bugs之间的关系,假定关系是两个Bugs的男女关系,那么问存不存在同性恋的情况. 那么若a与b是男女关系,b与c是男女关系,那么a和c的性别我们就可以认为是相同的.我们用可以建立两个并查集,一类放男男关系,一类放女女关系. 那么若男男关系中出现了环的情况(即有共同的根),那么同性恋关系就出现了. #include<iostream> #include<cstdio> #include<cs

hdu 1829 A Bug&#39;s Life(分组并查集(偏移量))

A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9204    Accepted Submission(s): 2961 Problem Description Background Professor Hopper is researching the sexual behavior of a rare sp

POJ 2492 || HDU 1829:A Bug&#39;s Life(并查集)

传送门: POJ:点击打开链接 HDU:点击打开链接 A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 27624   Accepted: 8979 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they fe

hdu 1829 并查集(食物链的弱化版)

http://acm.hdu.edu.cn/showproblem.php?pid=1829 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