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. 20181015 考试记录&数论

    题目传送门 W神爷的题解 数论 小 M 的算式 [问题描述] 小 M 在做数学作业的时候遇到了一个有趣的问题:有一个长度为 n 的数字 串 S,小 M 需要在数字之间填入若干个“+”和恰好一个“=”, ...

  2. PhoneGap API介绍:Camera

    本文将介绍PhoneGap API——Camera:使用设备的摄像头采集照片,对象提供对设备默认摄像头应用程序的访问. 方法: camera.getPicture 参数: cameraSuccess ...

  3. FileProvider记录下

    Mark下FileProvider,阿里巴巴Android开发手册有如下要求:[强制]应用间共享文件时,不要通过放宽文件系统权限的方式去实现,而应使用FileProvider. 知识点记录:1. An ...

  4. HDU3666 差分约束

    THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  5. C# 把类实例保存到文件里(类的序列化和反序列化)

    有时候我们希望把类的实例保存下来,以便以后的时候用.一个直观的方法就是StreamWriter把类写成一行,用\t分隔开每个属性,然后用StreamReader读出来. 但是这样太麻烦,代码行数较多, ...

  6. 51Nod 1014 X^2 Mod P

    注意潜在范围 x*x用long long #include <bits/stdc++.h> using namespace std; typedef long long LL; #defi ...

  7. vijos 1448 校门外的树 树状数组

    描述 校门外有很多树,有苹果树,香蕉树,有会扔石头的,有可以吃掉补充体力的……如今学校决定在某个时刻在某一段种上一种树,保证任一时刻不会出现两段相同种类的树,现有两个操作:K=1,K=1,读入l.r表 ...

  8. asp.net 文件上传,大文件上传。

    新建一个asp.net页面,在工具栏里拖入 FileUpload 上传控件.一个按钮 Button  !    !     ! 进入Button事件 //----------------------- ...

  9. 【bzoj1594-猜数游戏】线段树

    题解: 矛盾只有两种情况: 一.先前确定了x在区间(l,r),但是现在发现x在区间(l1,r1),并且两个区间不相交. 二.一个区间的最小值是x,这个区间中有一个子区间的最小值比x更小. 首先可以明确 ...

  10. 【BZOJ】1477 青蛙的约会

    [算法]扩展欧几里德算法(模线性方程) [题解]http://hzwer.com/2121.html 一些问题写在http://www.cnblogs.com/onioncyc/p/6146143.h ...