玄两关--求条
查看原帖
玄两关--求条
902351
Little_x_starTYJ楼主2024/9/10 17:24

只过了 #6

#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[5000010], degree[5000010], b[5000010], id2;
inline void toPrufer() {
	int k = n;
	for (int i = 1; i <= n && k > 2; i++) {
		int t = i;
		while (degree[t] == 1 && k > 2 && t != n) {
			k--;
			b[++id2] = a[t];
			degree[t] = 0;
			degree[a[t]]--;
			t = a[t];
		}
	}
}
inline void toFather() {
	for (int i = 1, k = 1; i < n; i++, k++) {
		while (degree[k]) k++;
		b[k] = a[i];
		while (i < n - 1 && !--degree[a[i]] && a[i] < k) {
			b[a[i]] = a[i + 1];
			i++;
		}
	}
	id2 = n - 1;
}
inline int calc() {
	int ans = 0;
	for (int i = 1; i <= id2; i++) {
		ans = ans ^ (i * b[i]);
	}
	return ans;
}
signed main() {
	ios::sync_with_stdio(false);
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> m;
	if (m == 1) {
		for (int i = 1; i < n; i++)
			cin >> a[i], degree[a[i]]++, degree[i]++;
		toPrufer();
		
	} else {
		for (int i = 1; i < n - 1; i++)
			cin >> a[i], degree[a[i]]++;
		a[n - 1] = n;
		toFather();
	}
	cout << calc();
	return 0;
}
2024/9/10 17:24
加载中...