QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#441779#8530. Cowntact TracingQwerty1232#8.695652 480ms21928kbC++235.1kb2024-06-14 18:00:302024-06-14 18:00:30

Judging History

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

  • [2024-06-14 18:00:30]
  • 评测
  • 测评结果:8.695652
  • 用时:480ms
  • 内存:21928kb
  • [2024-06-14 18:00:30]
  • 提交

answer

#include <bits/stdc++.h>

int32_t main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n;
    std::cin >> n;
    std::string s;
    std::cin >> s;
    std::vector<std::vector<int>> gr(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        std::cin >> u >> v;
        u--;
        v--;
        gr[u].push_back(v);
        gr[v].push_back(u);
    }

    std::vector<bool> used(n);
    std::vector<int> roots;
    for (int i = 0; i < n; i++) {
        if (used[i]) {
            continue;
        }
        if (s[i] == '0') {
            gr[i].clear();
            continue;
        }
        auto dfs = [&](auto dfs, int v, int f) {
            if (s[v] == '0') {
                return;
            }
            used[v] = true;
            for (int t : gr[v]) {
                if (t != f) {
                    dfs(dfs, t, v);
                }
            }
        };
        roots.push_back(i);
        dfs(dfs, i, -1);
    }

    std::vector<int> sz(n);
    for (int u : roots) {
        auto dfs = [&](auto dfs, int v, int f) {
            if (s[v] == '0') {
                return;
            }
            sz[v] = 1;
            for (int& t : gr[v]) {
                if (t != f && s[t] == '1') {
                    dfs(dfs, t, v);
                    sz[v] += sz[t];
                } else {
                    t = -1;
                }
            }
            std::erase(gr[v], -1);
            std::sort(gr[v].begin(), gr[v].end(), [&](int a, int b) { return sz[a] > sz[b]; });
        };
        dfs(dfs, u, -1);
    }

    std::vector<int> diam(n, -1);
    for (int u : roots) {
        continue;
        if (s[u] == '0') {
            continue;
        }
        std::pair<int, int> max(0, u);
        int cnt = 0;
        for (int i = 0; i < 2; i++) {
            cnt = 0;
            auto dfs = [&](auto dfs, int v, int d, int f) {
                if (s[v] == '0') {
                    return;
                }
                if (std::find_if(gr[v].begin(), gr[v].end(), [&](int t) { return s[t] == '0'; }) != gr[v].end()) {
                    cnt++;
                    max = std::max(max, {d, v});
                }
                for (int t : gr[v]) {
                    if (t != f) {
                        dfs(dfs, t, d + 1, v);
                    }
                }
            };
            int u = max.second;
            max = {0, u};
            dfs(dfs, u, 0, -1);
        }
        if (cnt == 0) {
            continue;
        }
        diam[u] = max.first;
    }

    int q;
    std::cin >> q;
    for (int test = 0; test < q; test++) {
        int d;
        std::cin >> d;

        for (int u : roots) {
            if (diam[u] != -1 && 2 * d > diam[u]) {
                d = -1;
                break;
            }
        }
        if (d == -1) {
            std::cout << "-1\n";
            continue;
        }
        std::vector<std::vector<int>> dp(n), dp2(n);
        int ans_total = 0;
        auto fuck_min = [&](auto& vec) {
            for (int i = (int)vec.size() - 2; i >= 0; i--) {
                vec[i] = std::min(vec[i], vec[i + 1]);
            }
        };
        for (int root : roots) {
            auto dfs = [&](auto dfs, int v) -> void {
                if (gr[v].empty()) {
                    dp[v] = {0};
                    dp2[v] = {1};
                    return;
                }
                for (int t : gr[v]) {
                    dfs(dfs, t);
                }
                dp[v] = std::move(dp[gr[v][0]]);
                dp2[v] = std::move(dp2[gr[v][0]]);

                dp[v].push_back(dp2[v].rbegin()[std::min<int>(dp2[v].size() - 1, d)]);
                dp2[v].push_back(dp[v].rbegin()[std::min<int>(dp[v].size() - 1, d)] + 1);

                for (int i = 1; i < gr[v].size(); i++) {
                    int t = gr[v][i];
                    dp[t].push_back(dp2[t].rbegin()[std::min<int>(dp2[t].size() - 1, d)]);
                    dp2[t].push_back(dp[t].rbegin()[std::min<int>(dp[t].size() - 1, d)] + 1);

                    if (dp[v].size() < dp[t].size()) {
                        dp[v].swap(dp[t]);
                        dp2[v].swap(dp2[t]);
                    }
                    for (int i = 0; i <= std::min<int>(d, dp[t].size() - 1); i++) {
                        dp[v].rbegin()[i] = std::min<int>({
                            dp[v].rbegin()[i] + dp[t].rbegin()[i],
                        });
                        dp2[v].rbegin()[i] = std::min<int>({
                            dp2[v].rbegin()[i] + dp2[t].rbegin()[i],
                            dp2[v].rbegin()[i] + dp[t].rbegin()[std::min<int>(dp[t].size() - 1, d - i)],
                            dp[v].rbegin()[std::min<int>(dp[v].size() - 1, d - i)] + dp2[t].rbegin()[i],
                        });
                    }
                }
            };

            dfs(dfs, root);

            int ans = dp2[root].rbegin()[std::min<int>(dp2[root].size() - 1, d)];
            ans_total += ans;
        }
        std::cout << ans_total << "\n";
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 4.34783
Accepted
time: 1ms
memory: 3620kb

input:

5
11111
1 2
2 3
3 4
4 5
6
5
4
3
2
1
0

output:

1
1
1
1
2
5

result:

ok 6 lines

Test #2:

score: 4.34783
Accepted
time: 0ms
memory: 3620kb

input:

10
1111111111
1 2
2 3
2 4
2 5
2 6
6 7
7 8
8 9
9 10
11
0
1
2
3
4
5
6
7
8
9
10

output:

10
3
2
1
1
1
1
1
1
1
1

result:

ok 11 lines

Test #3:

score: 0
Wrong Answer
time: 0ms
memory: 3844kb

input:

5
11100
1 2
2 3
3 4
4 5
6
0
1
2
3
4
5

output:

3
1
1
1
1
1

result:

wrong answer 4th lines differ - expected: '-1', found: '1'

Test #4:

score: 0
Wrong Answer
time: 0ms
memory: 3536kb

input:

10
1101111111
6 4
6 7
4 3
4 2
7 9
9 5
5 1
2 8
2 10
11
2
9
5
0
3
6
10
8
7
1
4

output:

2
1
1
9
3
1
1
1
1
4
1

result:

wrong answer 2nd lines differ - expected: '-1', found: '1'

Test #5:

score: 0
Wrong Answer
time: 0ms
memory: 3844kb

input:

10
1101011111
7 8
8 2
2 10
10 9
9 4
8 1
10 6
6 3
3 5
11
4
8
6
0
5
3
7
9
10
1
2

output:

1
1
1
8
1
1
1
1
1
3
2

result:

wrong answer 1st lines differ - expected: '-1', found: '1'

Test #6:

score: 0
Wrong Answer
time: 305ms
memory: 20896kb

input:

100000
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

output:

1
1
9
112
10
1
3
4
58
86
4
7
315
15
2
146
1
1
1
1

result:

wrong answer 3rd lines differ - expected: '3', found: '9'

Test #7:

score: 0
Wrong Answer
time: 275ms
memory: 21928kb

input:

100000
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

output:

4
5
135
4
5
408
18
9
173
6
5
5
5
6
19
8
5
5
3
3

result:

wrong answer 1st lines differ - expected: '3', found: '4'

Test #8:

score: 0
Wrong Answer
time: 152ms
memory: 16436kb

input:

100000
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

output:

100000
33334
33333
33333
33333
33333
1
1
1
1
1
1
1
1
1
1
1
1
1
1

result:

wrong answer 4th lines differ - expected: '1', found: '33333'

Test #9:

score: 0
Wrong Answer
time: 1ms
memory: 3892kb

input:

400
11111111101111110011101111111111011111111111111111111111111111111111111111110111111111111111111101111111011111111101111111111111110111111111110110101111111111111111111111111011111111111111110111111111111011111111111111111111110111101111101110111111010011011111111111011111101111110110111111111111...

output:

7
4
13
6
81
7
18
39
33
363
30
52
8
4
2
12
3
7
7
2

result:

wrong answer 1st lines differ - expected: '-1', found: '7'

Test #10:

score: 0
Wrong Answer
time: 1ms
memory: 3852kb

input:

400
11101111111111111111111110110111111111111111011111111111111011111111111111111010111111111110110111011111101111111111111101111111111010111111111111111111111111111111111111101111111111111110111111111111111111111111111111111111111111101011111111111111111110111101111111111111110111111111111111101111...

output:

5
1
77
2
65
6
2
2
164
91
2
47
1
6
2
6
374
2
5
2

result:

wrong answer 1st lines differ - expected: '-1', found: '5'

Test #11:

score: 0
Wrong Answer
time: 1ms
memory: 3608kb

input:

400
11010111101110111110111011010111101011011001111110101101000100011111110101011011011111011110111111111000011110101110011100111111101111101011101110111111111001100111111101101111011011101111111111011101110111111111111011111110111111000111111111111111111111010111101101111111111110111101101110011110...

output:

4
9
8
10
8
2
2
4
4
5
10
16
34
14
2
71
24
2
17
8

result:

wrong answer 1st lines differ - expected: '3', found: '4'

Test #12:

score: 0
Wrong Answer
time: 11ms
memory: 4264kb

input:

6000
1110011111111111110111111111111111111011011111011110111111110111101011111111110111111110110111111111111111111111111111111111111111101111011111111111111111111011111111111111111111111111111111111111111111111110111111111111111111111111011111111111011111101101111111111111111111111111111111110111111...

output:

28
7
34
22
34
7
62
7
50
5433
29
18
7
72
33
21
7
7
7
30

result:

wrong answer 1st lines differ - expected: '19', found: '28'

Test #13:

score: 0
Wrong Answer
time: 13ms
memory: 4244kb

input:

6000
1111111110011111111101111111111101111111111110011111111011111111111111111110110110011011111110111110111111101111111011110111111111101111111111111001111101110111110111111111111111111111111111111010111111111111111101111111110111111111111111111011111111111111111111111111101111111111010111110111111...

output:

6
1
1
4
1
8
6
173
162
1
448
228
1274
1
1
196
6
1
186
1

result:

wrong answer 1st lines differ - expected: '-1', found: '6'

Test #14:

score: 0
Wrong Answer
time: 6ms
memory: 4304kb

input:

6000
1101111110111110101111111111111111111110100111111111111111110111101111111111111111111111111111111111111001111111111111111111111111111111111011111111011110111111110111111111110011111111111111111111111111111111110111111111101111111111100111111111110111110111111011011111110101111111111101111001111...

output:

39
3
6
28
3
36
8
2
8
6
8
2
48
8
3
8
7
3
13
3

result:

wrong answer 1st lines differ - expected: '29', found: '39'

Test #15:

score: 0
Wrong Answer
time: 318ms
memory: 16788kb

input:

100000
11111111101111111101111011100111111011111101000111011001111010011010111110101001110111111110111111110011111111111111110110111110111111110100111011111111111011111101111111001111101101111011010110110011010111111011111111111011111011110110110111111011111001100100011011111111100011010110111011110...

output:

277
85
90
105
205
169
534
241
103
44
62
91
211
187
462
53
96
93
16229
106

result:

wrong answer 1st lines differ - expected: '189', found: '277'

Test #16:

score: 0
Wrong Answer
time: 373ms
memory: 17248kb

input:

100000
11111100010011111111001101110011001111000111010011101100101100011101111101010011111111000101111111111111011110011110110111101111101101111011110111001110111111101101111111111101011110101111111111011110111110111111111111011101111111110110111011110011111111101110111011110111111111110001111011111...

output:

21
16576
46
1
1
1
1
1
46
1107
45
1
1
1
19
1
1
5164
1
1

result:

wrong answer 1st lines differ - expected: '1', found: '21'

Test #17:

score: 0
Wrong Answer
time: 437ms
memory: 17436kb

input:

100000
11111111111111111111111111111110101111111011111111111011111111111101111111101111111111111101111111111111111111111111111111101101111111111111111111101111101001111111111110111111110111111110111110111111111111111111111011111101111011101011111111111111111111111111110111111101111111110111101111110...

output:

18
185
106
18
295
180
114
166
95
129
1410
162
234
18
823
153
97
219
185
120

result:

wrong answer 1st lines differ - expected: '-1', found: '18'

Test #18:

score: 0
Wrong Answer
time: 437ms
memory: 17672kb

input:

100000
11111111111111011111111111011101111111111111111111111111111101111001111111111010111110111111111110111111111011111111111011111111111111111111111111011111111111111111111111011111111111110011111110111111111110111111111101111111111111111111111111111101111111110111111101111101111111111111101111111...

output:

1220
1118
69
1
61
44
5
20909
1
1
55
1
1
12562
62
1
1
1
49
1

result:

wrong answer 1st lines differ - expected: '208', found: '1220'

Test #19:

score: 0
Wrong Answer
time: 363ms
memory: 17692kb

input:

100000
11111111111111111111111111110111111111111001111111111111111111110111110111111111111111101111111111111111011110111111111111110011111111111110010111101100111111111111011111111111111111111011111110011111111101010111111111011111111110111111001111011111111111111111111111111111111111111111111111111...

output:

79
63
57
173
107
229
10
51
69
58
105
144
1128
78
632
84
10
363
10
229

result:

wrong answer 1st lines differ - expected: '50', found: '79'

Test #20:

score: 0
Wrong Answer
time: 480ms
memory: 17784kb

input:

100000
11111111111111111110101111111111111011110101111111111011111111110111111111111111011111111111100111111101111111111111101101011111111111111111111111111111111111111111111111111011111110111111111111111111011111111111111111111111111111111111111111111101111111101111111111111111111011101111111111111...

output:

785
936
4
1
1579
1235
41250
1
14
1
1
1
733
34
1
5
38
21252
36
1

result:

wrong answer 1st lines differ - expected: '209', found: '785'

Test #21:

score: 0
Wrong Answer
time: 221ms
memory: 16272kb

input:

100000
00110111111011100111110111000111101111101001010100110101111001111111100011100110001111101100110110010011001011101011110111101011111111111110111101101111111111101110011111001111111111111101111111010111011010101111011110011111101101101010111011011111010110110010111100011010111101110010111110111...

output:

189
171
116
61
361
674
138
166
72
128
222
149
91
110
78
415
63
125
111
61

result:

wrong answer 1st lines differ - expected: '136', found: '189'

Test #22:

score: 0
Wrong Answer
time: 184ms
memory: 17844kb

input:

100000
11011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

output:

99999
50000
49998
49998
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

result:

wrong answer 2nd lines differ - expected: '49999', found: '50000'

Test #23:

score: 0
Wrong Answer
time: 84ms
memory: 15612kb

input:

99901
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

output:

1
1
2
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
4936
33367

result:

wrong answer 1st lines differ - expected: '-1', found: '1'