跳到正文

题解

AT_soundhound2018_summer_final_d 解题报告

yuchanxianqwq约 7 分钟
原文
文章信息与版本

文章信息

收录
2026年7月17日
更新
2026年7月22日
篇幅
约 7 分钟
获赞
5

版本

  1. v1当前

前言

这个东西是不是比较容易。看到题面已经可以往并查集、启发式合并方面想了。

析

我们把一操作叫做“合并”,二操作叫做“坍缩”,完全导出子图叫做“团”。

并查集维护连通性,没的说。

然后我们解决坍缩操作。

我们考虑在并查集上戳标记

首先只能采用启发式合并。不这样就不仅仅是编码复杂度的问题了。

这样我们会认为这题看似非常可做,但是如果戳过标记,又有新的集合并进来,需要使用时间戳,维护起来其实很炸。

有没有使用隐式时间戳的方式呢?

其实有的。先挖个坑,后面再讲它为何是“隐式时间戳”方法。我们对每一个连通块维护一个不定长数组(即 vector <int>)。它的代表元(老老老祖宗)就是数组的头。同时,对每一个元素维护它的所属集合,其中处在的位置,它还是代表元时团的最终大小。

于是合并时就不仅启发式合并并查集,还要启发式合并不定长数组,同时对小的(被兼并的)数组中元素运维这三个辅助量。别忘了把边塞进哈希表里统计是否未经坍缩直接相连。

坍缩时就对数组头的起始团大小进行更改即可。

查询时先访问哈希表,排除不在一个团中的情况。

考虑那些点曾作为代表元(数组头)被坍缩以同时覆盖 xx 与 yy。发现只可能是 xx 与 yy 或它们的祖先。于是可以暴力蹦跳,逐一考察。考察的标准是要在不定长数组中处于 xx 与 yy 的前边,且可以同时覆盖 xx 和 yy 的点。实现时不需要任何形式的 LCA,因为启发式合并时只有两棵树深度平衡才会增加 1,这伴随着树大小的翻倍,无疑是迅速的。事实上,并查集森林的最大深度为 log⁡2n\log_2 n 级别。正确性上也没有丝毫弊病:只有 LCA 到根链上的点才可能计入答案。

这样做为什么对呢?(填坑)。我们在使用不定长数组的时候,并没有对任何操作进行延迟下放,加点、坍缩都是即时的。一个点的祖宗永远会处在集合的更前面。也就是说,集合的下标就是时间戳。

码

::::correct[AC]

C++
#include<bits/stdc++.h>
#define int long long
#define y1 qht_yjx
#define hash ZhaoAk
#define maxn 200005
#define endl "\n"
#define nullptr 0
#define I ios::sync_with_stdio (nullptr);
#define AK cin.tie (nullptr);
#define CSP cout.tie (nullptr);


using namespace std;


int lmt = 200000;


struct BCJ
{
	int fa[maxn] ,siz[maxn];
	
	void init ()
	{
		for (int i = 1 ;i <= lmt ;i ++)
			fa[i] = i ,siz[i] = 1;
		return ;
	}
	
	int find (int x)
	{
		return (fa[x] == x) ? x : find (fa[x]);
	}
	
//	void merge (int x ,int y)
//	{
//		int fx = find (x) ,fy = find (y);
//		if (fx != fy)
//			fa[fx] = fy;
//		return ;
//	}

	vector <int> jiu_zhu (int x) // 旧主,以前的代表元
	{
		vector <int> res;
		while (x != fa[x])
		{
			x = fa[x];
			res.push_back (x);
		}
		return res;
	}

} s;


map <int ,int> mp[maxn];


int n ,q;


vector <int> vec[maxn];


int dqh[maxn] ,bel[maxn] ,pos[maxn];


signed main()
{
	I AK CSP

	freopen ("internet.in" ,"r" ,stdin); // 模拟赛的文件 IO
	freopen ("internet.out" ,"w" ,stdout);

	cin >> n >> q;
	s.init ();
	for (int i = 1 ;i <= n ;i ++) // 初始化
	{
		vec[i].push_back (i);
		dqh[i] = 1;
		bel[i] = i;
		pos[i] = 0;
	}
	while (q --)
	{
		int opt ,x ,y;
		cin >> opt >> x >> y;
		if (opt == 1)
		{
			mp[x][y] = mp[y][x] = 1;
			int fx = s.find (x) ,fy = s.find (y);
			if (fx == fy)
				continue ;
			if (s.siz[fx] < s.siz[fy]) // 启发式
				swap (fx ,fy);
			for (int i = 0 ;i < vec[fy].size () ;i ++)
			{
				vec[fx].push_back (vec[fy][i]);
				bel[vec[fy][i]] = fx;
				pos[vec[fy][i]] = vec[fx].size () - 1; // 随即更新
			}
			vec[fy].clear ();
			s.fa[fy] = fx;
            s.siz[fx] += s.siz[fy];
            s.siz[fy] = 0;
		}
		if (opt == 2)
		{
			int fx = s.find (x);
			dqh[fx] = vec[fx].size (); // 只更新团大小,当前弧状物
		}
		if (opt == 3)
		{
			if (mp[x].find(y) != mp[x].end())
			{
				cout << "Yes" << endl;
				continue ;
			}
			if (s.find (x) == s.find (y))
			{
				if ((pos[x] + dqh[x] - 1 >= pos[y] && pos[y] > pos[x]) || (pos[y] + dqh[y] - 1 >= pos[x] && pos[x] > pos[y])) // 老资历,能管辖
				{
					cout << "Yes" << endl;
					continue ;
				}
				vector <int> awa = s.jiu_zhu (x);
				for (int i = 0 ;i < awa.size () ;i ++)
				{
					int AWA = awa[i];
					if (pos[AWA] + dqh[AWA] - 1 >= pos[y] && pos[AWA] + dqh[AWA] - 1 >= pos[x] && pos[AWA] < pos[y] && pos[AWA] < pos[x]) // 老祖宗,能管辖
					{
						cout << "Yes" << endl;
						goto end;
					}
				}
				vector <int> qwq = s.jiu_zhu (y);
				for (int i = 0 ;i < qwq.size () ;i ++)
				{
					int QWQ = qwq[i];
					if (pos[QWQ] + dqh[QWQ] - 1 >= pos[y] && pos[QWQ] + dqh[QWQ] - 1 >= pos[x] && pos[QWQ] < pos[y] && pos[QWQ] < pos[x])
					{
						cout << "Yes" << endl;
						goto end;
					}
				}
			}
			cout << "No" << endl;
			end:;
		}
	}

	return !!!!! ("ShZhao" && "SHzhao");
}
//Code by Lyyq.
//wage.

预期时间复杂度 O(nlog⁡n+qlog⁡n)O(n \log n + q \log n)。