跳到正文

题解

P9067 [Ynoi Easy Round 2022] 虚空处刑 TEST_105

Genius_Star约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年3月14日
收录
2025年12月12日
更新
2026年11月8日
篇幅
约 6 分钟
获赞
2
题目
P9067

版本

  1. v1当前

或许更好的阅读体验。

思路:

邻域问题相关,考虑维护儿子。

容易发现合法的合并次数只有 O(N)O(N) 种,现在问题是如何快速找到需要合并的点。

设 mpi,jmp_{i, j} 表示 ii 所在的极大连通块内下方颜色为 jj 的联通块序列,使用 map<int, list<int>> mp[N] 维护。

那么当 ax←ya_x \gets y 时,首先对于任意 i∈mpx,yi \in mp_{x, y},将 mpimp_i 与 mpxmp_x 进行启发式合并;然后特判更新 mpfax,ymp_{fa_x, y} 即可;这里我们还需要将初始的 mpfax,axmp_{fa_x, a_x} 中删掉 xx,但是没必要,复杂度是不影响的,合并的时候特判一下即可;最后清空掉 mpx,ymp_{x, y}。

过程中使用并查集维护极大同色连通块,注意对于一个极大同色连通块并查集中以其深度最小的点为根。

对于 list 的拼接,可以使用 splice 函数。

时间复杂度为 O(Nlog⁡N)O(N \log N)。

link

完整代码:

C++
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#define mkp(x, y) make_pair(x, y)
#define Add(x, y) (x + y >= mod) ? (x + y - mod) : (x + y)
#define lowbit(x) x & (-x)
#define pi pair<ll, ll>
#define pii pair<ll, pair<ll, ll>>
#define iip pair<pair<ll, ll>, ll>
#define ppii pair<pair<ll, ll>, pair<ll, ll>>
#define ls(k) k << 1
#define rs(k) k << 1 | 1
#define fi first
#define se second
#define full(l, r, x) for(auto it = l; it != r; ++it) (*it) = x
#define Full(a) memset(a, 0, sizeof(a))
#define open(s1, s2) freopen(s1, "r", stdin), freopen(s2, "w", stdout);
#define For(i, l, r) for(register int i = l; i <= r; ++i)
#define _For(i, l, r) for(register int i = r; i >= l; --i)
using namespace std;
using namespace __gnu_pbds;
typedef __int128 __;
typedef long double lb;
typedef double db;
typedef unsigned long long ull;
typedef long long ll;
const int N = 1e6 + 10;
inline ll read(){
    ll x = 0, f = 1;
    char c = getchar();
    while(c < '0' || c > '9'){
        if(c == '-')
          f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9'){
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    return x * f;
}
inline void write(ll x){
	if(x < 0){
		putchar('-');
		x = -x;
	}
	if(x > 9)
	  write(x / 10);
	putchar(x % 10 + '0');
}
int n, m, op, x, y;
int a[N], fa[N], Fa[N], siz[N];
vector<int> E[N];
map<int, list<int>> mp[N];
inline void add(int u, int v){
	E[u].push_back(v);
}
inline int Find(int x){
	if(x != Fa[x])
	  return Fa[x] = Find(Fa[x]);
	return Fa[x];
}
inline void merge(int x, int y){
	x = Find(x), y = Find(y);
	if(x == y)
	  return ;
	Fa[y] = x;
	siz[x] += siz[y];
}
inline void dfs(int u){
	for(auto v : E[u]){
		if(a[v] == a[u])
		  merge(u, v);
		else
		  mp[Find(u)][a[v]].push_back(v);
		dfs(v);
	}
}
inline void Merge(int x, int y){
	if(mp[x].size() < mp[y].size())
	  swap(mp[x], mp[y]);
	auto t = mp[y];
	for(auto v : t)
	  mp[x][v.fi].splice(mp[x][v.fi].end(), v.se);
	mp[y].clear();
}
int main(){
	n = read(), m = read();
	for(int i = 2; i <= n; ++i){
		fa[i] = read();
		add(fa[i], i);
	}
	for(int i = 1; i <= n; ++i){
		a[i] = read();
		Fa[i] = i;
		siz[i] = 1;
	}
	dfs(1);
	while(m--){
		op = read(), x = read();
		x = Find(x);
		if(op == 1){
			y = read();
			if(a[x] == y)
			  continue;
			auto t = mp[x][y];
			for(auto v : t)
			  if(a[v] == y && Find(v) != x)
			    merge(x, v), Merge(x, v);
			if(fa[x]){
				int g = Find(fa[x]);
				if(a[g] == y)
				  merge(g, x), Merge(g, x);
				else
				  mp[g][y].push_back(x);
			}
			mp[x][y].clear();
			a[x] = y;
		}
		else{
			write(siz[x]);
			putchar('\n');
		}
	}
	return 0;
}
原文
存档评论1 条抓取于