题目链接: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 【种类并查集】的更多相关文章

  1. POJ1703Find them, Catch them[种类并查集]

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42416   Accepted: ...

  2. [poj1703]Find them, Catch them(种类并查集)

    题意:食物链的弱化版本 解题关键:种类并查集,注意向量的合成. $rank$为1代表与父亲对立,$rank$为0代表与父亲同类. #include<iostream> #include&l ...

  3. poj1703Find them, Catch them(并查集以及路径压缩)

    /* 题目大意:有两个不同的黑帮,开始的时候不清楚每个人是属于哪个的! 执行两个操作 A a, b回答a, b两个人是否在同一帮派,或者不确定 D a, b表示a, b两个人不在同一个帮派 思路:利用 ...

  4. poj--1703--Find them, Catch them(并查集巧用)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64 ...

  5. POJ 1703 Find them,Catch them ----种类并查集(经典)

    http://blog.csdn.net/freezhanacmore/article/details/8774033?reload  这篇讲解非常好,我也是受这篇文章的启发才做出来的. 代码: #i ...

  6. POJ 1703 Find them, Catch them(种类并查集)

    题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...

  7. POJ1703--Find them, Catch them(种类并查集)

    Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 32909Accepted: 10158 Description The polic ...

  8. poj1703 Find them, Catch them(种类并查集

    题目地址:http://poj.org/problem?id=1703 题目大意:警察抓了n个坏蛋,这些坏蛋分别属于龙帮或蛇帮.输入m个语句,A x y询问x和y的关系(在一个帮派,不在,不能确定), ...

  9. POJ1703 Find them Catch them 关于分集合操作的正确性证明 种类并查集

    题目链接:http://poj.org/problem?id=1703 这道题和食物链那道题有异曲同工之处,都是要处理不同集合之间的关系,而并查集的功能是维护相同集合之间的关系.这道题中有两个不同的集 ...

  10. NOI2001|POJ1182食物链[种类并查集 向量]

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65430   Accepted: 19283 Description ...

随机推荐

  1. scrapy+selenium 爬取淘宝商城商品数据存入到mongo中

    1.配置信息 # 设置mongo参数 MONGO_URI = 'localhost' MONGO_DB = 'taobao' # 设置搜索关键字 KEYWORDS=['小米手机','华为手机'] # ...

  2. MySql大小写配置

    新安装mysql5.7版本后,linux环境下默认是大小写敏感的.可以在客户端执行以下命令: SHOW VARIABLES LIKE '%case%' 可以看到 lower_case_table_na ...

  3. CF1208 Red Blue Tree

    题目链接 问题分析 这是蒟蒻第一道3500!不过话说luogu上两个题解的程序都是假的可还行(2019.11.1)-- 为了方便叙述,下面我们约定 : \([c]\) 的值为 \(1\) 当且仅当 \ ...

  4. python中with语句的使用

    引言 with 语句是从 Python 2.5 开始引入的一种与异常处理相关的功能(2.5 版本中要通过 from __future__ import with_statement 导入后才可以使用) ...

  5. ARTS打卡计划第七周

    Algorithms: https://leetcode-cn.com/problems/longest-common-prefix/ Review: https://link.medium.com/ ...

  6. Vue.js 生命周期、计算属性及侦听器

    一.创建一个Vue实例 每个Vue应用都是使用Vue函数创建一个Vue实例.所有的Vue组件都是一个Vue实例,并且接受相同的选项对象(一些根实例特有的选项除外). 数据和方法 当一个实例被创建后,它 ...

  7. Telerik JustDecompile

    Free. For everyone. Forever. With an open source decompilation engine https://www.telerik.com/produc ...

  8. Chrome console不输出内容

    设置成info就好了

  9. 【Taro全实践】修改radio组件的大小

    需求是将radio选中后颜色改为橙色.大小改成合适大小. 1.改颜色 <Radio color='#FF7464'></Radio> 2.改大小 <Radio style ...

  10. C标准库中转换wchar_t和char类型的字符串

    C 库函数 - mbstowcs()  C 标准库 - <stdlib.h> 描述 C 库函数 size_t mbstowcs(schar_t *pwcs, const char *str ...