POJ 2492 A Bug's Life (并查集)
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 30130 | Accepted: 9869 |
Description
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
Output
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! 判断两只虫子是否同性。
#include <iostream>
#include <cstdio>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std; const int SIZE = ;
int FATHER[SIZE],MARK[SIZE],RANK[SIZE]; void ini(int);
int get_father(int);
void unite(int,int);
bool same(int,int); int main(void)
{
int t,n,m,x,y,count = ;
bool flag; scanf("%d",&t);
while(t --)
{
count ++;
scanf("%d%d",&n,&m);
ini(n);
flag = false;
while(m --)
{
scanf("%d%d",&x,&y);
if(flag)
continue;
if(same(x,y))
flag = true;
if(!MARK[x] && !MARK[y])
{
MARK[x] = y;
MARK[y] = x;
}
else if(!MARK[x])
{
MARK[x] = y;
unite(x,MARK[y]);
}
else if(!MARK[y])
{
MARK[y] = x;
unite(y,MARK[x]);
}
else
{
unite(x,MARK[y]);
unite(y,MARK[x]);
}
}
printf("Scenario #%d:\n",count);
if(flag)
puts("Suspicious bugs found!");
else
puts("No suspicious bugs found!");
puts("");
} return ;
} void ini(int n)
{
for(int i = ;i <= n;i ++)
{
MARK[i] = RANK[i] = ;
FATHER[i] = i;
}
} int get_father(int n)
{
if(n == FATHER[n])
return n;
return FATHER[n] = get_father(FATHER[n]);
} void unite(int x,int y)
{
x = get_father(x);
y = get_father(y); if(x == y)
return ;
if(RANK[x] < RANK[y])
FATHER[x] = y;
else
{
FATHER[y] = x;
if(RANK[x] == RANK[y])
RANK[x] ++;
}
} bool same(int x,int y)
{
return get_father(x) == get_father(y);
}
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只虫子,每次给出一对互为异性的虫子的编号,输出是否存在冲突. 思路:用并查集,每次输入一对虫子后就先判定一下.如果两者父亲相同,则说明关系已确定,再看性别是否相同,如果相同则有冲突.否则就将 ...
- 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 ...
随机推荐
- Rule of write code with C# in Unity3d
Good Practice 普通的Public变量可以在inspect里显示,变量是可以在inspect里赋值并realtime反映在被attach到的GameObject上的.注意经典public ...
- Release 版本和 Debug 版本
什么是 Release 版本.Debug 版本? bug-缺陷,程序故障.而debug指的是排除缺陷,显然这个模式是面向开发者的. 而release是满足发布所用. Debug 和 Release,在 ...
- MFC打开文件对话框
{ CString FilePathName; CFileDialog dlg(TRUE);///TRUE为OPEN对话框,FALSE为SAVE AS对话框 if(dlg.DoModal()==IDO ...
- HTML要点(四)<meta>标签
浏览器支持 所有浏览器都支持 <meta> 标签. 定义和用法 <meta> 元素可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和 ...
- 一条结合where、group、orderby的linq语法
DataTable dt = (from x in dsResult.Tables[0].AsEnumerable() where DataTrans.CBoolean(x["IsCheck ...
- A debugger is already attached
Today is the last day that all the laptops of winXP OS should be upgrade to WIN7. After updated. whe ...
- Ext combox 动态 检索
spring mvc + extjs 免费下载 1 ext jar包 点击打开链接 2 ext 中文api 点击打开链接 java code: package com.paic.bbs.action; ...
- 都是iconv惹的祸
今天在做采集的时候发现只取到了网页的部分内容,当时我就郁闷了,之前都用的采集都可以采集到网页的所有内容,但这次死活就取到部分内容.寻找原因才知道原来是iconv惹的祸. 发现问题时,网上搜了搜,才发现 ...
- RedHat Linux 安装oracle11g
1.准备oracle安装文件Oracle11gR2包含两个文件linux_11gR2_database_1of2.zip和linux_11gR2_database_2of2.zip,将这两个文件通过S ...
- 检测MYSQL不同步发邮件通知的脚本
脚本代码如下:#!/bin/bash ...