POJ1703Find them, Catch them 【种类并查集】
题目链接:http://poj.org/problem?id=1703
题目大意:给n个人,m次询问。A代表询问a, b之间的关系,D代表给出a, b属于不同的帮派。
我的想法:
太菜了,上课的时候没想到这种方法。
思路:
1.感觉有点像2-sat的思想,对于D给出的a, b两人属于不同帮派。我们就将a于b的对立点用并查集维护在一个集合中,也将b于a的对立点连通。
2.查询时,若a,b两人pre值相等则属于同一帮派。若a与b的对立点属于同一集合,或b与a的对立点属于同一集合,则说明a与b属于不同帮派。若上述都不成立则说明关系还未知。
3.对立点是虚构的,不能影响到原来的点。所以数据范围翻倍,x + n代表x的对立点。
注意:不能用 cin 输入,会超时。只能用scanf
#include<stdio.h>
const int maxn = 2e5 + 1e4; int pre[maxn]; int find(int x)
{
if(pre[x] == x)
return x;
else
{
int root = find(pre[x]);
pre[x] = root;
return pre[x];
}
} int main()
{
int T;
scanf("%d", &T);
while(T --)
{
int n, m;
scanf("%d%d", &n, &m);
getchar();
for(int i = ; i <= * n; i ++)
{
pre[i] = i;
}
for(int i = ; i <= m; i ++)
{
char ch;
int a, b;
scanf("%c%d%d", &ch, &a, &b);
if(ch == 'D')
{
int x = find(a), y = find(b);
int xx = find(a + n), yy = find(b + n);
if(x != yy)
pre[yy] = x;
if(y != xx)
pre[xx] = y;
}
if(ch == 'A')
{
int x = find(a), y = find(b);
int xx = find(a + n), yy = find(b + n);
if(x == y)
printf("In the same gang.\n");
else if(x == yy || y == xx)
printf("In different gangs.\n");
else
printf("Not sure yet.\n");
}
getchar();
}
}
return ;
}
POJ1703
POJ1703Find them, Catch them 【种类并查集】的更多相关文章
- POJ1703Find them, Catch them[种类并查集]
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42416 Accepted: ...
- [poj1703]Find them, Catch them(种类并查集)
题意:食物链的弱化版本 解题关键:种类并查集,注意向量的合成. $rank$为1代表与父亲对立,$rank$为0代表与父亲同类. #include<iostream> #include&l ...
- poj1703Find them, Catch them(并查集以及路径压缩)
/* 题目大意:有两个不同的黑帮,开始的时候不清楚每个人是属于哪个的! 执行两个操作 A a, b回答a, b两个人是否在同一帮派,或者不确定 D a, b表示a, b两个人不在同一个帮派 思路:利用 ...
- poj--1703--Find them, Catch them(并查集巧用)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64 ...
- POJ 1703 Find them,Catch them ----种类并查集(经典)
http://blog.csdn.net/freezhanacmore/article/details/8774033?reload 这篇讲解非常好,我也是受这篇文章的启发才做出来的. 代码: #i ...
- POJ 1703 Find them, Catch them(种类并查集)
题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...
- POJ1703--Find them, Catch them(种类并查集)
Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 32909Accepted: 10158 Description The polic ...
- poj1703 Find them, Catch them(种类并查集
题目地址:http://poj.org/problem?id=1703 题目大意:警察抓了n个坏蛋,这些坏蛋分别属于龙帮或蛇帮.输入m个语句,A x y询问x和y的关系(在一个帮派,不在,不能确定), ...
- POJ1703 Find them Catch them 关于分集合操作的正确性证明 种类并查集
题目链接:http://poj.org/problem?id=1703 这道题和食物链那道题有异曲同工之处,都是要处理不同集合之间的关系,而并查集的功能是维护相同集合之间的关系.这道题中有两个不同的集 ...
- NOI2001|POJ1182食物链[种类并查集 向量]
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65430 Accepted: 19283 Description ...
随机推荐
- 【Android-Zxing框架】二维码扫描框区域大小与不同手机分辨率适配问题
Zxing框架进行二维码扫描时候会发现,随着分辨率的增加,扫描框会越来越小,SurfaceView扫描窗口就看不见了,我们可以自己定义扫描窗口的大小,以及适配屏幕问题. Zxing包中有个类Camer ...
- 012_linuxC++之_类的继承定义
(一)访问控制和继承 公有继承(public):当一个类派生自公有基类时,基类的公有成员也是派生类的公有成员,基类的保护成员也是派生类的保护成员,基类的私有成员不能直接被派生类访问,但是可以通过调用基 ...
- apply, bind, call--绑定this的方法
Function.prototype.call(),Function.prototype.apply(),Function.prototype.bind() 是三种改变函数内部this指向(即函数执行 ...
- Windows:获取本地时间
造冰箱的大熊猫@cnblogs 2019/6/4 #include <windows.h> int func() { SYSTEMTIME systime; GetLocalTime ( ...
- Hdu 4333 Revolving Digits(Exkmp)
Revolving Digits Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- 数据类型(C++)
不同系统会有不同差异: 类型 位(byte) 范围 char 1 -128—127 or 0 – 255 unsigned char 1 0 – 255 signed int 1 -128—127 i ...
- c++继承子类构造函数问题
c++中子类在继承基类的时候需要在构造函数中初始化变量.如果基类没有构造函数或者只有不带参数的构造函数,那么子类中的构造函数就不需要调用基类的构造函数了. 个人总结了两点子类中构造函数必须调用父类的构 ...
- python控制窗口移动(轨迹为圆)
需先打开一个无标题记事本, import win32con import win32gui import time import math notepad = win32gui.FindWindow( ...
- RabbitMQ 简使用案例
第一步导入依赖 : <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-clie ...
- Java项目服务器跨域设置
引入jar包 cors-filter-2.6 :http://central.maven.org/maven2/com/thetransactioncompany/cors-filter/2.6/co ...