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

本文共 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

你可能感兴趣的文章
【BZOJ】1455 罗马游戏
查看>>
python 的urlparse学习
查看>>
vmtouch命令 -
查看>>
spring boot 实际应用(一) 内置tomcat 实现JMX配置
查看>>
MacOS X APK 最新版本 反编译
查看>>
SharePOint 2010 Mysettings 不能编辑用户信息
查看>>
Spring Boot打包部署和环境配置
查看>>
javascript基本语法总结
查看>>
【算法学习笔记】22.算法设计初步 二分查找 上下界判断
查看>>
451. 根据字符出现频率排序
查看>>
网易面试题:和为n连续正数序列
查看>>
mysql 数据类型
查看>>
C语言 指针练习-直接插入排序法
查看>>
2.Java实现基于SOAP的XML文档网络传输及远程过程调用(RPC)-
查看>>
7.zookeeper集群搭建(windows环境下)
查看>>
spring中aop的注解实现方式简单实例
查看>>
Java发送邮件Utils
查看>>
JFreeChart与struts2整合实例
查看>>
利用Arraylist数组简单实现随机双色球Demo
查看>>
使用OpenCV&&C++进行模板匹配
查看>>