来个新做法,为啥除了超时,还有WA?
查看原帖
来个新做法,为啥除了超时,还有WA?
791222
lby_commandBlock楼主2024/7/22 21:26

通过画图知,若出现了黑色点,则黑色点的爸爸们必须全部染成黑色。但是为啥会有 Wrong Answer ?

这里在 Luogu 评测是 00 分的,在某平台全真模拟是 2323 分(满分 2525 分)。

#include <bits/stdc++.h>
#define endl '\n'
using namespace std;

const int N = 1e5 + 9;

vector<int> e[N];

int n, color[N], ans;

bool dfs(int root) {
	if (e[root].size() == 0 && color[root] == 0)
		return true;
	if (color[root] == 1)
		return false;
	for (auto node : e[root]) {
		if (!dfs(node))
			return false;
	}
	return true;
}

int main() {
	cin >> n;
	ans = 0;
	for (int i = 1; i <= n; i++)
		cin >> color[i];
	for (int i = 1; i < n; i++) {
		int x, y;
		cin >> x >> y;
		e[x].push_back(y);
	}
	for (int i = 1; i <= n; i++) {
		if (color[i] == 0) {
			ans += (!dfs(i));
		}
	}
	cout << ans << endl;
	return 0;
}

2024/7/22 21:26
加载中...