QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#642632#7684. Sweet Sugarucup-team2526WA 2ms7752kbC++201.5kb2024-10-15 15:23:482024-10-15 15:23:48

Judging History

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

  • [2024-10-15 15:23:48]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:7752kb
  • [2024-10-15 15:23:48]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long

#define dbg(x...) \
do { \
std::cout << #x << " -> "; \
err(x); \
} while (0)

void err() {
	std::cout << std::endl;
}

template<class T, class... Ts>
void err(T arg, Ts &... args) {
	std::cout << fixed << setprecision(10) << arg << ' ';
	err(args...);
}

const int maxn = 1e6 + 5;
vector<int>t[maxn];
int dp[maxn][2],ans[maxn],c[maxn];
int n,k;

void dfs(int u,int f) {
	for (auto i : t[u]) {
		if (i == f) continue;
		dfs(i,u);
		dp[u][0] += dp[i][0];
		dp[u][1] += dp[i][1];
		ans[u] += ans[i];
	}
	int tmp1 = dp[u][1],tmp0 = dp[u][0];
	if (c[u] == 1) {
		dp[u][1] = tmp0 + 1;
		if (tmp1 > 0) dp[u][0] = tmp1 + 1;
	}
	else if (c[u] == 2) {
		if (tmp1 > 0) dp[u][1] = tmp1 + 2;
		dp[u][0] = tmp0 + 2;
	}
	if (k == 1) {
		if (dp[u][1] == 1) {
			ans[u]++;
			dp[u][1] = 0;
			dp[u][0] = 0;
		}
		else {
			dp[u][1] = 0;
			dp[u][0] = 0;
		}
		return ;
	}
	if (((dp[u][1] - k) % 2 == 0 && dp[u][1] >= k) || ((dp[u][0] - k) % 2 == 0 && dp[u][0] >= k)) {
		ans[u]++;
		dp[u][1] = 0;
		dp[u][0] = 0;
	}
}

void GENSHEN_START() {
	cin >> n >> k;
	for (int i = 1;i <= n;i++) cin >> c[i];
	for (int i = 1;i <= n;i++) {
		t[i].clear();
		dp[i][0] = dp[i][1] = ans[i] = 0;
	}
	for (int i = 1;i < n;i++) {
		int u,v;cin >> u >> v;
		t[u].push_back(v);
		t[v].push_back(u);
	}
	dfs(1,0);
	cout << ans[1] << '\n';
}

signed main()
{
	ios::sync_with_stdio(false);cin.tie(nullptr);
	int T = 1;
	cin >> T;
	while (T--) GENSHEN_START();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 7752kb

input:

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

output:

1
0
1
0

result:

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