hdu 1829 A Bug's Life(分组并查集(偏移量))
A Bug's Life
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9204 Accepted Submission(s): 2961
Problem 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
题意:
1喜欢2,2喜欢3,而1又喜欢3,则矛盾。 (排出同性恋)
基础并查集的新应用:
分组并查集(偏移量):开一个并查集,但是要加个偏移向量数组,来记录每个节点距离根节点的距离
代码:
1 /*带权值的并查集*/
2 #include<cstdio>
3 #define maxn 2005
4 int father[maxn],rank[maxn];
5 int tt,nn,mm;
6 void init()
7 {
8 for(int i=1;i<nn;i++){
9 father[i]=i;
10 rank[i]=0;
11 }
12 }
13
14 int fin(int x)
15 {
16 if(x==father[x]) return x;
17 int temp=fin(father[x]);
18 rank[x]=(rank[x]+rank[father[x]])%2;
19 father[x]=temp;
20 return father[x];
21 }
22 int unin(int a,int b)
23 {
24 int x=fin(a);
25 int y=fin(b);
26 if(x==y)
27 {
28 if(rank[a]==rank[b])return 1; //说明矛盾
29 return 0;
30 }
31 father[x]=y;
32 rank[x]=(rank[a]+rank[b]+1)%2;
33 return 0;
34 }
35 int main()
36 {
37 int a,b;
38 int tag;
39 scanf("%d",&tt);
40 for(int j=1;j<=tt;j++)
41 {
42 tag=0;
43 scanf("%d%d",&nn,&mm);
44 init();
45 for(int i=0;i<mm;i++)
46 {
47 scanf("%d%d",&a,&b);
48 tag+=unin(a,b);
49 }
50 printf("Scenario #%d:n",j);
51
52 if(tag)puts("Suspicious bugs found!");
53 else puts("No suspicious bugs found!");
54 puts("");
55 }
56 return 0;
57 }
- Vue.js系列之四计算属性和观察者
- EF基础知识小记四(数据库=>模型设计器)
- OEA ORM 框架中的冗余属性设计
- MIS性能优化常见问题与方案(辅助项目组性能优化的总结贴)
- 使用自定义行为扩展 WCF
- android多线程下载3
- android多线程下载2
- android多线程下载1
- ASP获取微信小程序的OpenID服务器端代码
- 手把手教你用python抢票回家过年 !(附代码)
- 技术总结:自动扩张WPF树型表格列宽
- 部署Microsoft .NET Framework 3.0[翻译]
- 性能优化总结(六):预加载、聚合SQL应用实例
- 性能优化总结(五):CSLA服务端如何使用多线程的解决方案
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 总结:DCIC算法分析赛完整方案分享!
- 提高微服务安全性的11个方法
- nmap
- MSF基础与应用
- Windows系统组件漏洞
- 【风险通告】FastAdmin会员中心Getshell漏洞
- Azure Cosmos DB介绍及演示
- 从一次编译出发梳理概念: Jetty,Jersey,hk2,glassFish,Javax,Jakarta
- 《一起学sentinel》一、一起搭建sentinel服务
- InfluxDB和Grafana实现传感器数据的存储和可视化
- 样本相关性分析
- Android 序列化 Serializable与Parcelable
- 《一起学sentinel》二、初探sentinel的Slot
- Redis突然报错,今晚又不能回家了...
- 查询ElasticSearch:用SQL代替DSL