POJ 2492 A Bug's Life(并查集)
http://poj.org/problem?id=2492
题意 :就是给你n条虫子,m对关系,每一对关系的双方都是异性的,让你找出有没有是同性恋的。
思路 :这个题跟POJ1703其实差不多,也是需要一个数组来存跟父亲节点的关系,需要两个集合来存是否有关系,在最后稍微变一下形就OK了。
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<stdlib.h> using namespace std; int pre[];//代表着父亲结点,如果D后边是a b,则pre[b]=a ;
int father[] ;//代表着这个点和父亲结点的关系,是属于同一类还是不同团伙
int m,n ; int find(int x)
{
int temp = pre[x] ;
if (pre[x] == x)
return x ;
pre[x] = find(pre[x]);
father[x] = father[x] == father[temp] ? : ;//等于0表示属于同一类
return pre[x];
} void unionn(int x,int y,int xx,int yy)
{
pre[xx] = yy ;
father[xx] = father[x] == father[y] ? : ;
}
int main()
{
int t ;
scanf("%d",&t) ;
int m,n ,kase = ;
while(t--)
{
scanf("%d %d",&n,&m) ;
for(int i = ; i <= n ; i++)
{
pre[i]=i;
father[i] = ;
}
bool flag = false ;
int a,b ;
for(int i = ; i <= m ; i++)
{
scanf("%d %d",&a,&b) ;
int aa = find(a),bb = find(b);
if(!flag)
{
if(aa != bb)
unionn(a,b,aa,bb) ;
else
{
if(father[a] == father[b])
flag = true ;
}
}
}
printf("Scenario #%d:\n",kase) ;
kase++ ;
if(flag)
printf("Suspicious bugs found!\n\n") ;
else printf("No suspicious bugs found!\n\n") ;
}
return ;
}
POJ 2492 A Bug's Life(并查集)的更多相关文章
- 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 2492 A Bug's Life (并查集)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 30130 Accepted: 9869 De ...
- A Bug's Life POJ - 2492 (种类或带权并查集)
这个题目的写法有很多,用二分图染色也可以写,思路很好想,这里我们用关于并查集的两种写法来做. 题目大意:输入x,y表示x和y交配,然后判断是否有同性恋. 1 带权并查集: 我们可以用边的权值来表示一种 ...
- POJ 1703 Find them, Catch them(并查集高级应用)
手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...
- 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条臭 ...
- POJ 2492 A Bug's Life (并查集)
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- POJ 2492 A Bug's Life【并查集高级应用+类似食物链】
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- hdu 1829 &poj 2492 A Bug's Life(推断二分图、带权并查集)
A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- poj 2492 A Bug's Life 二分图染色 || 种类并查集
题目链接 题意 有一种\(bug\),所有的交往只在异性间发生.现给出所有的交往列表,问是否有可疑的\(bug\)(进行同性交往). 思路 法一:种类并查集 参考:https://www.2cto.c ...
随机推荐
- [wordpress]后台自定义菜单字段和使用wordpress color picker
Wordpress Version 4.4.2 参考链接 插件使用wordpress color picker:Add A New Color Picker To WordPress 后台菜单自定义字 ...
- 1.2、Mybatis二级缓存测试
package me.gacl.test; import me.gacl.domain.User; import me.gacl.util.MyBatisUtil; import org.apache ...
- OAuth2集成——《跟我学Shiro》
http://jinnianshilongnian.iteye.com/blog/2038646 目前很多开放平台如新浪微博开放平台都在使用提供开放API接口供开发者使用,随之带来了第三方应用要到开放 ...
- 怎样启用SQL SERVER混合身份验证方式
转载:http://jingyan.baidu.com/article/380abd0aa8f2311d90192cd0.html 大家都知道sql server 有两种登录验证方式,即sql ser ...
- C#学习笔记11:C#中的顺序结构、分支结构、循环结构
顺序结构: 代码从Main()函数开始运行,从上到下,一行一行的执行,不漏掉代码. Int a=6; int b=5; int c=a+b; Console.Write(c); 分支结构: 代码有可能 ...
- mysql安全
安装MySql时,尽量选择别的端口(默认是3306),密码设复杂一点!在next的步骤中,注意不要选中"允许远程登录". Web漏洞检测及修复 http://wiki.open.q ...
- Centos7最小化安装后(minimal)安装图形界面
centos7下载地址:http://mirrors.cqu.edu.cn/CentOS/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso 下载后用vmwa ...
- 我的第一个html页面
<!DOCTYPE html><meta charset="UTF-8"><title>第一个html5界面</title>< ...
- 07_XPath_02_常用语法
[工程截图] [person.xml] <?xml version="1.0" encoding="UTF-8"?> <students> ...
- CodeForces 538B
Description A number is called quasibinary if its decimal representation contains only digits 0 or 1 ...