题目不难理解,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. BRAM 和 DRAM 区别

    转载: BRAM和DRAM的区别 Xilinx的FPGA开发板可以直接调用RAM,其中包括了BRAM和DRAM.经过网上查找资料发现,这两者的区别在于: 选择distributed memory ge ...

  2. Zabbix 常用术语

    Zabbix 常用术语 1.主机(host) 一台你想监控的网络设备,用IP或域名表示 2.主机组(host group) 主机的逻辑组;它包含主机和模板.-个主机组里的主机和模板之间并没有任何直接的 ...

  3. TCP/IP协议三次握手_四次挥手

    TCP/IP协议 TCP是一种面向连接的端到端的可靠传输协议. TCP报头格式 三次握手的过程 一.客户端发送一个连接请求,发送一个随机数X,这时客户端的端口状态变为SYN_SENT状态. 二.服务端 ...

  4. 1.5linux用户权限相关命令

    用户权限相关命令 目标 用户 和 权限 的基本概念 用户管理 终端命令 组管理 终端命令 修改权限 终端命令 01. 用户 和 权限 的基本概念 1.1 基本概念 用户 是 Linux 系统工作中重要 ...

  5. EasyUI系列—点击按钮加载tabs_day26

    我们先来看下效果图 1.为div添加点击事件(也可使用jQuery绑定事件) 1 <div id="mm2" style="width:100px;"&g ...

  6. Nacos源码结构和AP模式注册中心实现介绍

    前言 NacosAP模式源码分析目录 微服务下的注册中心如何选择 Nacos使用和注册部分源码介绍 Nacos服务心跳和健康检查源码介绍 Nacos服务发现 Nacos源码结构介绍 Nacos版本基于 ...

  7. 快速上手 Linkerd v2 Service Mesh(服务网格)

    在本指南中,我们将引导您了解如何将 Linkerd 安装到您的 Kubernetes 集群中. 然后我们将部署一个示例应用程序来展示 Linkerd 的功能. 安装 Linkerd 很容易.首先,您将 ...

  8. Linux基础_vim命令

    简介:Vim是从 vi 发展出来的一个文本编辑器.代码补完.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用. vi/vim 共分为三种模式,分别是命令模式(Command mode)也叫 ...

  9. 编译原理-DFA与正规式的转化

  10. 【SQLite】教程03-SQLite语法

    注释: sqlite>.help -- 这是一个简单的注释 SQLite ANALYZE 语句: 收集有关表和索引的统计信息,并将收集的信息存储在数据库的内部表中 ANALYZE; or ANA ...