Submission #595407

# Submission time Handle Problem Language Result Execution time Memory
595407 2022-07-13T17:25:16 Z elkernos Mergers (JOI19_mergers) C++17
100 / 100
850 ms 139500 KB
#include <bits/stdc++.h>

using namespace std;

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    int n, k;
    cin >> n >> k;
    vector<vector<int>> g(n + 1), gg(k + 1);
    for(int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }
    for(int i = 1; i <= n; i++) {
        int s;
        cin >> s;
        gg[s].emplace_back(i);
    }
    int T = 0;
    vector<int> st(n + 1), en(n + 1), dep(n + 1);
    const int lg = 18;
    vector<vector<int>> jump(lg + 1, vector<int>(n + 1));
    vector<int> ord;
    function<void(int, int)> dfs = [&](int u, int p) {
        st[u] = ++T;
        ord.push_back(u);
        for(int to : g[u]) {
            if(to == p) {
                continue;
            }
            dep[to] = dep[u] + 1;
            jump[0][to] = u;
            dfs(to, u);
        }
        en[u] = T;
    };
    dfs(1, 1);
    for(int i = 1; i <= lg; i++) {
        for(int j = 1; j <= n; j++) {
            jump[i][j] = jump[i - 1][jump[i - 1][j]];
        }
    }
    auto anc = [&](int e, int dx) {
        for(int i = 0; dx > 0; i++) {
            if(dx % 2 == 1) {
                e = jump[i][e];
            }
            dx /= 2;
        }
        return e;
    };
    auto lca = [&](int a, int b) {
        if(dep[a] > dep[b]) {
            swap(a, b);
        }
        b = anc(b, dep[b] - dep[a]);
        if(a == b) {
            return a;
        }
        for(int i = lg; i >= 0; i--) {
            if(jump[i][a] != jump[i][b]) {
                a = jump[i][a];
                b = jump[i][b];
            }
        }
        return jump[0][a];
    };
    vector<int> pre(n + 1);
    for(int i = 1; i <= k; i++) {
        sort(gg[i].begin(), gg[i].end(), [&](int ii, int jj) {
            return st[ii] < st[jj];
        });
        for(int j = 0; j + 1 < (int)gg[i].size(); j++) {
            int a = gg[i][j], b = gg[i][j + 1], c = lca(a, b);
            pre[a]++, pre[b]++, pre[c] -= 2;
        }
    }
    vector<int> par(n + 1);
    function<int(int)> f = [&](int u) {
        return u == par[u] ? u : par[u] = f(par[u]);
    };
    auto u = [&](int a, int b) {
        par[f(a)] = f(b);
    };
    for(int i = 1; i <= n; i++) {
        par[i] = i;
    }
    for(int i = n - 1; i >= 0; i--) {
        int x = ord[i];
        for(int to : g[x]) {
            if(to != jump[0][x]) {
                pre[x] += pre[to];
            }
        }
        if(pre[x]) {
            u(jump[0][x], x);
        }
    }
    vector<int> deg(n + 1);
    for(int i = 2; i <= n; i++) {
        int a = f(jump[0][i]), b = f(i);
        if(a != b) {
            deg[a]++;
            deg[b]++;
        }
    }
    int ans = 0;
    for(int i = 1; i <= n; i++) {
        if(deg[i] == 1) {
            ans++;
        }
    }
    cout << (ans + 1) / 2 << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 320 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 320 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 2 ms 852 KB Output is correct
27 Correct 2 ms 852 KB Output is correct
28 Correct 2 ms 1104 KB Output is correct
29 Correct 2 ms 980 KB Output is correct
30 Correct 2 ms 852 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 2 ms 1108 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 2 ms 852 KB Output is correct
35 Correct 2 ms 964 KB Output is correct
36 Correct 2 ms 852 KB Output is correct
37 Correct 3 ms 852 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 3 ms 852 KB Output is correct
40 Correct 2 ms 852 KB Output is correct
41 Correct 2 ms 852 KB Output is correct
42 Correct 2 ms 976 KB Output is correct
43 Correct 2 ms 996 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 2 ms 980 KB Output is correct
46 Correct 2 ms 852 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 2 ms 852 KB Output is correct
49 Correct 2 ms 980 KB Output is correct
50 Correct 2 ms 980 KB Output is correct
51 Correct 2 ms 852 KB Output is correct
52 Correct 2 ms 852 KB Output is correct
53 Correct 2 ms 980 KB Output is correct
54 Correct 2 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 320 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 76 ms 22256 KB Output is correct
27 Correct 98 ms 18184 KB Output is correct
28 Correct 2 ms 980 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 86 ms 17960 KB Output is correct
32 Correct 2 ms 852 KB Output is correct
33 Correct 87 ms 25232 KB Output is correct
34 Correct 94 ms 18008 KB Output is correct
35 Correct 2 ms 852 KB Output is correct
36 Correct 80 ms 18856 KB Output is correct
37 Correct 3 ms 852 KB Output is correct
38 Correct 2 ms 852 KB Output is correct
39 Correct 77 ms 19736 KB Output is correct
40 Correct 2 ms 980 KB Output is correct
41 Correct 84 ms 17780 KB Output is correct
42 Correct 86 ms 20396 KB Output is correct
43 Correct 0 ms 340 KB Output is correct
44 Correct 82 ms 25296 KB Output is correct
45 Correct 100 ms 21780 KB Output is correct
46 Correct 2 ms 852 KB Output is correct
47 Correct 2 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 22296 KB Output is correct
2 Correct 59 ms 23600 KB Output is correct
3 Correct 2 ms 852 KB Output is correct
4 Correct 2 ms 852 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
7 Correct 2 ms 852 KB Output is correct
8 Correct 102 ms 20620 KB Output is correct
9 Correct 2 ms 852 KB Output is correct
10 Correct 92 ms 18244 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 74 ms 18176 KB Output is correct
13 Correct 88 ms 20416 KB Output is correct
14 Correct 77 ms 23284 KB Output is correct
15 Correct 80 ms 19692 KB Output is correct
16 Correct 2 ms 980 KB Output is correct
17 Correct 0 ms 320 KB Output is correct
18 Correct 61 ms 23344 KB Output is correct
19 Correct 74 ms 27204 KB Output is correct
20 Correct 2 ms 852 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 71 ms 22172 KB Output is correct
23 Correct 2 ms 852 KB Output is correct
24 Correct 83 ms 18868 KB Output is correct
25 Correct 78 ms 25916 KB Output is correct
26 Correct 2 ms 980 KB Output is correct
27 Correct 2 ms 980 KB Output is correct
28 Correct 2 ms 852 KB Output is correct
29 Correct 2 ms 852 KB Output is correct
30 Correct 2 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 320 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 2 ms 852 KB Output is correct
27 Correct 2 ms 852 KB Output is correct
28 Correct 2 ms 1104 KB Output is correct
29 Correct 2 ms 980 KB Output is correct
30 Correct 2 ms 852 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 2 ms 1108 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 2 ms 852 KB Output is correct
35 Correct 2 ms 964 KB Output is correct
36 Correct 2 ms 852 KB Output is correct
37 Correct 3 ms 852 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 3 ms 852 KB Output is correct
40 Correct 2 ms 852 KB Output is correct
41 Correct 2 ms 852 KB Output is correct
42 Correct 2 ms 976 KB Output is correct
43 Correct 2 ms 996 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 2 ms 980 KB Output is correct
46 Correct 2 ms 852 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 2 ms 852 KB Output is correct
49 Correct 2 ms 980 KB Output is correct
50 Correct 2 ms 980 KB Output is correct
51 Correct 2 ms 852 KB Output is correct
52 Correct 2 ms 852 KB Output is correct
53 Correct 2 ms 980 KB Output is correct
54 Correct 2 ms 852 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 76 ms 22256 KB Output is correct
57 Correct 98 ms 18184 KB Output is correct
58 Correct 2 ms 980 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 86 ms 17960 KB Output is correct
62 Correct 2 ms 852 KB Output is correct
63 Correct 87 ms 25232 KB Output is correct
64 Correct 94 ms 18008 KB Output is correct
65 Correct 2 ms 852 KB Output is correct
66 Correct 80 ms 18856 KB Output is correct
67 Correct 3 ms 852 KB Output is correct
68 Correct 2 ms 852 KB Output is correct
69 Correct 77 ms 19736 KB Output is correct
70 Correct 2 ms 980 KB Output is correct
71 Correct 84 ms 17780 KB Output is correct
72 Correct 86 ms 20396 KB Output is correct
73 Correct 0 ms 340 KB Output is correct
74 Correct 82 ms 25296 KB Output is correct
75 Correct 100 ms 21780 KB Output is correct
76 Correct 2 ms 852 KB Output is correct
77 Correct 2 ms 852 KB Output is correct
78 Correct 84 ms 22296 KB Output is correct
79 Correct 59 ms 23600 KB Output is correct
80 Correct 2 ms 852 KB Output is correct
81 Correct 2 ms 852 KB Output is correct
82 Correct 0 ms 212 KB Output is correct
83 Correct 1 ms 328 KB Output is correct
84 Correct 2 ms 852 KB Output is correct
85 Correct 102 ms 20620 KB Output is correct
86 Correct 2 ms 852 KB Output is correct
87 Correct 92 ms 18244 KB Output is correct
88 Correct 1 ms 324 KB Output is correct
89 Correct 74 ms 18176 KB Output is correct
90 Correct 88 ms 20416 KB Output is correct
91 Correct 77 ms 23284 KB Output is correct
92 Correct 80 ms 19692 KB Output is correct
93 Correct 2 ms 980 KB Output is correct
94 Correct 0 ms 320 KB Output is correct
95 Correct 61 ms 23344 KB Output is correct
96 Correct 74 ms 27204 KB Output is correct
97 Correct 2 ms 852 KB Output is correct
98 Correct 1 ms 212 KB Output is correct
99 Correct 71 ms 22172 KB Output is correct
100 Correct 2 ms 852 KB Output is correct
101 Correct 83 ms 18868 KB Output is correct
102 Correct 78 ms 25916 KB Output is correct
103 Correct 2 ms 980 KB Output is correct
104 Correct 2 ms 980 KB Output is correct
105 Correct 2 ms 852 KB Output is correct
106 Correct 2 ms 852 KB Output is correct
107 Correct 2 ms 980 KB Output is correct
108 Correct 549 ms 122076 KB Output is correct
109 Correct 632 ms 106720 KB Output is correct
110 Correct 627 ms 118032 KB Output is correct
111 Correct 660 ms 139500 KB Output is correct
112 Correct 659 ms 129800 KB Output is correct
113 Correct 453 ms 116464 KB Output is correct
114 Correct 693 ms 88796 KB Output is correct
115 Correct 694 ms 88980 KB Output is correct
116 Correct 850 ms 96000 KB Output is correct
117 Correct 616 ms 116636 KB Output is correct
118 Correct 684 ms 89180 KB Output is correct
119 Correct 588 ms 116480 KB Output is correct
120 Correct 627 ms 130568 KB Output is correct
121 Correct 606 ms 116476 KB Output is correct
122 Correct 762 ms 105584 KB Output is correct
123 Correct 438 ms 118048 KB Output is correct
124 Correct 633 ms 114068 KB Output is correct