hdu 1829(继续扩展并查集)
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
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.
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 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.
3 3
1 2
2 3
1 3
4 2
1 2
3 4
Suspicious bugs found!
No suspicious bugs found!
#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(继续扩展并查集)的更多相关文章
- hdu 1829 带权并查集的运用类似于食物链但是更简单些
#include<stdio.h> #define N 1100000 struct node { int x,y; }f[N],pre[N]; int find(int x) { if( ...
- HDU 1811 拓扑排序 并查集
有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...
- hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)
hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...
- hdu 3047(扩展并查集)
Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- <hdu - 1232> 畅通工程 并查集问题 (注意中的细节)
本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 结题思路:因为题目是汉语的,那我就不解释题意了,要求的是最少建设的道路,我们可以用并查集来做这 ...
- HDU 5441 Travel(并查集+统计节点个数)
http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给出一个图,每条边有一个距离,现在有多个询问,每个询问有一个距离值d,对于每一个询问,计算出有多少点 ...
- HDU 4313 Matrix(并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=4313 题意: 给出一棵树,每条边都有权值,其中有几个点是特殊点,现在破坏边还使得这几个特殊点互相不可达,需要使得 ...
- hdu 1558 (线段相交+并查集) Segment set
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1558 题意是在坐标系中,当输入P(注意是大写,我当开始就wa成了小写)的时候输入一条线段的起点坐标和终点坐 ...
- HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...
随机推荐
- sass的循环for,while,each
1. for @for $i from 1 to 10 { .border-#{$i} { border: #{$i}px solid blue; } } 2. while $i: 6; @while ...
- dubbox管理中心
当服务多了,不好管理,可以用管理中心来查看. 管理中心就是一个 web项目(一个war包),可以部署在tomcat下面就能运行了. 用户名密码都是root 每一条服务点开后都可以看生产者和消费者.
- 自动清理N天前的二进制日志
这里以自动清理5天前的二进制日志为例(做了同步或依赖于二进制日志备份的请慎用): 以root身份登录数据库,执行以下命令: ; 首次设置expire_logs_days参数后需要执行flush log ...
- Nginx反向代理两个tomcat服务器
第一步,在Linux上安装两个tomcat,修改好端口号后,启动起来. 第二步,配置本地的DNS解析,即修改host文件: 第三步,配置Nginx配置文件 反向代理的配置虚拟主机配置差不多也要配置虚拟 ...
- kali wireless driver install
acer-gateway nv47h94c 1.lspci -nn |grep 0280 get pci-id root@silee:/# lspci -nn |grep 028003:00.0 Ne ...
- OopenCV复习及函数深入理解(轮廓查询及绘图)
核心函数:(后面标明号的,下面有解析) int cvFindContours(Iplimage* img,//这是输入函数,必须是8bit,单通道的图像---1 CvMemStorage* stora ...
- Android滚动栏控件的优化
背景 由于普通TextView的跑马灯效果与焦点获取有关 所以不能直接使用 之前查找到的控件在数据设置方面存在问题 所以通过寻找github上的开源控件 并修改源码 得到一个目前感觉不错的效果 原理 ...
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- j2ee 项目部署指引
j2ee相关的项目一般是web工程或java application,部署到linux服务器上,本文结合自己的经验.教训,总结下部署的过程. 一.准备阶段 部署前要做的事情: 1.明确自己的产品都包含 ...
- java web中resources路径
UserBean.class.getClassLoader().getResource(filePath).getPath() 或者 Thread.currentThread().getContext ...