답안 #333825

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
333825 2020-12-07T20:40:04 Z 12tqian Mergers (JOI19_mergers) C++17
100 / 100
1995 ms 193352 KB
#include <bits/stdc++.h>
struct LCAJump {
    int n;
    std::vector<std::vector<int>> par;
    std::vector<std::vector<int>> adj;
    std::vector<int> depth;
    void init(int _n) {
        n = _n;
        int d = 1;
        while ((1 << d) < n) d++;
        par.assign(d, std::vector<int>(n));
        adj.resize(n);
        depth.resize(n);
    }
    void ae(int x, int y) {
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    void gen(int root = 0) {
        par[0][root] = root;
        dfs(root);
    }
    void dfs(int src = 0) {
        for (int i = 1; i < par.size(); i++) {
            par[i][src] = par[i - 1][par[i - 1][src]];
        }
        for (int nxt: adj[src]) {
            if (nxt == par[0][src]) continue;
            depth[nxt] = depth[par[0][nxt] = src] + 1;
            dfs(nxt);
        }
    }
    int jump(int x, int d) {
        for (int i = 0; i < par.size(); i++) {
            if ((d >> i) & 1) {
                x = par[i][x];
            }
        }
        return x;
    }
    int lca(int x, int y) {
        if (depth[x] < depth[y]) std::swap(x, y);
        x = jump(x, depth[x] - depth[y]);
        if (x == y) return x;
        for (int i = par.size() - 1; i >= 0; i--) {
            int nx = par[i][x];
            int ny = par[i][y];
            if (nx != ny) x = nx, y = ny;
        }
        return par[0][x];
    }
};

int main() {
    using namespace std;
    ios_base::sync_with_stdio(0);
    int n, k;
    cin >> n >> k;
    LCAJump L;
    L.init(n);
    vector<vector<int>> adj(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v; cin >> u >> v;
        u--, v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
        L.ae(u, v);
    }
    L.gen(0);
    vector<vector<int>> loc(k);
    vector<int> s(n);
    for (int i = 0; i < n; i++) 
        cin >> s[i], s[i]--, loc[s[i]].push_back(i);
    if (n == 1) {
        cout << 0 << '\n';
        return 0;
    }
    vector<int> add(n, 1);
    for (int i = 0; i < k; i++) {
        if (loc[i].empty())
            continue;
        int lca = loc[i].back();
        for (int x : loc[i])
            lca = L.lca(lca, x);
        add[lca] -= (int) loc[i].size();
    }
    vector<int> tot(n);
    for (int x : s)
        tot[x]++;
    vector<multiset<int>> store(n);
    vector<int> id(n);
    iota(id.begin(), id.end(), 0);
    vector<int> bad(n);
    function<int(int, int)> combine = [&](int src, int par) -> int {
        int res = add[src];
        for (int nxt : adj[src]){ 
            if (nxt == par)
                continue;
            res += combine(nxt, src);
        }
        if (res == 0 && src != 0)
            bad[src] = 1;
        return res;
    };
    combine(0, -1);
    int amt = 0;
    for (int i = 1; i < n; i++)
        amt += bad[i];
    int leaves = 0;
    function<int(int, int)> dfs_sub = [&](int src, int par) -> int {
        int sub = bad[src];
        for (int nxt : adj[src]) {
            if (nxt == par)
                continue;
            sub += dfs_sub(nxt, src);
        }
        if (sub == 1 && bad[src]) 
            leaves++;
        else if (sub == amt && bad[src])
            leaves++;
        return sub;
    };
    dfs_sub(0, -1);
    int ans = (leaves + 1) / 2;
    cout << ans << '\n';
    return 0;
}

Compilation message

mergers.cpp: In member function 'void LCAJump::dfs(int)':
mergers.cpp:24:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |         for (int i = 1; i < par.size(); i++) {
      |                         ~~^~~~~~~~~~~~
mergers.cpp: In member function 'int LCAJump::jump(int, int)':
mergers.cpp:34:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |         for (int i = 0; i < par.size(); i++) {
      |                         ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 376 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 0 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 376 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 0 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 4 ms 1132 KB Output is correct
27 Correct 3 ms 1004 KB Output is correct
28 Correct 3 ms 1260 KB Output is correct
29 Correct 3 ms 1132 KB Output is correct
30 Correct 3 ms 1004 KB Output is correct
31 Correct 0 ms 364 KB Output is correct
32 Correct 3 ms 1388 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 2 ms 1132 KB Output is correct
35 Correct 4 ms 1132 KB Output is correct
36 Correct 3 ms 1004 KB Output is correct
37 Correct 3 ms 1132 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 3 ms 1132 KB Output is correct
40 Correct 3 ms 1004 KB Output is correct
41 Correct 3 ms 1004 KB Output is correct
42 Correct 3 ms 1132 KB Output is correct
43 Correct 3 ms 1260 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 3 ms 1260 KB Output is correct
46 Correct 3 ms 1132 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 3 ms 1004 KB Output is correct
49 Correct 3 ms 1260 KB Output is correct
50 Correct 4 ms 1260 KB Output is correct
51 Correct 3 ms 1004 KB Output is correct
52 Correct 2 ms 1132 KB Output is correct
53 Correct 3 ms 1132 KB Output is correct
54 Correct 3 ms 1004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 376 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 0 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 119 ms 26340 KB Output is correct
27 Correct 182 ms 27236 KB Output is correct
28 Correct 3 ms 1132 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 185 ms 27108 KB Output is correct
32 Correct 3 ms 1132 KB Output is correct
33 Correct 237 ms 35684 KB Output is correct
34 Correct 185 ms 27236 KB Output is correct
35 Correct 3 ms 1132 KB Output is correct
36 Correct 205 ms 28004 KB Output is correct
37 Correct 3 ms 1132 KB Output is correct
38 Correct 3 ms 1132 KB Output is correct
39 Correct 132 ms 27876 KB Output is correct
40 Correct 3 ms 1388 KB Output is correct
41 Correct 180 ms 26980 KB Output is correct
42 Correct 228 ms 30052 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 233 ms 35812 KB Output is correct
45 Correct 229 ms 31716 KB Output is correct
46 Correct 4 ms 1132 KB Output is correct
47 Correct 3 ms 1132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 119 ms 26340 KB Output is correct
2 Correct 155 ms 32996 KB Output is correct
3 Correct 3 ms 1132 KB Output is correct
4 Correct 3 ms 1132 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 3 ms 1132 KB Output is correct
8 Correct 224 ms 29668 KB Output is correct
9 Correct 3 ms 1132 KB Output is correct
10 Correct 193 ms 27364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 203 ms 27364 KB Output is correct
13 Correct 219 ms 29668 KB Output is correct
14 Correct 216 ms 32612 KB Output is correct
15 Correct 130 ms 27876 KB Output is correct
16 Correct 3 ms 1132 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 159 ms 32484 KB Output is correct
19 Correct 247 ms 37112 KB Output is correct
20 Correct 3 ms 1132 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 154 ms 30180 KB Output is correct
23 Correct 3 ms 1132 KB Output is correct
24 Correct 212 ms 27876 KB Output is correct
25 Correct 244 ms 35428 KB Output is correct
26 Correct 3 ms 1260 KB Output is correct
27 Correct 3 ms 1260 KB Output is correct
28 Correct 5 ms 1132 KB Output is correct
29 Correct 3 ms 1132 KB Output is correct
30 Correct 3 ms 1132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 376 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 0 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 4 ms 1132 KB Output is correct
27 Correct 3 ms 1004 KB Output is correct
28 Correct 3 ms 1260 KB Output is correct
29 Correct 3 ms 1132 KB Output is correct
30 Correct 3 ms 1004 KB Output is correct
31 Correct 0 ms 364 KB Output is correct
32 Correct 3 ms 1388 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 2 ms 1132 KB Output is correct
35 Correct 4 ms 1132 KB Output is correct
36 Correct 3 ms 1004 KB Output is correct
37 Correct 3 ms 1132 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 3 ms 1132 KB Output is correct
40 Correct 3 ms 1004 KB Output is correct
41 Correct 3 ms 1004 KB Output is correct
42 Correct 3 ms 1132 KB Output is correct
43 Correct 3 ms 1260 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 3 ms 1260 KB Output is correct
46 Correct 3 ms 1132 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 3 ms 1004 KB Output is correct
49 Correct 3 ms 1260 KB Output is correct
50 Correct 4 ms 1260 KB Output is correct
51 Correct 3 ms 1004 KB Output is correct
52 Correct 2 ms 1132 KB Output is correct
53 Correct 3 ms 1132 KB Output is correct
54 Correct 3 ms 1004 KB Output is correct
55 Correct 1 ms 364 KB Output is correct
56 Correct 119 ms 26340 KB Output is correct
57 Correct 182 ms 27236 KB Output is correct
58 Correct 3 ms 1132 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 185 ms 27108 KB Output is correct
62 Correct 3 ms 1132 KB Output is correct
63 Correct 237 ms 35684 KB Output is correct
64 Correct 185 ms 27236 KB Output is correct
65 Correct 3 ms 1132 KB Output is correct
66 Correct 205 ms 28004 KB Output is correct
67 Correct 3 ms 1132 KB Output is correct
68 Correct 3 ms 1132 KB Output is correct
69 Correct 132 ms 27876 KB Output is correct
70 Correct 3 ms 1388 KB Output is correct
71 Correct 180 ms 26980 KB Output is correct
72 Correct 228 ms 30052 KB Output is correct
73 Correct 1 ms 364 KB Output is correct
74 Correct 233 ms 35812 KB Output is correct
75 Correct 229 ms 31716 KB Output is correct
76 Correct 4 ms 1132 KB Output is correct
77 Correct 3 ms 1132 KB Output is correct
78 Correct 119 ms 26340 KB Output is correct
79 Correct 155 ms 32996 KB Output is correct
80 Correct 3 ms 1132 KB Output is correct
81 Correct 3 ms 1132 KB Output is correct
82 Correct 1 ms 364 KB Output is correct
83 Correct 1 ms 364 KB Output is correct
84 Correct 3 ms 1132 KB Output is correct
85 Correct 224 ms 29668 KB Output is correct
86 Correct 3 ms 1132 KB Output is correct
87 Correct 193 ms 27364 KB Output is correct
88 Correct 1 ms 364 KB Output is correct
89 Correct 203 ms 27364 KB Output is correct
90 Correct 219 ms 29668 KB Output is correct
91 Correct 216 ms 32612 KB Output is correct
92 Correct 130 ms 27876 KB Output is correct
93 Correct 3 ms 1132 KB Output is correct
94 Correct 1 ms 364 KB Output is correct
95 Correct 159 ms 32484 KB Output is correct
96 Correct 247 ms 37112 KB Output is correct
97 Correct 3 ms 1132 KB Output is correct
98 Correct 1 ms 364 KB Output is correct
99 Correct 154 ms 30180 KB Output is correct
100 Correct 3 ms 1132 KB Output is correct
101 Correct 212 ms 27876 KB Output is correct
102 Correct 244 ms 35428 KB Output is correct
103 Correct 3 ms 1260 KB Output is correct
104 Correct 3 ms 1260 KB Output is correct
105 Correct 5 ms 1132 KB Output is correct
106 Correct 3 ms 1132 KB Output is correct
107 Correct 3 ms 1132 KB Output is correct
108 Correct 1113 ms 152648 KB Output is correct
109 Correct 1837 ms 161608 KB Output is correct
110 Correct 1839 ms 173000 KB Output is correct
111 Correct 1995 ms 193352 KB Output is correct
112 Correct 1940 ms 181716 KB Output is correct
113 Correct 1223 ms 166216 KB Output is correct
114 Correct 1266 ms 139712 KB Output is correct
115 Correct 1278 ms 138312 KB Output is correct
116 Correct 1554 ms 145480 KB Output is correct
117 Correct 1531 ms 166268 KB Output is correct
118 Correct 1291 ms 137820 KB Output is correct
119 Correct 1538 ms 166216 KB Output is correct
120 Correct 1924 ms 182488 KB Output is correct
121 Correct 1538 ms 166072 KB Output is correct
122 Correct 1643 ms 155208 KB Output is correct
123 Correct 1125 ms 169032 KB Output is correct
124 Correct 1661 ms 162416 KB Output is correct