QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#394763#7684. Sweet Sugarucup-team228#WA 107ms3672kbC++201.6kb2024-04-20 19:18:342024-04-20 19:18:35

Judging History

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

  • [2024-04-20 19:18:35]
  • 评测
  • 测评结果:WA
  • 用时:107ms
  • 内存:3672kb
  • [2024-04-20 19:18:34]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int inf = 1e9 + 10;
const int N = 1e6 + 10;
int c[N];
vector<int> g[N];
int dp[N][2];

void init(int n) {
    for (int i = 1; i <= n; i++) {
        g[i].clear();
    }
}

void dfs(int v, int p, int k, int& ans) {
    dp[v][0] = 0;
    dp[v][1] = -inf;
    dp[v][c[v] & 1] = c[v];
    for (int to : g[v]) {
        if (to != p) {
            dfs(to, v, k, ans);
            int res[2] = {dp[v][0], dp[v][1]};
            for (int i = 0; i <= 1; i++) {
                for (int j = 0; j <= 1; j++) {
                    res[i ^ j] = max(res[i ^ j], dp[v][i] + dp[to][j]);
                }
            }
            dp[v][0] = res[0];
            dp[v][1] = res[1];
        }
    }
    if (dp[v][k & 1] >= k) {
        ans++;
        dp[v][0] = 0;
        dp[v][1] = -inf;
    }
}

int solve(int n, int k) {
    int ans = 0;
    dfs(1, 1, k, ans);
    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif

    int t;
    cin >> t;
    while (t--) {
        int n, k;
        cin >> n >> k;
        init(n);
        for (int i = 1; i <= n; i++) {
            cin >> c[i];
        }
        for (int i = 1; i <= n - 1; i++) {
            int u, v;
            cin >> u >> v;
            g[u].push_back(v);
            g[v].push_back(u);
        }
        cout << solve(n, k) << "\n";
    }

#ifdef LOCAL
    cout << "\nTime elapsed: " << double(clock()) / CLOCKS_PER_SEC << " s.\n";
#endif
}

详细

Test #1:

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

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

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

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'