博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2492 A Bug's Life (并查集)
阅读量:6247 次
发布时间:2019-06-22

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

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

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! 判断两只虫子是否同性。
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 using namespace std; 14 15 const int SIZE = 2005; 16 int FATHER[SIZE],MARK[SIZE],RANK[SIZE]; 17 18 void ini(int); 19 int get_father(int); 20 void unite(int,int); 21 bool same(int,int); 22 23 int main(void) 24 { 25 int t,n,m,x,y,count = 0; 26 bool flag; 27 28 scanf("%d",&t); 29 while(t --) 30 { 31 count ++; 32 scanf("%d%d",&n,&m); 33 ini(n); 34 flag = false; 35 while(m --) 36 { 37 scanf("%d%d",&x,&y); 38 if(flag) 39 continue; 40 if(same(x,y)) 41 flag = true; 42 if(!MARK[x] && !MARK[y]) 43 { 44 MARK[x] = y; 45 MARK[y] = x; 46 } 47 else if(!MARK[x]) 48 { 49 MARK[x] = y; 50 unite(x,MARK[y]); 51 } 52 else if(!MARK[y]) 53 { 54 MARK[y] = x; 55 unite(y,MARK[x]); 56 } 57 else 58 { 59 unite(x,MARK[y]); 60 unite(y,MARK[x]); 61 } 62 } 63 printf("Scenario #%d:\n",count); 64 if(flag) 65 puts("Suspicious bugs found!"); 66 else 67 puts("No suspicious bugs found!"); 68 puts(""); 69 } 70 71 72 return 0; 73 } 74 75 void ini(int n) 76 { 77 for(int i = 1;i <= n;i ++) 78 { 79 MARK[i] = RANK[i] = 0; 80 FATHER[i] = i; 81 } 82 } 83 84 int get_father(int n) 85 { 86 if(n == FATHER[n]) 87 return n; 88 return FATHER[n] = get_father(FATHER[n]); 89 } 90 91 void unite(int x,int y) 92 { 93 x = get_father(x); 94 y = get_father(y); 95 96 if(x == y) 97 return ; 98 if(RANK[x] < RANK[y]) 99 FATHER[x] = y;100 else101 {102 FATHER[y] = x;103 if(RANK[x] == RANK[y])104 RANK[x] ++;105 }106 }107 108 bool same(int x,int y)109 {110 return get_father(x) == get_father(y);111 }

 

转载于:https://www.cnblogs.com/xz816111/p/4536825.html

你可能感兴趣的文章
七年软件测试历程,回过头来,最能帮助我的还是这些.....
查看>>
yum 安装nginx
查看>>
前端(js+JQuery非空校验)
查看>>
[Android] ImageView.ScaleType设置图解
查看>>
银行卡卡号验证
查看>>
VS的内存断点
查看>>
Jenkins实战演练之Linux系统节点管理
查看>>
为什么init脚本需要lock文件
查看>>
利用python的zmail模块发送邮件
查看>>
DIY-希捷硬盘固件问题的解决方法
查看>>
有关电脑不能上网的检查方法
查看>>
我的友情链接
查看>>
Spring5最新源码导入Eclipse详解
查看>>
Linux 基础学习
查看>>
我的友情链接
查看>>
js之拖动节点放置链路上
查看>>
window server 2008 R2 standard ×××配置
查看>>
jdbc基础知识
查看>>
XDebug 调试 php
查看>>
百度地图自定义一个布局Mark
查看>>