QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#642678#7684. Sweet Sugarucup-team2526WA 118ms7748kbC++201.8kb2024-10-15 15:40:162024-10-15 15:40:17

Judging History

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

  • [2024-10-15 15:40:17]
  • 评测
  • 测评结果:WA
  • 用时:118ms
  • 内存:7748kb
  • [2024-10-15 15:40:16]
  • 提交

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) {
	if (c[u] == 1) dp[u][1] = 1;
	else if (c[u] == 2) dp[u][0] = 2;
	for (auto i : t[u]) {
		if (i == f) continue;
		dfs(i,u);
		int tmp1 = dp[u][1],tmp0 = dp[u][0];
		if (tmp1 > 0 && dp[i][1] > 0) dp[u][1] = max(tmp1 + dp[i][0],tmp0 + dp[i][1]);
		else if (tmp1 > 0) dp[u][1] = tmp1 + dp[i][0];
		else if (dp[i][1] > 0) dp[u][1] = dp[i][1] + tmp0;
		else dp[u][1] = 0;
		if (tmp1 % 2 == 1 && dp[i][1] % 2 == 1) dp[u][0] = max(tmp1 + dp[i][1],tmp0 + dp[i][0]);
		else dp[u][0] = tmp0 + dp[i][0];
 		ans[u] += ans[i];
	}
	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 ;
	}
	//dbg(u,dp[u][0],dp[u][1]);
	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;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 7652kb

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: 7664kb

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: 118ms
memory: 7748kb

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'