poj 2492A Bug's Life(并查集)
/*
目大意:输入一个数t,表示测试组数。然后每组第一行两个数字n,m,n表示有n只昆虫,编号从1—n,m表示下面要输入m行交配情况,每行两个整数,表示这两个编号的昆虫为异性,要交配。
要求统计交配过程中是否出现冲突,即是否有两个同性的昆虫发生交配。 思路:并查集
将同性的昆虫放入集合之中,如果输入的昆虫u, v同时出现在了集合中,那么 u,v就是同性的!发生了同性交配的可能!
*/ #include <string>
#include <cstdio>
#include <cstring>
#include <iostream> using namespace std;
int n, m;
int f[];
int mark[];//mark[i]表示 与 i 交配的昆虫的编号! int getFather(int x){
return x==f[x] ? x : f[x]=getFather(f[x]);
} void Union(int a, int b){
int fa=getFather(a), fb=getFather(b);
if(fa!=fb)
f[fa]=fb;
} int main(){
int t, cnt=;
scanf("%d", &t);
while(t--){ scanf("%d%d", &n, &m);
for(int i=; i<=n; ++i)
f[i]=i;
memset(mark, , sizeof(mark));
int flag=;
while(m--){
int u, v;
scanf("%d%d", &u, &v);
if(flag){
if(getFather(u) == getFather(v)){
flag=;
continue;
}
if(!mark[u] && !mark[v]){
mark[u]=v;
mark[v]=u;
}
else if(!mark[u]){
mark[u]=v;
Union(u, mark[v]); //如果v配对了,u没有配对,那么u和mark[v]就是同性昆虫,放入集合之中
}
else if(!mark[v]){
mark[v]=u;
Union(v,mark[u]);//,,,,,,
}
else{
Union(u, mark[v]);//如果之前u和v都已经配对,现在u和v进行配对, 那么u和mark[v]是同性, v和mark[u]是同性!
Union(v, mark[u]);
}
}
}
printf("Scenario #%d:\n",++cnt);
if (flag==)
printf("No suspicious bugs found!\n");
else
printf("Suspicious bugs found!\n");
printf("\n");
}
}
poj 2492A Bug's Life(并查集)的更多相关文章
- poj 2236:Wireless Network(并查集,提高题)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 16065 Accepted: 677 ...
- A Bug's Life POJ - 2492 (带权并查集)
A Bug's Life POJ - 2492 Background Professor Hopper is researching the sexual behavior of a rare spe ...
- hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them
http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...
- nyoj 209 + poj 2492 A Bug's Life (并查集)
A Bug's Life 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Background Professor Hopper is researching th ...
- POJ 2492 A Bug's Life 并查集的应用
题意:有n只虫子,每次给出一对互为异性的虫子的编号,输出是否存在冲突. 思路:用并查集,每次输入一对虫子后就先判定一下.如果两者父亲相同,则说明关系已确定,再看性别是否相同,如果相同则有冲突.否则就将 ...
- poj 1182:食物链(种类并查集,食物链问题)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44168 Accepted: 12878 Description ...
- POJ 1456 Supermarket 区间问题并查集||贪心
F - Supermarket Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ 1182 食物链(种类并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63592 Accepted: 18670 Description ...
- POJ 2762 tarjan缩点+并查集+度数
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15494 ...
随机推荐
- poi解析excel 03、07
maven依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</a ...
- babylonjs
A Babylon.js Primer Table of contents Babylon.js WebGL Game Creation System! The Browser Useful Link ...
- 关于“windows无法自动将ip协议栈绑定到网络适配器”问题导致不能连上网的解决办法
问题出现的原因:这个问题的直接表象并不是显示给用户这个问题,而是提示无线网络驱动可能有问题或者以太网驱动可能有问题,但只要用户查看”详细信息“,就会得到标题这个问题,而出现这个问题的本质并不是驱动问题 ...
- 转:如何调试PHP的Core之获取基本信息
其实一直想写这个系列, 但是一想到这个话题的宽泛性, 我就有点感觉无法组织. 今天我也不打算全部讲如何调试一个PHP的Core文件, 也不会介绍什么是Coredump, 选择一个相对比较简单的方向来介 ...
- Android ------ handler 异步处理消息
Handler基本概念: Handler主要用于异步消息的处理:当发出一个消息之后,首先进入一个消息队列,发送消息的函数即刻返回,而另外一个部分逐个的在消息队列中将消息取出,然后对消息进行出来,就是发 ...
- iOS关于定制某个控件四个角是否为圆角
UIView *myView=[[UIView alloc]initWithFrame:CGRectMake(50, 70, 200, 200)]; UIBezierPath * bezierPath ...
- GCC 源码编译 mpc mprf gmp 不用make(否则会有lib/libgmp.so: could not read symbols: File in wrong format等错误)
错误信息: lib/libgmp.so: could not read symbols: File in wrong formatcollect2: error: ld returned 1 exit ...
- HTML5-链接
链接:外部,图片,内部 <!DOCTYPE html> <html> <head lang="en"> <meta charset=&qu ...
- python学习GUIwxpython不支持中文输出入的问题
# -*- coding: utf8 -*- import wx def load(event): file = open(filename.GetValue()) contents.SetValue ...
- iOS常用的忽略警告
在iOS开发过程中,偶尔会碰到一些编译器警告,如果能够确定该警告不会影响到程序的正常运行,则可以手动告诉编译器忽略掉这个警告 iOS常用的忽略警告类型: 1.方法弃用警告 #pragma clang ...