【POJ】Find them, Catch them

【题解-POJ】1703 Find them, Catch them WA,还在找BUG😩。 阅读详情

 

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 62168 Accepted: 18835

Description

The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is, given two criminals; do they belong to a same clan? You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.)

Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:

1. D [a] [b]
where [a] and [b] are the numbers of two criminals, and they belong to different gangs.

2. A [a] [b]
where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.

Input

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case begins with a line with two integers N and M, followed by M lines each containing one message as described above.

Output

For each message "A [a] [b]" in each case, your program should give the judgment based on the information got before. The answers might be one of "In the same gang.", "In different gangs." and "Not sure yet."

Sample Input

1
5 5
A 1 2
D 1 2
A 1 2
D 2 4
A 1 4

Sample Output

Not sure yet.
In different gangs.
In the same gang.

Source

POJ Monthly--2004.07.18

【思路】

  使用并查集。但是并查集内相同父节点认为是一个集合内,这道题的题意是给我们一对数,这对数不在一个集合内。

  例子:

  A B

  B C

  A 和 B 不在一个集合内, B 和 C 不在一个集合内, 可以推导出A和C在一个集合内。如果我们直接使用并查集的话。A,B,C

  回使A,B,C 在相同的集合内。这样就找不到A,B,C的关系了。

  解决办法是:

  A 和 B + n 在一个集合内

  B 和 A + n 在一个集合内

  C 和 B + n 在一个集合内

  B 和 C + n 在一个集合内

  这样 查询 A 和 B 是否在一个集合的时候,我们使用 find(A, B+n) 和 find(A + n, B) 

  在判断 A 和 C 是否一个集合的时候, 我们通过 A 和 B + n 和 C 和 B + n 推导出 A 和 C 在一个团伙里。

【代码】

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 2e5 + 10;
int s[N];
int t, n, m;
char c;
void init() {
    memset(s, 0, sizeof(s));
    for(int i = 1; i <= n * 2; i++) {
        s[i] = i;
    }
}
int find(int x) {
    if(s[x] != x) s[x] = find(s[x]);

    return s[x];
}
void union_set(int x, int y) {
    x = find(x);
    y = find(y);
    if(x != y ) s[x] = s[y];
}

bool same(int x, int y ) {
    return find(x) == find(y);
}
int main() {
    cin >> t;
    while(t--) {
        scanf("%d%d", &n, &m);
        init();

        getchar();
        for(int i = 0; i < m; i++) {
            int x, y;
            scanf("%c %d %d", &c, &x, &y);
            getchar();
            if(c == 'A') {
                if(n == 2) {
                    printf("In different gangs.\n");
                } else if( same(x, y) ) {
                    printf("In the same gang.\n");
                } else if( same(x, y + n) || same(y, x + n)) {
                    printf("In different gangs.\n");
                } else {
                    puts("Not sure yet.");
                }
            } else {
                union_set(x, y + n);
                union_set(y, x + n);
            }
        }
    }
    return 0;
}

 

poj1703 Find them, Catch them 这是题目: 3:Find them, Catch them 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, G 阅读详情

相关推荐

Android 12 HAL开发避坑实录:手把手教你用AIDL在Native层实现一个可开机自启动的Service

本文详细介绍了在Android 12 HAL开发中,如何通过AIDL在Native层实现一个可开机自启动的Service。从环境准备、AIDL接口定义、服务实现到VINTF集成与SELinux策略配置,提供了完整的实战指南,帮助开发者避开常见陷阱,提升开发效率。

weixin_28314575的博客 321

Find them, Catch them POJ - 1703 (带权并查集)

The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is, giv.

Jin_Yanhui 276

Ollama安装教程

Ollama 是一个开源的大语言模型(LLM)部署工具,专为在本地机器部署和运行大模型而设计,通过简单的命令就可以在本地快速运行大模型(如DeepSeek、Llama、Gemma、QWQ等)。非常适合开发者、研究人员以及对数据安全和隐私有较高要求的企业和用户

学习AI应用开发,分享AI干货、获取最新AI咨询 3852

Find them, Catch them

The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which ga

Techay的博客 582

基于单片机的宠物狗智能项圈设计

GPS定位功能:利用GPS模块实时获取宠物的位置,帮助主人追踪宠物的活动轨迹。活动监测:使用加速度传感器监测宠物的活动情况,如运动、休息等,并进行分析。健康监测:集成心率传感器,实时监控宠物的心率,以确保其健康。声音识别功能:通过麦克风捕捉宠物的叫声,检测宠物的状态(如饥饿、疼痛等)。蓝牙/Wi-Fi通信:通过蓝牙或Wi-Fi模块与手机APP或云端平台进行数据交互,实时反馈宠物的状态信息。电池管理:实现低功耗设计,确保设备的续航能力。

2201_76125393的博客 2281

C++多线程 task--std::promise和std::future

本文主要讲解C++11/14/17中所提供的应用于多线程编程中的Task机制。Task可由如下几部分取得而本文主要讲解std::promise和std::future相关的内容。std::promise和std::future通过一个数据管道进行交流,可以将std::promise看作生产者,std::future看作消费者,其两者形成如下的方式交互std::promise本身是一个模版类,通过std::promise可以像数据管道中设置value,异常和相应的通知信息。std::promise提供了如下接

qls315的博客 1818

poj 1073 Find them, Catch them

Find them, Catch them Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the poli...

海马有力量的博客 301

STM32串口控制LED灯的亮灭

STM32中的串口控制LED灯的亮灭,分为两种方式,一种是直接发送数字0和1来控制灯的亮灭,另一种是通过发送字符串来控制。 我所使用的开发板主控芯片是STM32F401RET6,主频84MHz, 首先要进行串口的初始化: /* 函数功能:串口的初始化 函数名:Usart_Init 返回值:无 参数:u32 brr 备注: USART1_TXD->PA9 USART1_RXD->PA10 */ void Usart_Init(u32 brr) { float div; u16 div

博客 2万+

解题报告:POJ_1703Find them, Catch them 并查集

Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to i

Hee的博客~ 475

uniapp:详细ios本地离线打包完整流程,使用XCode苹果本地离线打包APP,UNIAPP IOS离线打包并上传到上传到App Store应用商店(mac环境 Uniapp iOS本地离线打包)

uniapp ios 本地离线打包(攻略),iOS App打包发布上架,uni-app项目离线打包之iOS,uni-app实现XCode苹果本地离线打包APP,UNIAPP之IOS离线打包,iOS开发打包ipa,uniapp 打包 IOS,uniapp ios app离线打包配置,uniapp打包离线App(ios),uniapp ios打包详细步骤,最新版iOS Xcode如何证书配置和ipa打包(附图文教程)uniapp申请ios证书,uniapp iOS打包,证书申请,XCode打包IOS应用发布Ap

✅ 精选推荐文章 4506

【AI智能体】Dify 搭建发票识别助手操作实战详解

Dify 搭建发票识别专属助手操作实战详解

congge 5549

并查集

poj 1703  Find them,Catch them Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. Ho

lemon_boluo的博客 312

RH850从0搭建Autosar开发环境【3X】- Davinci Configurator之Port模块配置详解(MCAL配置)

RH850从0搭建Autosar开发环境【3X】- Davinci Configurator之Port模块配置详解(MCAL配置)

weixin_42412049的博客 2277

【并查集】POJ 1703 Find them, Catch them

POJ 1703 : Find them, Catch them 有两大帮派,现在不知道每个人的帮派情况。有两种操作,D x y 表示x和y在不同帮派,A x y查询x和y的帮派情况。 如果在同一帮派,输出In the same gang. 如果不在同一帮派,输出In different gangs. 不确定帮派情况则输出Not sure yet. 这里是POJ 11...

小酷Miki的专栏 229

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

POJ - 1703 Find them, Catch them (并查集)

                  POJ - 1703  Find them, Catch them (并查集)          这道题与食物链那一道题是同一类型的,但稍微简单些,题意就不说了                   比普通的并查集多了一个relation [i]数组,记录与父亲节点的关系,在并查集里的元素都是已经它们之间的关系的,在路径压缩与集合合并时不断的更新relation数...

GYH0730的博客 443

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

poj 1703 Find them, Catch them(带权并查集)

题目链接:poj 1703 Find them, Catch them 题目大意:在城市中有两个大的帮派,Gang Dragon 和Gang Snake,先在有n个人被抓了,警察队他们进行m次操作,操作分为两种,询问A a b, 要求判断a和b是不是属于同一个团伙,D a b 表示说a b不属于同一个团伙。 解题思路:带权并查集的简单版,权值为1表示和前一个人不属于同一个团

不慌不忙、不急不躁 1111

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

POJ Find them, Catch them

Language: Default Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33726   Accepted: 10411 Description The police office in Tadu Cit

走一步再走一步 1553

【POJ 1703】 Find them, Catch them(关系并查集)

【POJ 1703】 Find them, Catch them(关系并查集) Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38951   Accepted: 11987 Description The police office in Tadu City

ACMer' 1309

POJ 1703: Find them, Catch them

非常基础的一道在查询时需要传递其它信息的并查集, 这方面的经典题目首推 POJ 1182 食物链 鉴于博客里没有这种类型的题目就放上来了。 写合并的时候还错误的把两个节点间的距离写成两个根节点之间的距离了, 智商捉急得简直不能多说 = =||| 题目链接:http://poj.org/problem?id=1703 题意: 有一些人分属两个帮派。 每个帮派至少有一个

frog的小池塘 952

poj1703 Find them Catch them

http://www.elijahqi.win/2018/01/12/poj1703-find-them-catch-them/ ‎ Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city

Elijahqi 385

Find them, Catch them POJ - 1703

The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which g...

songziyuan_的博客 604

POJ 1703 Find them, Catch them

Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24693   Accepted: 7418 Description The police office in Tadu City decides to say ends to

ACM2272902662的专栏 881

并查集:POJ No1703 Find them, Catch them

并查集:POJ No1703 Find them, Catch them 题目链接:http://poj.org/problem?id=1703 题意:两个坏蛋属于不同的组织,给出两个坏蛋判定是否一个组织。 题解:已知每次输入的两个帮派人员 x, y; 合并 (x, y + N), (x + N, y)。判定时,如果 (x, y) 属于同一个 ...

豆子 149

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

题目地址:POJ 1703 种类并查集水题。 代码如下: #include #include #include #include #include #include #include #include #include #include #include using namespace std; int bin[600000], rank[600000]; int f

scf0920 1281

POJ 1703——Find them, Catch them【并查集变形】

题目传送门 Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs ...

NIRVANA REBIRTH 249

POJ 1703 Find them,Catch them

#include<iostream> #include<cstdio> #include<cstring> const int MAXN=200020; int n,m; int set[MAXN]; int set_find(int d) { if(set[d]<0) return d; else retu...

flymoyu的博客 273
上一篇: ACM/IOI 历年国家集训队论文集和论文算法分类整理
下一篇: [洛古]P1044 栈
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值