题目链接: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. 【Android-代码破解】代码破解步骤

    一.准备工具 准备要破解的apk 下载dex2jar 下载jd-gui 下载apk-tool 二.反编译apk得到Java源代码 (dex2jar是将apk中的classes.dex转化成Jar文件, ...

  2. DNS服务基础

    DNS服务器的功能 – 正向解析:根据注册的域名查找其对应的IP地址 – 反向解析:根据IP地址查找对应的注册域名(不常用) NS(声明DNS记录) A(正向解析记录) CNAME(解析记录别名) 安 ...

  3. laravel各种请求类

    curl请求类 composer require php-curl-class/php-curl-class

  4. Java线程之创建线程

    翻译自:https://www.journaldev.com/1016/java-thread-example 进程 进程是一个自包含的执行环境,它可以被看成一个程序或应用程序.然而一个应用程序本身包 ...

  5. 2017 ZSTU寒假排位赛 #7

    题目链接:https://vjudge.net/contest/149498#overview. A题,水题,直接按照题意模拟一下即可. B题,我用的是线段树.大力用的差分标记(上次听zy说过,下次再 ...

  6. 第十四周课程总结&实验报告(简单记事本的实现)

    1.JDBC概述 JDBC提供了一种与平台无关的用于执行SQL语句的标准JavaAPI,可以方便的实现多种关系型数据库的统一操作,它由一组用Java语言编写的类和接口组成 主要常用操作类与接口: ja ...

  7. python操作s3 -- boto2.x

    以下是python操作s3常用方法: boto s3手册:http://boto.readthedocs.org/en/latest/ref/s3.html boto s3快速入门:http://bo ...

  8. HearthBuddy DONOTDELETE.bin

    namespace Hearthbuddy{    // Token: 0x02000022 RID: 34    public class App : System.Windows.Applicat ...

  9. oracle获取当前月的第一个星期五

    -to_char(trunc(sysdate,'month'),'D') end || '号是当月的第一个星期五' from dual;

  10. Winform运行外部控制台程序,并在程序结束后执行其他动作

    ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = @"程序名"; psi.Arguments = @&qu ...