A Bug's Life

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

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!
这题还是找关系,然后套用公式 ,要注意的是这个题每组测试用例后面要多空一行。我标注的是relation[] 为0表示同性,1表示异性
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int N =;
int father[N];
int relation[N]; ///0表示同性,1表示异性 int _find(int x){
if(x!=father[x]){
int t = father[x];
father[x] = _find(t);
relation[x] = (relation[x]+relation[t])%;
}
return father[x];
}
int main()
{
int tcase;
scanf("%d",&tcase);
int k=;
while(tcase--){ int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
father[i] = i;
relation[i] = ;
}
int flag = ;
for(int i=;i<m;i++){
int a,b;
scanf("%d%d",&a,&b);
int roota = _find(a);
int rootb = _find(b);
if(roota==rootb){
if(relation[a]==relation[b]) { ///是同性
flag = ;
continue;
}
}else{
father[roota] = rootb;
relation[roota] =(-relation[a]+relation[b]++)%; ///roota->rootb = roota->a+a->b+b->rootb
}
}
printf("Scenario #%d:\n",(k++));
if(flag) printf("Suspicious bugs found!\n");
else printf("No suspicious bugs found!\n");
printf("\n");
}
return ;
}
 
 
 

hdu 1829(继续扩展并查集)的更多相关文章

  1. hdu 1829 带权并查集的运用类似于食物链但是更简单些

    #include<stdio.h> #define N 1100000 struct node { int x,y; }f[N],pre[N]; int find(int x) { if( ...

  2. HDU 1811 拓扑排序 并查集

    有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...

  3. hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)

    hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...

  4. hdu 3047(扩展并查集)

    Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  5. <hdu - 1232> 畅通工程 并查集问题 (注意中的细节)

    本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232  结题思路:因为题目是汉语的,那我就不解释题意了,要求的是最少建设的道路,我们可以用并查集来做这 ...

  6. HDU 5441 Travel(并查集+统计节点个数)

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给出一个图,每条边有一个距离,现在有多个询问,每个询问有一个距离值d,对于每一个询问,计算出有多少点 ...

  7. HDU 4313 Matrix(并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=4313 题意: 给出一棵树,每条边都有权值,其中有几个点是特殊点,现在破坏边还使得这几个特殊点互相不可达,需要使得 ...

  8. hdu 1558 (线段相交+并查集) Segment set

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1558 题意是在坐标系中,当输入P(注意是大写,我当开始就wa成了小写)的时候输入一条线段的起点坐标和终点坐 ...

  9. HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...

随机推荐

  1. wildcard ,notdir ,patsubst ,obj=$(dir:%.c=%.o)

    Makefile中wildcard的介绍 在Makefile规则中,通配符会被自动展开.但在变量的定义和函数引用时,通配符将失效.这种情况下如果需要通配符有效,就需要使用函数“wildcard”,它的 ...

  2. OLTP与OLAP比较【转】

    OLTP与OLAP的介绍 数据处理大致可以分成两大类:联机事务处理OLTP(on-line transaction processing).联机分析处理OLAP(On-Line Analytical ...

  3. SpringMVC配置详解(转)

    要想灵活运用Spring MVC来应对大多数的Web开发,就必须要掌握它的配置及原理. 一.Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0) 1. jar ...

  4. Codeforces Round #330 (Div. 2) B. Pasha and Phone

    B. Pasha and Phone time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. 查看自己电脑外网IP

    连着wifi,在CMD窗口中显示的无线局域网适配器IP很有可能是内网IP.此时可以通过下面方法查看自己的电脑外网IP. 1.前提条件可以上外网: 2.上外网百度,输入IP,进行搜索. 3.查看结果即可 ...

  6. 状压DP的总结

    状压dp的标志 ①数据小 ②通过题目所给出的条件以后得到的特征集合小 一:CF259div2 D: 题目大意:保证b[i]中每个数互质,给出a[i],然后求1~n的abs(a[i]-b[i])最小.a ...

  7. job源码分析

    /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agree ...

  8. 记一次rsync日志报错directory has vanished

    中午两点的时候邮件告知rsync同部svn源库失败,看rsync日志报错显示如上,当时还在上课,没在公司,怀疑是不是有人动了svn的版本库,后来询问同事并通过vpn登录服务器上查看版本库是正常的,也没 ...

  9. centos6.8配置FTP普通用户除了家目录外还能访问其他目录

    今天有个需求,使用ftp服务搭建一个文件共享服务器,每个普通用户除了能访问自己家目录的东西,还能访问一个公共的目录.配置步骤如下: 环境如下: 创建用户并配置密码(使用默认家目录/home) user ...

  10. 【BZOJ】1774: [Usaco2009 Dec]Toll 过路费

    [题意]给定无向图,距离定义为边权和+最大点权,询问若干个两点最短距离.n<=250. [算法]排序+floyd [题解]考虑floyd的过程是每次找一个中转点,为了在当前找到一条新路径时方便地 ...