跳到正文

题解

题解:AT_agc016_d [AGC016D] XOR Replace

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

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 6 分钟

版本

  1. v1当前

考虑这个操作有什么性质。注意到一个数被替换成所有数的 xor⁡\operatorname{xor} 后,所有数的 xor⁡\operatorname{xor} 即为原来所有数的 xor⁡\operatorname{xor} 和原来除了这个被替换掉的数的 xor⁡\operatorname{xor},也就是这个数的 xor⁡\operatorname{xor}。

人话:一个数被替换以后下一个替换的数就是这个原来的数。

这样已经可以把无解的情况给判掉了。考虑最小操作数。

考虑将原图看作一张图。边就是 bi→aib_i \rightarrow a_i(ai≠bia_i \neq b_i)。每一次可以从一个点到另一个点。考虑什么情况下是一个符合要求的。

注意到每条边至少被走一次。从一个连通块到另一个连通块至少多走一次。所以答案至少是边数加连通块数减一。

同时注意到有解的情况下,每个连通块:

  • 要么长成可以分解为环路的图,此时直接沿着环一个一个换即可。
  • 要么长成可以一笔画但无法变成环路的图。此时出发点一定是一笔画的出发点,沿着该路径一个一个换即可。

所以答案就是边数加连通块数减一。


C++
#include <bits/stdc++.h>
using namespace std;

struct FastIO {
	inline FastIO& operator >> (short& x) {
		x = 0;
		char f = 0, ch = getchar();
		while (ch > '9' || ch < '0') f |= (ch == '-'), ch = getchar();
		while (ch <= '9' && ch >= '0') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
		return x = (f ? -x : x), *this;
	}
	inline FastIO& operator >> (int& x) {
		x = 0;
		char f = 0, ch = getchar();
		while (ch > '9' || ch < '0') f |= (ch == '-'), ch = getchar();
		while (ch <= '9' && ch >= '0') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
		return x = (f ? -x : x), *this;
	}
	inline FastIO& operator >> (long long& x) {
		x = 0;
		char f = 0, ch = getchar();
		while (ch > '9' || ch < '0') f |= (ch == '-'), ch = getchar();
		while (ch <= '9' && ch >= '0') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
		return x = (f ? -x : x), *this;
	}
	inline FastIO& operator >> (double& x) {
		x = 0;
		char f = 0, ch = getchar();
		double d = 0.1;
		while (ch > '9' || ch < '0') f |= (ch == '-'), ch = getchar();
		while (ch <= '9' && ch >= '0') x = x * 10 + (ch ^ 48), ch = getchar();
		if (ch == '.') {
			ch = getchar();
			while (ch <= '9' && ch >= '0') x += d * (ch ^ 48), d *= 0.1, ch = getchar();
		}
		return x = (f ? -x : x), *this;
	}
} rin;

#define ll long long
#define uint unsigned int
#define reg register
#define ld long double
#define uint unsigned int
#define ull unsigned long long
#define qint const int&
#define qll const ll&
#define quint cosnt uint&
#define qull const ull&
#define endl "\n"

inline void qmod(int& x, const int& mod) {
	x = x >= mod? x - mod : x;
}

const int N = 1e5 + 10;
int n, a[N], b[N], s, cnt, flag;
map<int, int> fa, c;
inline int getfa(int x) {
	return !fa.count(x) || fa[x] == x? x : fa[x] = getfa(fa[x]);
}
inline void merge(int x, int y) {
	x = getfa(x), y = getfa(y);
	if (x != y) fa[x] = y;
}

signed main() {
	rin >> n;
	for (int i = 1; i <= n; ++i) rin >> a[i], s ^= a[i], ++c[a[i]];
	for (int i = 1; i <= n; ++i) rin >> b[i], cnt += a[i] != b[i], --c[b[i]];
	for (int i = 1; i <= n; ++i) if (a[i] != b[i]) fa[a[i]] = a[i], fa[b[i]] = b[i];
	fa[s] = s, ++c[s], --cnt;
	for (int i = 1; i <= n; ++i) merge(a[i], b[i]);
	for (auto p : fa) cnt += p.first == p.second;
	for (auto p : c) flag += abs(p.second);
	if (flag > 1) cout << -1 << endl;
	else cout << cnt << endl;
	return 0;
}
原文