首页 > 编程语言 >papamelon 349. 城市帮派 Find them, Catch them(挑战程序设计竞赛)

papamelon 349. 城市帮派 Find them, Catch them(挑战程序设计竞赛)

时间:2023-06-10 20:23:00浏览次数:46  
标签:them gangs int find gang 帮派 Catch yet Find

地址 https://www.papamelon.com/problem/349

一个城市里有两个帮派,另外有 N 个成员,成员从 1∼N 进行编号,每个成员来自于其中一个帮派。
给定 M 个信息,每个信息有两种格式:
D a b:a,b(1≤a,b≤N,a≠b) 两个人不是一个帮派的
A a b: 判断 a,b(1≤a,b≤N,a≠b) 两个人是否为同一个帮派
两人同属一个帮派则输出 In the same gang.
不同属一个帮派则输出 In different gangs.
根据现有的消息无法确定则输出 Not sure yet.

输入
第一行整数 T(1≤T≤20),表示有多少组测试数据
每组测试数据格式如下:
第一行两个整数N,M(1≤N,M≤10^5),表示成员的数量,消息的数量
接下来 M 行,每行一条消息,随机给出,格式如上所述
输出
对于每个 A 操作,需要一行结果,可能为 In the same gang.,In different gangs.,Not sure yet.

输入
1
5 5
A 1 2
D 1 2
A 1 2
D 2 4
A 1 4
输出
Not sure yet.
In different gangs.
In the same gang.

解答 并查集
两个帮派成员不是A就是B 不是敌对就是同伙
并查集N 表示帮派A N+1到2N表示帮派B

所以两个成员ab 要么a==b  要么a==b+N  b==a+N
#include <iostream>

using namespace std;

const int N = 200020;
int f[N];
int n, m, t;

void init() {
	for (int i = 0; i < N; i++) { f[i] = i; }
}


int find(int x) {
	if (f[x] != x) { f[x] = find(f[x]); }
	return f[x];
}

void merge(int a, int b) {
	a = find(a); b = find(b);
	if (a < b) {
		f[b] = a;
	}
	else {
		f[a] = b;
	}
}


int main()
{
	scanf("%d",&t);
	while (t--) {
		init();
		scanf("%d%d",&n,&m);

		for (int i = 0; i < m; i++) {
			char op[2]; int a, b;
			scanf("%s%d%d", op, &a, &b);

			if (op[0] == 'A') {
				a = find(a); b = find(b);
				int aN = find(a + n); int bN = find(b + n);
				if (a == b) {
					printf("In the same gang.\n");
				}
				else if (a == bN || b == aN) {
					printf("In different gangs.\n");
				}
				else {
					printf("Not sure yet.\n");
				}
			}
			else {
				merge(a,b+n);
				merge(b, a + n);
			}
		}
	}

	return 0;
}

视频题解空间

标签:them,gangs,int,find,gang,帮派,Catch,yet,Find
From: https://www.cnblogs.com/itdef/p/17471879.html

相关文章

  • ubuntu find whereis locate find
     which只能寻找执行文件,并在PATH变量里面寻找。whereis从linux文件数据库(/var/lib/slocate/slocate.db)寻找,所以有可能找到刚刚删除,或者没有发现新建的文件。locate同上,不过文件名是部分匹配。find是直接在硬盘上搜寻,功能强大,但耗硬盘,一般不要用。......
  • 用Mathematica和SciPy阐明Jacobi椭圆函数的定义方法
    这,这个,那,那个Jacobi椭圆函数SN和CN类似于三角函数正弦和余弦。它们出现在非线性振动和保形映射等应用中。不幸的是,定义这些函数有多种约定。这篇文章的目的是澄清围绕这些不同公约的混淆。上面的图像是函数sn[1]的一个图。模量、参数和模数角Jacobi函数有两个输入。我们通常认为Jac......
  • Vue学习笔记之Vue项目启动gyp ERR! find Python
    0x00报错详细该报错在Windows和MacOS平台均会出现,项目启动时候报错如下:E:\vue-admin\node_modules\fibers>ifnotdefinednpm_config_node_gyp(node"D:\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.......
  • [ERROR] Can't find error-message file '/data/mysql/share/errmsg.sys'. Check erro
    1.MySQL5.7.21启动时报错:[ERROR]Can'tfinderror-messagefile'/data/mysql/3307/share/errmsg.sys'.Checkerror-messagefilelocationand'lc-messages-dir'configurationdirective.2.登录MySQL查看系统全局参数:mysql>showglobalvariablesl......
  • windows 10 wsl 环境 docker 无法正常启动 -The system cannot find the file specif
    错误信息:errorduringconnect:inthedefaultdaemonconfigurationonWindows,thedockerclientmustberunwithelevatedprivilegestoconnect:Get"http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.24/containers/json":open//./pipe/docker_engine:Thesy......
  • Re: finding all cycles in a graph
    ref:https://cs.stackexchange.com/questions/7216/find-the-simple-cycles-in-a-directed-graphRe:findingallcyclesinagraphFrom: JuanPabloCarbajalSubject: Re:findingallcyclesinagraphDate: Wed,25Jan201219:43:48+0100OnWed,Jan25,2012a......
  • kanzi的安卓工程报错解决办法:Error: Could not find or access Kanzi's Gradle plugin
    这是因为安卓里配置的环境变量不对。需要检查下述文件的路径是否真实存在,以及和使用的版本是否匹配  ......
  • QA|重写了元素定位后报错xx object has no attribute 'find_element'|网页计算器自动
    代码如下:1#basepage.py23fromseleniumimportwebdriver456classBasePage():7"""8基类用作初始化封装常用操作9"""1011def__init__(self):12"""13初始化driver14......
  • Python正则表达式学习(5)——re.findall()
    re.findall(pattern,string,flags=0)返回字符串中模式的所有非重叠匹配,作为字符串列表。字符串从左到右扫描,并按照找到的顺序返回匹配项。如果模式中存在一个或多个组,则返回组的列表;如果模式有多个组,这将是一个元组的列表。结果中包含空匹配,除非他们触及另一个匹配的开始。In[1......
  • mysql 多值检索 find_in_set()函数
    问题描述:有一个字段type类型,存储的值为:1,2,3,4,等这样的,要检索出里面全部含有某一个类型的值,列如3想要的结果如下:如何实现。。下面是具体的示例:+-----+-----------+|fid|type|+-----+-----------+|1|1,2,3,4,5||4|2,3,4,5|+-----+-----------+2rowsins......