POJ 2492 并查集应用的扩展
A Bug's Life
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 28651 Accepted: 9331
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!
Hint
Huge input,scanf is recommended.
Source
TUD Programming Contest 2005, Darmstadt, Germany
/*********************************
author : Grant Yuan
time : 2014/10/5 11:29
algorithm: 并查集应用的扩展
source : POJ 2492
**********************************/
#include<iostream>
#include<cstdio>
#define MAX 2007
using namespace std;
int parent[MAX],relation[MAX];
int n,t,m;
int find_parent(int k)
{
if(k==parent[k])
return k;
int t=parent[k];
parent[k]=find_parent(parent[k]);
relation[k]=(relation[k]+relation[t]+1)%2;
return parent[k];
}
void unite(int a,int b)
{
int pa=find_parent(a);
int pb=find_parent(b);
parent[pa]=pb;
relation[pa]=(relation[b]-relation[a])%2;
}
int main()
{
scanf("%d",&t);
int ct=1;bool first=0;
while(t--){
bool ans=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
parent[i]=i,relation[i]=1;
for(int i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
int pa=find_parent(a);
int pb=find_parent(b);
if(pa==pb){
if(relation[a]==relation[b]) ans=1;
}
else unite(a,b);
}
printf("Scenario #%d:\n",ct++);
if(ans) printf("Suspicious bugs found!\n\n");
else printf("No suspicious bugs found!\n\n");
}
return 0;
}
最新文章
- jQuery 绑定事件
- 第5.5次Scrum会议
- Atitit.js图表控件总结
- PHP学习笔记:用php读取xml文件
- mysql主从同步mysql slave_io_running:no的解决方案
- kickstrat
- Promise的用简要使用方式
- Microsoft Office 2007 Professional Plus+ 正版密钥
- hdu 1385 Minimum Transport Cost
- /etc/fstab 文件详解 及 /etc/mtab
- ke
- LCS 算法实现
- 高精度(x ,/, +, -, %)良心模板
- tomcat端口设置
- HTML 第十二章总结
- python入门之深浅copy
- google打不开怎么办?谷歌打不开的解决方法
- 【HTML】div居中显示
- Spring Cloud Netflix项目进入维护模式
- java instanceof和isInstance的关系 精析
热门文章
- hive内置函数大全
- std::list.pop_back() 弹空了列表导致的崩溃
- Unity3D合并着色器
- [ExtJS5学习笔记]第22 Extjs5正在使用beforeLabelTpl添加所需的配置选项标注星号标记
- OGG &;quot;Loading data from file to Replicat&;quot;table静态数据同步配置过程
- atitit.为什么技术的选择方法java超过.net有前途
- Linux内核分析(二)----内核模块简介|简单内核模块实现
- Testin一日游实验室发布的行级APP质量报告:在那里拍携程双赢
- react.js 从零开始(二)组件的生命周期
- osx launchpad删除图标