QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#642759#7684. Sweet Sugarucup-team2526WA 107ms5696kbC++201.6kb2024-10-15 16:10:532024-10-15 16:10:53

Judging History

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

  • [2024-10-15 16:10:53]
  • 评测
  • 测评结果:WA
  • 用时:107ms
  • 内存:5696kb
  • [2024-10-15 16:10:53]
  • 提交

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],c[maxn],ans;
int n,k;

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

void GENSHEN_START() {
	ans = 0;
	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] = -1;
	}
	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 << '\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: 100
Accepted
time: 2ms
memory: 5696kb

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:

2
0
1
0

result:

ok 4 number(s): "2 0 1 0"

Test #2:

score: 0
Accepted
time: 2ms
memory: 5624kb

input:

12
1 1
0
1 1
1
1 1
2
1 2
0
1 2
1
1 2
2
1 3
0
1 3
1
1 3
2
1 2000000
0
1 2000000
1
1 2000000
2

output:

0
1
0
0
0
1
0
0
0
0
0
0

result:

ok 12 numbers

Test #3:

score: -100
Wrong Answer
time: 107ms
memory: 5636kb

input:

200000
5 2
1 1 0 0 1
2 4
5 2
4 1
3 2
5 1
0 0 0 0 0
5 1
1 2
3 2
5 4
5 3
1 0 0 0 1
1 4
4 2
3 4
5 2
5 9
1 0 0 0 2
4 3
2 1
3 1
5 1
5 3
0 1 1 0 1
5 4
2 1
4 3
5 1
5 1
0 2 1 1 1
5 3
2 4
3 4
1 4
5 1
1 0 1 1 0
1 5
4 2
1 3
5 2
5 7
0 2 1 1 2
5 1
2 3
2 5
5 4
5 5
0 1 0 1 0
2 4
4 3
5 2
1 5
5 1
0 0 1 0 1
4 1
4 5
2...

output:

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

result:

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