题目不难理解,A判断2人是否属于同一帮派,D确认两人属于不同帮派。于是需要一个数组r[]来判断父亲节点和子节点的关系。具体思路可参考http://blog.csdn.net/freezhanacmore/article/details/8774033

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std; const int maxn=100005;
int f[maxn];
int r[maxn]; int Find_f(int x)
{
if (x != f[x])
{
int t=f[x];
f[x] = Find_f(f[x]);
r[x]=(r[x]+r[t])%2;
return f[x];
}
return f[x];
}
void Union(int x,int y)
{
int fx=Find_f(x);
int fy=Find_f(y);
f[fx]=fy;
r[fx]=(r[x]+1+r[y])%2;
}
int main()
{
//freopen("in.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
{
f[i]=i;
r[i]=0;
}
char c;
int crime1,crime2;
while(m--)
{
getchar();
scanf("%c%d%d",&c,&crime1,&crime2);
//printf("%c\n",c);
if(c=='D')
Union(crime1,crime2);
else if(c=='A')
{
if(Find_f(crime1)==Find_f(crime2))
if(r[crime1]!=r[crime2])
printf("In different gangs.\n");
else
printf("In the same gang.\n");
else
printf("Not sure yet.\n");
}
}
}
return 0;
}

POJ 1703 Find them, Catch them 并查集,还是有点不理解的更多相关文章

  1. POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

    POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...

  2. poj.1703.Find them, Catch them(并查集)

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

  3. POJ 1703 Find them, catch them (并查集)

    题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2  D 3 4 D 5 6...这就至少有3个集合了.并且 ...

  4. POJ 1703 Find them, Catch them 并查集的应用

    题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...

  5. POJ 1703 Find them, Catch them(并查集高级应用)

    手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...

  6. [并查集] POJ 1703 Find them, Catch them

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

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

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

  8. hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them

    http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...

  9. POJ 1703 Find them, Catch them (数据结构-并查集)

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

随机推荐

  1. 2.Python入门-计算机组成、指令和程序、标识符、变量、数据类型、对象和变量关系、运算符

    一.计算机的组成 计算机由两部分组成:硬件 和 软件 硬件包含:键盘.鼠标.显示器.CPU.主板.内存.硬盘 ... -硬件是看的见摸得着的 软件包含:系统软件(windows.macOS.Linux ...

  2. 数据库权限grant

    数据库权限grant 创建授权grant 权限类型(priv_type) 权限类型 代表什么? ALL 所有权限 SELECT 读取内容的权限 INSERT 插入内容的权限 UPDATE 更新内容的权 ...

  3. Ansible_处理失败的任务

    一.Ansible处理任务失败 1.管理play中任务错误 1️⃣:Ansible评估任务的返回代码,从而确定任务是成功还是失败 2️⃣:通常而言,当任务失败时,Ansible将立即在该主机上中止pl ...

  4. IT菜鸟之BIOS和VT

    一.虚拟化:VT(Virtualization Technology) 二.BIOS (basic input output system基本输入输出系统) 主板优先启动--bios启动--bios开 ...

  5. 057.Python前端Django模型ORM多表查询

    一 基于对象的查询 1.1 一对多查询 设计路由 from django.contrib import admin from django.urls import path from app01 im ...

  6. Docker-Compose入门-(转载)

    Compose 是一个用户定义和运行多个容器的 Docker 应用程序.在 Compose 中你可以使用 YAML 文件来配置你的应用服务.然后,只需要一个简单的命令,就可以创建并启动你配置的所有服务 ...

  7. synchronized使用及java中的原子性问题

    1.Synchronized关键字使用 class X { // 修饰非静态方法 synchronized void foo() { // 临界区 } // 修饰静态方法 synchronized s ...

  8. Relay外部库使用

    Relay外部库使用 本文介绍如何将cuDNN或cuBLAS等外部库与Relay一起使用. Relay内部使用TVM生成目标特定的代码.例如,使用cuda后端,TVM为用户提供的网络中的所有层生成cu ...

  9. 计图MPI分布式多卡

    计图MPI分布式多卡 计图分布式基于MPI(Message Passing Interface),主要阐述使用计图MPI,进行多卡和分布式训练.目前计图分布式处于测试阶段. 计图MPI安装 计图依赖O ...

  10. CEVA引入新的可配置传感器集线器DSP架构

    CEVA引入新的可配置传感器集线器DSP架构 CEVA introduces new configurable sensor hub DSP architecture 在一个将多个传感器设计成几乎所有 ...