Submission #1083489

# Submission time Handle Problem Language Result Execution time Memory
1083489 2024-09-03T10:19:10 Z Sharky Mergers (JOI19_mergers) C++17
100 / 100
707 ms 202324 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 5e5 + 10, LG = 20;
int p[N], sz[N], dep[N], st[N][LG], conn[N], tin[N], timer = 0, cc = 0, leaf = 0;
vector<int> adj[N], sta[N];
set<int> g[N];

int find(int u) {
    return p[u] == u ? u : p[u] = find(p[u]);
}

void merge(int u, int v) {
    u = find(u), v = find(v);
    if (u == v) return;
    p[v] = u;
}

void dfs(int u, int p) {
    tin[u] = ++timer;
    for (int i = 1; i < LG; i++) {
        st[u][i] = st[st[u][i - 1]][i - 1];
    }
    for (int v : adj[u]) {
        if (v == p) continue;
        st[v][0] = u;
        dep[v] = dep[u] + 1;
        dfs(v, u);
    }
}

int getLCA(int u, int v) {
    if (dep[u] < dep[v]) swap(u, v);
    for (int i = LG - 1; i >= 0; i--) {
        if ((dep[u] - dep[v]) & (1 << i)) {
            u = st[u][i];
        }
    }
    if (u == v) return u;
    for (int i = LG - 1; i >= 0; i--) {
        int ut = st[u][i], vt = st[v][i];
        if (ut != vt) u = ut, v = vt;
    }
    return st[u][0];
}

int32_t main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n, k;
    cin >> n >> k;
    if (n == 1) {
        cout << "0\n";
        return 0;
    }
    for (int i = 1; i <= n; i++) p[i] = i, sz[i] = 1;
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    st[1][0] = 1;
    dfs(1, -1);
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        sta[x].push_back(i);
    }
    for (int i = 1; i <= k; i++) {
        sort(sta[i].begin(), sta[i].end(), [&] (int x, int y) {
            return tin[x] < tin[y];
        });
        for (int j = 1; j < (int) sta[i].size(); j++) {
            int x = sta[i][j - 1], y = sta[i][j];
            int lca = getLCA(x, y);
            while (dep[x] > dep[lca]) {
                merge(st[x][0], x);
                x = find(x);
            }
            while (dep[y] > dep[lca]) {
                merge(st[y][0], y);
                y = find(y);
            }
        }
    }
    for (int u = 1; u <= n; u++) for (int &v : adj[u]) if (find(u) != find(v)) g[find(u)].insert(find(v));
    for (int i = 1; i <= n; i++) if ((int) g[i].size() == 1) leaf++;
    cout << (leaf + 1) / 2 << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 18 ms 47452 KB Output is correct
2 Correct 19 ms 47452 KB Output is correct
3 Correct 18 ms 47452 KB Output is correct
4 Correct 18 ms 47468 KB Output is correct
5 Correct 19 ms 47448 KB Output is correct
6 Correct 18 ms 47448 KB Output is correct
7 Correct 18 ms 47196 KB Output is correct
8 Correct 18 ms 47452 KB Output is correct
9 Correct 18 ms 47448 KB Output is correct
10 Correct 19 ms 47452 KB Output is correct
11 Correct 26 ms 47376 KB Output is correct
12 Correct 18 ms 47452 KB Output is correct
13 Correct 19 ms 47452 KB Output is correct
14 Correct 19 ms 47228 KB Output is correct
15 Correct 18 ms 47452 KB Output is correct
16 Correct 19 ms 47428 KB Output is correct
17 Correct 23 ms 47452 KB Output is correct
18 Correct 20 ms 47448 KB Output is correct
19 Correct 18 ms 47448 KB Output is correct
20 Correct 19 ms 47452 KB Output is correct
21 Correct 20 ms 47356 KB Output is correct
22 Correct 21 ms 47452 KB Output is correct
23 Correct 18 ms 47448 KB Output is correct
24 Correct 18 ms 47452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 47452 KB Output is correct
2 Correct 19 ms 47452 KB Output is correct
3 Correct 18 ms 47452 KB Output is correct
4 Correct 18 ms 47468 KB Output is correct
5 Correct 19 ms 47448 KB Output is correct
6 Correct 18 ms 47448 KB Output is correct
7 Correct 18 ms 47196 KB Output is correct
8 Correct 18 ms 47452 KB Output is correct
9 Correct 18 ms 47448 KB Output is correct
10 Correct 19 ms 47452 KB Output is correct
11 Correct 26 ms 47376 KB Output is correct
12 Correct 18 ms 47452 KB Output is correct
13 Correct 19 ms 47452 KB Output is correct
14 Correct 19 ms 47228 KB Output is correct
15 Correct 18 ms 47452 KB Output is correct
16 Correct 19 ms 47428 KB Output is correct
17 Correct 23 ms 47452 KB Output is correct
18 Correct 20 ms 47448 KB Output is correct
19 Correct 18 ms 47448 KB Output is correct
20 Correct 19 ms 47452 KB Output is correct
21 Correct 20 ms 47356 KB Output is correct
22 Correct 21 ms 47452 KB Output is correct
23 Correct 18 ms 47448 KB Output is correct
24 Correct 18 ms 47452 KB Output is correct
25 Correct 19 ms 47452 KB Output is correct
26 Correct 20 ms 47964 KB Output is correct
27 Correct 19 ms 47708 KB Output is correct
28 Correct 20 ms 48152 KB Output is correct
29 Correct 19 ms 48216 KB Output is correct
30 Correct 20 ms 47708 KB Output is correct
31 Correct 18 ms 47476 KB Output is correct
32 Correct 21 ms 47964 KB Output is correct
33 Correct 20 ms 47452 KB Output is correct
34 Correct 23 ms 47704 KB Output is correct
35 Correct 24 ms 48212 KB Output is correct
36 Correct 19 ms 47704 KB Output is correct
37 Correct 22 ms 47708 KB Output is correct
38 Correct 18 ms 47452 KB Output is correct
39 Correct 25 ms 47752 KB Output is correct
40 Correct 19 ms 47696 KB Output is correct
41 Correct 21 ms 47704 KB Output is correct
42 Correct 22 ms 47956 KB Output is correct
43 Correct 23 ms 47964 KB Output is correct
44 Correct 21 ms 47448 KB Output is correct
45 Correct 20 ms 47964 KB Output is correct
46 Correct 22 ms 47708 KB Output is correct
47 Correct 21 ms 47452 KB Output is correct
48 Correct 21 ms 47704 KB Output is correct
49 Correct 21 ms 48216 KB Output is correct
50 Correct 21 ms 48220 KB Output is correct
51 Correct 20 ms 47752 KB Output is correct
52 Correct 30 ms 47880 KB Output is correct
53 Correct 20 ms 47964 KB Output is correct
54 Correct 21 ms 47876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 47452 KB Output is correct
2 Correct 19 ms 47452 KB Output is correct
3 Correct 18 ms 47452 KB Output is correct
4 Correct 18 ms 47468 KB Output is correct
5 Correct 19 ms 47448 KB Output is correct
6 Correct 18 ms 47448 KB Output is correct
7 Correct 18 ms 47196 KB Output is correct
8 Correct 18 ms 47452 KB Output is correct
9 Correct 18 ms 47448 KB Output is correct
10 Correct 19 ms 47452 KB Output is correct
11 Correct 26 ms 47376 KB Output is correct
12 Correct 18 ms 47452 KB Output is correct
13 Correct 19 ms 47452 KB Output is correct
14 Correct 19 ms 47228 KB Output is correct
15 Correct 18 ms 47452 KB Output is correct
16 Correct 19 ms 47428 KB Output is correct
17 Correct 23 ms 47452 KB Output is correct
18 Correct 20 ms 47448 KB Output is correct
19 Correct 18 ms 47448 KB Output is correct
20 Correct 19 ms 47452 KB Output is correct
21 Correct 20 ms 47356 KB Output is correct
22 Correct 21 ms 47452 KB Output is correct
23 Correct 18 ms 47448 KB Output is correct
24 Correct 18 ms 47452 KB Output is correct
25 Correct 18 ms 47452 KB Output is correct
26 Correct 66 ms 62228 KB Output is correct
27 Correct 76 ms 61784 KB Output is correct
28 Correct 23 ms 47704 KB Output is correct
29 Correct 18 ms 47452 KB Output is correct
30 Correct 19 ms 47316 KB Output is correct
31 Correct 69 ms 61836 KB Output is correct
32 Correct 22 ms 47708 KB Output is correct
33 Correct 95 ms 69196 KB Output is correct
34 Correct 75 ms 62036 KB Output is correct
35 Correct 23 ms 47700 KB Output is correct
36 Correct 85 ms 62604 KB Output is correct
37 Correct 19 ms 47708 KB Output is correct
38 Correct 21 ms 47732 KB Output is correct
39 Correct 75 ms 62288 KB Output is correct
40 Correct 21 ms 47964 KB Output is correct
41 Correct 80 ms 61904 KB Output is correct
42 Correct 91 ms 64384 KB Output is correct
43 Correct 19 ms 47252 KB Output is correct
44 Correct 87 ms 69448 KB Output is correct
45 Correct 97 ms 65620 KB Output is correct
46 Correct 23 ms 47780 KB Output is correct
47 Correct 23 ms 47708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 62640 KB Output is correct
2 Correct 97 ms 74572 KB Output is correct
3 Correct 23 ms 47964 KB Output is correct
4 Correct 22 ms 47708 KB Output is correct
5 Correct 21 ms 47452 KB Output is correct
6 Correct 22 ms 47452 KB Output is correct
7 Correct 21 ms 47708 KB Output is correct
8 Correct 92 ms 64596 KB Output is correct
9 Correct 20 ms 47700 KB Output is correct
10 Correct 72 ms 62032 KB Output is correct
11 Correct 21 ms 47452 KB Output is correct
12 Correct 72 ms 62036 KB Output is correct
13 Correct 91 ms 65112 KB Output is correct
14 Correct 85 ms 74320 KB Output is correct
15 Correct 71 ms 62092 KB Output is correct
16 Correct 23 ms 47964 KB Output is correct
17 Correct 20 ms 47452 KB Output is correct
18 Correct 96 ms 72256 KB Output is correct
19 Correct 96 ms 78072 KB Output is correct
20 Correct 22 ms 47708 KB Output is correct
21 Correct 21 ms 47440 KB Output is correct
22 Correct 76 ms 65296 KB Output is correct
23 Correct 20 ms 47696 KB Output is correct
24 Correct 80 ms 63316 KB Output is correct
25 Correct 99 ms 76884 KB Output is correct
26 Correct 24 ms 48220 KB Output is correct
27 Correct 25 ms 48216 KB Output is correct
28 Correct 28 ms 47712 KB Output is correct
29 Correct 23 ms 47704 KB Output is correct
30 Correct 22 ms 47964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 47452 KB Output is correct
2 Correct 19 ms 47452 KB Output is correct
3 Correct 18 ms 47452 KB Output is correct
4 Correct 18 ms 47468 KB Output is correct
5 Correct 19 ms 47448 KB Output is correct
6 Correct 18 ms 47448 KB Output is correct
7 Correct 18 ms 47196 KB Output is correct
8 Correct 18 ms 47452 KB Output is correct
9 Correct 18 ms 47448 KB Output is correct
10 Correct 19 ms 47452 KB Output is correct
11 Correct 26 ms 47376 KB Output is correct
12 Correct 18 ms 47452 KB Output is correct
13 Correct 19 ms 47452 KB Output is correct
14 Correct 19 ms 47228 KB Output is correct
15 Correct 18 ms 47452 KB Output is correct
16 Correct 19 ms 47428 KB Output is correct
17 Correct 23 ms 47452 KB Output is correct
18 Correct 20 ms 47448 KB Output is correct
19 Correct 18 ms 47448 KB Output is correct
20 Correct 19 ms 47452 KB Output is correct
21 Correct 20 ms 47356 KB Output is correct
22 Correct 21 ms 47452 KB Output is correct
23 Correct 18 ms 47448 KB Output is correct
24 Correct 18 ms 47452 KB Output is correct
25 Correct 19 ms 47452 KB Output is correct
26 Correct 20 ms 47964 KB Output is correct
27 Correct 19 ms 47708 KB Output is correct
28 Correct 20 ms 48152 KB Output is correct
29 Correct 19 ms 48216 KB Output is correct
30 Correct 20 ms 47708 KB Output is correct
31 Correct 18 ms 47476 KB Output is correct
32 Correct 21 ms 47964 KB Output is correct
33 Correct 20 ms 47452 KB Output is correct
34 Correct 23 ms 47704 KB Output is correct
35 Correct 24 ms 48212 KB Output is correct
36 Correct 19 ms 47704 KB Output is correct
37 Correct 22 ms 47708 KB Output is correct
38 Correct 18 ms 47452 KB Output is correct
39 Correct 25 ms 47752 KB Output is correct
40 Correct 19 ms 47696 KB Output is correct
41 Correct 21 ms 47704 KB Output is correct
42 Correct 22 ms 47956 KB Output is correct
43 Correct 23 ms 47964 KB Output is correct
44 Correct 21 ms 47448 KB Output is correct
45 Correct 20 ms 47964 KB Output is correct
46 Correct 22 ms 47708 KB Output is correct
47 Correct 21 ms 47452 KB Output is correct
48 Correct 21 ms 47704 KB Output is correct
49 Correct 21 ms 48216 KB Output is correct
50 Correct 21 ms 48220 KB Output is correct
51 Correct 20 ms 47752 KB Output is correct
52 Correct 30 ms 47880 KB Output is correct
53 Correct 20 ms 47964 KB Output is correct
54 Correct 21 ms 47876 KB Output is correct
55 Correct 18 ms 47452 KB Output is correct
56 Correct 66 ms 62228 KB Output is correct
57 Correct 76 ms 61784 KB Output is correct
58 Correct 23 ms 47704 KB Output is correct
59 Correct 18 ms 47452 KB Output is correct
60 Correct 19 ms 47316 KB Output is correct
61 Correct 69 ms 61836 KB Output is correct
62 Correct 22 ms 47708 KB Output is correct
63 Correct 95 ms 69196 KB Output is correct
64 Correct 75 ms 62036 KB Output is correct
65 Correct 23 ms 47700 KB Output is correct
66 Correct 85 ms 62604 KB Output is correct
67 Correct 19 ms 47708 KB Output is correct
68 Correct 21 ms 47732 KB Output is correct
69 Correct 75 ms 62288 KB Output is correct
70 Correct 21 ms 47964 KB Output is correct
71 Correct 80 ms 61904 KB Output is correct
72 Correct 91 ms 64384 KB Output is correct
73 Correct 19 ms 47252 KB Output is correct
74 Correct 87 ms 69448 KB Output is correct
75 Correct 97 ms 65620 KB Output is correct
76 Correct 23 ms 47780 KB Output is correct
77 Correct 23 ms 47708 KB Output is correct
78 Correct 69 ms 62640 KB Output is correct
79 Correct 97 ms 74572 KB Output is correct
80 Correct 23 ms 47964 KB Output is correct
81 Correct 22 ms 47708 KB Output is correct
82 Correct 21 ms 47452 KB Output is correct
83 Correct 22 ms 47452 KB Output is correct
84 Correct 21 ms 47708 KB Output is correct
85 Correct 92 ms 64596 KB Output is correct
86 Correct 20 ms 47700 KB Output is correct
87 Correct 72 ms 62032 KB Output is correct
88 Correct 21 ms 47452 KB Output is correct
89 Correct 72 ms 62036 KB Output is correct
90 Correct 91 ms 65112 KB Output is correct
91 Correct 85 ms 74320 KB Output is correct
92 Correct 71 ms 62092 KB Output is correct
93 Correct 23 ms 47964 KB Output is correct
94 Correct 20 ms 47452 KB Output is correct
95 Correct 96 ms 72256 KB Output is correct
96 Correct 96 ms 78072 KB Output is correct
97 Correct 22 ms 47708 KB Output is correct
98 Correct 21 ms 47440 KB Output is correct
99 Correct 76 ms 65296 KB Output is correct
100 Correct 20 ms 47696 KB Output is correct
101 Correct 80 ms 63316 KB Output is correct
102 Correct 99 ms 76884 KB Output is correct
103 Correct 24 ms 48220 KB Output is correct
104 Correct 25 ms 48216 KB Output is correct
105 Correct 28 ms 47712 KB Output is correct
106 Correct 23 ms 47704 KB Output is correct
107 Correct 22 ms 47964 KB Output is correct
108 Correct 447 ms 132368 KB Output is correct
109 Correct 575 ms 138180 KB Output is correct
110 Correct 571 ms 149364 KB Output is correct
111 Correct 609 ms 202324 KB Output is correct
112 Correct 707 ms 174416 KB Output is correct
113 Correct 546 ms 173932 KB Output is correct
114 Correct 509 ms 120176 KB Output is correct
115 Correct 582 ms 120108 KB Output is correct
116 Correct 540 ms 125008 KB Output is correct
117 Correct 531 ms 183124 KB Output is correct
118 Correct 462 ms 120836 KB Output is correct
119 Correct 541 ms 182880 KB Output is correct
120 Correct 618 ms 196944 KB Output is correct
121 Correct 544 ms 182884 KB Output is correct
122 Correct 587 ms 137792 KB Output is correct
123 Correct 574 ms 184556 KB Output is correct
124 Correct 569 ms 164252 KB Output is correct