Description POJ1703
Description
Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:
1. D [a] [b]
where [a] and [b] are the numbers of two criminals, and they belong to different gangs.
2. A [a] [b]
where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.
Input
Output
Sample Input
1
5 5
A 1 2
D 1 2
A 1 2
D 2 4
A 1 4
Sample Output
Not sure yet.
In different gangs.
In the same gang.
Source
题解
并查集,如果我们有这样的关系D(a,b),D(b,c)
考虑这样存储关系 mix(a,b+n),mix(a+n,b),mix(b,c+n),mix(b+n,c) ,通过这样的存图,我们可以得到这样的集合(a,b+n,c)和(b,a+n,c+n)因此就可以找到正确的关系
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=1e5+10;
int pri[MAXN*2];
int find(int x)
{
if(pri[x]==x )return x;
return pri[x]=find(pri[x]);
}
void mix(int x,int y)
{
int a=find(x),b=find(y);
if(a!=b)
{
pri[a]=b;
}
}
int main()
{
int _;
scanf("%d",&_);
while(_--)
{
int n,m;
scanf("%d%d",&n,&m);
for (int i = 0; i <=n+n ; ++i) {
pri[i]=i;
}
for (int i = 0; i <m ; ++i) {
char S[2];
scanf("%s",&S);
if(S[0]=='A')
{
int x,y;
scanf("%d%d",&x,&y);
if(n==2)
{
printf("In different gangs.\n");
}
else if(find(x)==find(y))
{ printf("In the same gang.\n");
}
else if(find(x)==find(y+n))//y+n如果和x在一个集合中那么y一定和x不在一个集合中,如果直接find(x)!=find(y) 可能会有些y的关系还没有清楚
{
printf("In different gangs.\n");
}
else
{
printf("Not sure yet.\n");
} }
else if(S[0]=='D')
{
int x, y;
scanf("%d%d",&x,&y);
mix(x,y+n);
mix(x+n,y);
}
}
} return 0;
}
Description POJ1703的更多相关文章
- POJ1703(2集合并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39402 Accepted: ...
- 完美解决CodeSmith无法获取MySQL表及列Description说明注释的方案
问题描述: CodeSmith是现在比较实用的代码生成器,但是我们发现一个问题: 使用CodeSmith编写MySQL模板的时候,会发现一个问题:MySQL数据表中的列说明获取不到,也就是column ...
- 资源描述结构(Resource Description Framework,RDF)
资源描述框架(Resource Description Framework),一种用于描述Web资源的标记语言.RDF是一个处理元数据的XML(标准通用标记语言的子集)应用,所谓元数据,就是" ...
- MVC中得到成员元数据的Description特性描述信息公用方法
#region 从类型成员获取指定的Attribute T特性集合 /// <summary> /// 从类型成员获取指定的Attribute T特性集合 /// </summary ...
- 枚举扩展方法获取枚举Description
枚举扩展方法 /// <summary> /// 扩展方法,获得枚举的Description /// </summary> /// <param name="v ...
- 什么情况下才要重写Objective-C中的description方法
特别注意: 千万不要在description方法中同时使用%@和self,同时使用了%@和self,代表要调用self的description方法,因此最终会导致程序陷入死循环,循环调用descrip ...
- 启用WebApi 2里的Api描述信息(Help下的Description)
环境:vs2013+web api 2 问题:默认情况下新建的Web Api 2项目,自带的Help页下会显示Api的相关信息,但Description那一栏无法获取到数据,如下图所示: 解决: 1. ...
- Description Resource Path Location Type Error executing aapt: Return code -1073741819 Client line 1
Logcat报错:Description Resource Path Location Type Error executing aapt: Return code -1073741 ...
- C# Enum Name String Description之间的相互转换
最近工作中经常用到Enum中Value.String.Description之间的相互转换,特此总结一下. 1.首先定义Enum对象 public enum Weekday { [Descriptio ...
随机推荐
- python3线程介绍01(如何启动和调用线程)
#!/usr/bin/env python# -*- coding:utf-8 -*- import osimport time,randomimport threading # 1-进程说明# 进程 ...
- mongoDB基础知识(一)
mongoDB是一个基于分布式文件存储的数据库,介于关系型数据库和非关系型数据库之间,在非关系型数据库中功能最丰富, 最像关系型数据库.数据结构松散,类似于json的bson格式,可以存储比较复杂的数 ...
- IOS通讯录的隐藏标签【电话】的特殊功能(在IOS11已失效)
这功能比较适合有强迫症,爱折腾的人哈!! 规范了通讯录标签,以后可以轻松的知道别人是用短号还是亲情网给你打电话. 如果是长号还可以显示归属地. 也许从IOS8(不太清楚)开始自带了号码归属地显示功能, ...
- 2017.10.28 QB模拟赛 —— 下午
题目链接 T1 按x值排序 遇到第二种牌插入 遇到第一种牌 查询<=y 的最小值 删除他 splay multiset cys大佬说 multiset就是不去重的set, #include &l ...
- server 2008 64位安装Rational错误
Administrator has detected that this is a terminal server session.Administrator does not support run ...
- React怎么创建.babelrc文件
在windows环境下做react开发其实是一件非常让人头疼的事,强烈建议使用Mac或者是Linux系统,否则真的是自己挖坑自己跳了. 不过,这里还是给大家说说如何在windows环境下新建一个.ba ...
- cesium 加载倾斜摄影模型(这里有一坑)
代码如下: // Construct the default list of terrain sources. var terrainModels = Cesium.createDefaultTerr ...
- 基于LBS的多人聊天
- hdu-1892 See you~---二维树状数组运用
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1892 题目大意: 题目大意:有很多方格,每个方格对应的坐标为(I,J),刚开始时每个格子里有1本书, ...
- 【BZOJ5212】[ZJOI2018] 历史(LCT大黑题)
点此看题面 大致题意: 给定一棵树每个节点\(Access\)的次数,求最大虚实链切换次数,带修改. 什么是\(Access\)? 推荐你先去学一学\(LCT\)吧. 初始化(不带修改的做法) 首先考 ...