博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[poj2492]A Bug's Life(并查集+补集)
阅读量:5092 次
发布时间:2019-06-13

本文共 2995 字,大约阅读时间需要 9 分钟。

A Bug's Life
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 34678   Accepted: 11339

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

23 31 22 31 34 21 23 4

Sample Output

Scenario #1:Suspicious bugs found!Scenario #2:No suspicious bugs found!

Hint

Huge input,scanf is recommended.

Source

, Darmstadt, Germany
 
 
此题同poj1182 http://www.cnblogs.com/Pumbit-Legion/p/5925331.html
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #define LL long long 9 int fa[4020],size[4020];10 int n;11 inline int read(){12 int sum=0;char ch=getchar();13 while(ch>'9'||ch<'0')ch=getchar();14 while(ch<='9'&&ch>='0'){15 sum=sum*10+ch-'0';16 ch=getchar();17 }18 return sum;19 }20 inline int fnd(int x){21 int r=x;22 while(x!=fa[x])x=fa[x];23 int tmp;24 while(r!=x)tmp=fa[r],fa[r]=x,r=tmp;25 return x;26 }27 inline int uni(int x,int y){28 int fx=fnd(x);29 int fy=fnd(y);30 if(size[fx]
size[fy])fa[fy]=fx;32 else{33 fa[fy]=fx;34 ++size[fx];35 }36 return 0;37 }38 int main(){39 int t,x=1;40 t=read();41 while(x<=t){42 printf("Scenario #%d:\n",x);43 int i,m;44 n=read(),m=read();45 memset(size,0,sizeof(size[0])*n*2+10);46 for(i=1;i<=n*2;++i)fa[i]=i;47 int note=0,a,b;48 for(i=1;i<=m;++i){49 a=read(),b=read();50 if(!note){51 int fa1=fnd(a);52 int fb1=fnd(b);53 if(fa1==fb1){54 note=1;55 puts("Suspicious bugs found!");56 continue;57 }58 int fa2=fnd(a+n);59 int fb2=fnd(b+n);60 uni(fa1,fb2);61 uni(fa2,fb1);62 }63 }64 if(!note)puts("No suspicious bugs found!");65 puts("");66 x++;67 }68 return 0;69 }
View Code

 

转载于:https://www.cnblogs.com/Pumbit-Legion/p/5925332.html

你可能感兴趣的文章
centos redis 安装过程,解决办法
查看>>
IOS小技巧整理
查看>>
WebDriverExtensionsByC#
查看>>
我眼中的技术地图
查看>>
lc 145. Binary Tree Postorder Traversal
查看>>
sublime 配置java运行环境
查看>>
在centos上开关tomcat
查看>>
重启rabbitmq服务
查看>>
正则表达式(进阶篇)
查看>>
无人值守安装linux系统
查看>>
【传道】中国首部淘宝卖家演讲公开课:农业本该如此
查看>>
jQuery应用 代码片段
查看>>
MVC+Servlet+mysql+jsp读取数据库信息
查看>>
黑马程序员——2 注释
查看>>
用OGRE1.74搭建游戏框架(三)--加入人物控制和场景
查看>>
转化课-计算机基础及上网过程
查看>>
android dialog使用自定义布局 设置窗体大小位置
查看>>
ionic2+ 基础
查看>>
互联网模式下我们更加应该“专注”
查看>>
myeclipse集成jdk、tomcat8、maven、svn
查看>>