[賽記] 多校A層衝刺NOIP2024模擬賽01【衡中】

Peppa_Even_Pig發表於2024-10-05

構造字串 50pts

錯解50pts;

考慮正解,對於題目中的要求,我們可以轉換成若干個相等與不等的操作,若相等則用並查集合並一下,不等則連邊,若同塊連邊則無解,否則從前往後遍歷賦值,每次找所連邊其它塊值的 $ \operatorname{mex} $ 即可;

時間複雜度:$ \Theta(nm \alpha(n)) $;

點選檢視程式碼
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
int n, m;
int fa[5005];
int find(int x) {
	if (x != fa[x]) fa[x] = find(fa[x]);
	return fa[x];
}
struct sss{
	int x, y, z;
}e[5005];
int cnt;
vector<int> v[5005];
int ans[5005];
bool vis[5005];
int main() {
	freopen("str.in", "r", stdin);
	freopen("str.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++) fa[i] = i;
	int cnt = m;
	memset(ans, -1, sizeof(ans));
	for (int i = 1; i <= m; i++) {
		cin >> e[i].x >> e[i].y >> e[i].z;
		if (e[i].x > e[i].y) swap(e[i].x, e[i].y);
		if (e[i].z != 0) {
			int l = e[i].x;
			int r = e[i].y;
			int o = e[i].z;
			while(o) {
				fa[find(l)] = find(r);
				l++;
				r++;
				o--;
			}
			if (r <= n) {
				e[++cnt] = {l, r, 0};
			}
		} else {
			e[++cnt] = e[i];
		}
	}
	for (int i = m + 1; i <= cnt; i++) {
		int l = e[i].x;
		int r = e[i].y;
		if (find(l) == find(r)) {
			cout << -1;
			return 0;
		}
		v[find(l)].push_back(find(r));
		v[find(r)].push_back(find(l));
	}
	for (int i = 1; i <= n; i++) {
		int x = find(i);
		if (ans[x] != -1) continue;
		for (int j = 0; j <= n; j++) vis[j] = false;
		for (int j = 0; j < v[x].size(); j++) {
			if (ans[v[x][j]] != -1) vis[ans[v[x][j]]] = true;
		}
		for (int j = 0; j <= n; j++) {
			if (!vis[j]) {
				ans[x] = j;
				break;
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		cout << ans[find(i)] << ' ';
	}
	return 0;
}

相關文章