QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#392967#6317. XOR Tree PathGiga_Cronos#WA 1ms5724kbC++231.3kb2024-04-18 00:38:462024-04-18 00:38:47

Judging History

你现在查看的是最新测评结果

  • [2024-04-18 00:38:47]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5724kb
  • [2024-04-18 00:38:46]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAXN ((ll)(1e5 + 5))

int a[MAXN];
vector<int> g[MAXN];
int dp[MAXN][2];

void dfs(int u, int p) {
	int sum = 0;
	int cnt = 0;
	// cout << "\n";
	// cout << "for " << u << "\n";
	dp[u][1]=-1e6;
	for (auto v : g[u]) {
		if (v == p)
			continue;
		dfs(v, u);
		cnt++;
		sum ^= a[v];
		// cout << "\n";
		// cout << v << ' ' << dp[v][0] << ' ' << dp[v][1] << "\n";
		int v0 = max(dp[u][0] + dp[v][0], dp[u][1] + dp[v][1]);
		int v1 = max(dp[u][0] + dp[v][1], dp[u][0] + dp[v][0]);
		dp[u][0] = v0;
		dp[u][1] = v1;
		// cout << dp[u][0] << ' ' << dp[u][1] << "\n";
	}

	if (!cnt) {
		dp[u][0] = 0;
		dp[u][1] = 1;
		return;
	}
	// cout << u << ' ' << sum << ' ' << a[u] << "\n";
	// cout << dp[u][0] << ' ' << dp[u][1] << "\n";
	if (sum ^ a[u]) {
		swap(dp[u][0], dp[u][1]);
	}
	dp[u][1]++;
}

int32_t main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	for (int i = 0; i < n - 1; i++) {
		int u, v;
		cin >> u >> v;
		u--, v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}

	dfs(0, -1);

	// for (int i = 0; i < n; i++) {
	// 	cout << dp[i][0] << ' ' << dp[i][1] << "\n";
	// }

	cout << max(dp[0][0], dp[0][1]) << "\n";

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3736kb

input:

5
1 0 0 1 0
1 2
1 3
3 4
3 5

output:

5

result:

ok 1 number(s): "5"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3684kb

input:

6
1 1 0 0 1 0
3 1
2 5
1 2
4 1
2 6

output:

5

result:

ok 1 number(s): "5"

Test #3:

score: -100
Wrong Answer
time: 1ms
memory: 5724kb

input:

9
1 0 1 0 1 0 1 0 1
2 9
1 2
6 9
3 8
4 5
5 9
2 8
7 8

output:

5

result:

wrong answer 1st numbers differ - expected: '6', found: '5'