POJ 1703 Find them, Catch them 并查集,还是有点不理解
题目不难理解,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 并查集,还是有点不理解的更多相关文章
- POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集
POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...
- poj.1703.Find them, Catch them(并查集)
Find them, Catch them Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- POJ 1703 Find them, catch them (并查集)
题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2 D 3 4 D 5 6...这就至少有3个集合了.并且 ...
- POJ 1703 Find them, Catch them 并查集的应用
题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...
- POJ 1703 Find them, Catch them(并查集高级应用)
手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...
- [并查集] POJ 1703 Find them, Catch them
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: ...
- POJ 1703 Find them, Catch them(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41463 Accepted: ...
- 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条臭 ...
- POJ 1703 Find them, Catch them (数据结构-并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31102 Accepted: ...
随机推荐
- HUAWEI防火墙通过L2TP隧道让外出员工访问公司内网的各种资源
组网图形 组网需求 企业网络如图所示,企业希望公司外的移动办公用户能够通过L2TP VPN隧道访问公司内网的各种资源. 操作步骤 配置LNS. 1.配置接口IP地址,并将接口加入安全区域. <L ...
- ThinkPHP 全局异常处理
wqy的笔记:http://www.upwqy.com/details/273.html 在thinkphp6 和 thinkphp5 全局异常处理 稍有不同 ThinkPHP6 在 tp6 中 框架 ...
- properties模板
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/ssmdemo1?useSSL=true&useUn ...
- Step By Step(Lua字符串库)
Step By Step(Lua字符串库) 1. 基础字符串函数: 字符串库中有一些函数非常简单,如: 1). string.len(s) 返回字符串s的长度: 2). string ...
- python3操作Kafka
# -- coding: UTF-8 import datetime import json import time from kafka import KafkaProducer producer= ...
- CVPR 2020几篇论文内容点评:目标检测跟踪,人脸表情识别,姿态估计,实例分割等
CVPR 2020几篇论文内容点评:目标检测跟踪,人脸表情识别,姿态估计,实例分割等 CVPR 2020中选论文放榜后,最新开源项目合集也来了. 本届CPVR共接收6656篇论文,中选1470篇,&q ...
- 嵌入式Linux设备驱动程序:在运行时读取驱动程序状态
嵌入式Linux设备驱动程序:在运行时读取驱动程序状态 Embedded Linux device drivers: Reading driver state at runtime 在运行时了解驱动程 ...
- eclipse左边的工程列表窗口不见了解决方案
解决eclipse左边的工程列表窗口看不到工程目录的方法: Window->Show View->Project Explorer(如果没有Project Explorer选项,则Wind ...
- 重新整理 .net core 实践篇—————日志系统之作用域[十七]
前言 前面介绍了服务与日志之间的配置,那么我们服务会遇到下面的场景会被遇到一些打log的问题. 前面我提及到我们的log,其实是在一个队列里面,而我们的请求是在并发的,多个用户同时发送请求这个时候我们 ...
- jvm调优的几种场景
假定你已经了解了运行时的数据区域和常用的垃圾回收算法,也了解了Hotspot支持的垃圾回收器. 一.cpu占用过高 cpu占用过高要分情况讨论,是不是业务上在搞活动,突然有大批的流量进来,而且活动结束 ...