Submission #699453

# Submission time Handle Problem Language Result Execution time Memory
699453 2023-02-17T03:13:42 Z nguyentunglam Mergers (JOI19_mergers) C++17
70 / 100
166 ms 71612 KB
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 1e5 + 10;
int c[20][N * 2], st[N], lab[N], f[N], g[N];
vector<int> adj[N], lst[N];
int cnt;
void dfs(int u) {
    c[0][++cnt] = u;
    st[u] = cnt;
    for(int &v : adj[u]) if (!st[v]) {
        dfs(v);
        c[0][++cnt] = u;
    }
}
int lca(int u, int v) {
    int l = st[u], r = st[v];
    if (l > r) swap(l, r);
    int k = log2(r - l + 1);
    int x = c[k][l], y = c[k][r - (1 << k) + 1];
    return st[x] < st[y] ? x : y;
}
int root(int v) {
    return lab[v] < 0 ? v : lab[v] = root(lab[v]);
}
void join(int u, int v) {
    u = root(u); v = root(v);
    if (u == v) return;
    if (lab[u] > lab[v]) swap(u, v);
    lab[u] += lab[v];
    lab[v] = u;
}
void scan(int u, int par) {
    for(int &v : adj[u]) if (v != par) {
        scan(v, u);
        f[u] += f[v];
    }
    if (f[u] && par) join(u, par);
}
int main() {
    #define task ""
    cin.tie(0) -> sync_with_stdio(0);
    if (fopen ("task.inp", "r")) {
        freopen ("task.inp", "r", stdin);
        freopen ("task.out", "w", stdout);
    }
    if (fopen (task".inp", "r")) {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }
    int n, k; cin >> n >> k;
    for(int i = 1; i < n; i++) {
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(1);
    for(int j = 1; j <= 18; j++) for(int i = 1; i <= cnt; i++) {
        int x = c[j - 1][i], y = c[j - 1][i + (1 << (j - 1))];
        c[j][i] = st[x] < st[y] ? x : y;
    }
    for(int i = 1; i <= n; i++) {
        int s; cin >> s;
        lst[s].push_back(i);
        lab[i] = -1;
    }
    for(int color = 1; color <= k; color++) if (!lst[color].empty()) {
        int p = lst[color].back();
        for(int &j : lst[color]) p = lca(p, j);
        for(int &j : lst[color]) {
            f[j]++; f[p]--;
        }
    }
    scan(1, 0);
    for(int u = 1; u <= n; u++) {
        for(int &v : adj[u]) if (root(v) != root(u)) {
            g[root(u)]++, g[root(v)]++;
        }
    }
    int leaf = 0;
    for(int i = 1; i <= n; i++) if (g[i] == 2) leaf++;
    cout << (leaf + 1) / 2;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:47:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |         freopen ("task.inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:48:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         freopen ("task.out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:51:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:52:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |         freopen (task".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5032 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 3 ms 5036 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5080 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5040 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5036 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5032 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 3 ms 5036 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5080 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5040 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5036 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 2 ms 5076 KB Output is correct
26 Correct 5 ms 5716 KB Output is correct
27 Correct 5 ms 5716 KB Output is correct
28 Correct 4 ms 5936 KB Output is correct
29 Correct 4 ms 5844 KB Output is correct
30 Correct 5 ms 5688 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 4 ms 5940 KB Output is correct
33 Correct 3 ms 5032 KB Output is correct
34 Correct 4 ms 5716 KB Output is correct
35 Correct 4 ms 5812 KB Output is correct
36 Correct 4 ms 5688 KB Output is correct
37 Correct 5 ms 5716 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 4 ms 5716 KB Output is correct
40 Correct 5 ms 5716 KB Output is correct
41 Correct 4 ms 5716 KB Output is correct
42 Correct 6 ms 5716 KB Output is correct
43 Correct 4 ms 5948 KB Output is correct
44 Correct 3 ms 5076 KB Output is correct
45 Correct 4 ms 5816 KB Output is correct
46 Correct 4 ms 5732 KB Output is correct
47 Correct 3 ms 5036 KB Output is correct
48 Correct 5 ms 5716 KB Output is correct
49 Correct 4 ms 5844 KB Output is correct
50 Correct 4 ms 5816 KB Output is correct
51 Correct 5 ms 5688 KB Output is correct
52 Correct 4 ms 5684 KB Output is correct
53 Correct 5 ms 5768 KB Output is correct
54 Correct 4 ms 5716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5032 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 3 ms 5036 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5080 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5040 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5036 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 53 ms 26628 KB Output is correct
27 Correct 74 ms 26256 KB Output is correct
28 Correct 4 ms 5716 KB Output is correct
29 Correct 3 ms 5076 KB Output is correct
30 Correct 3 ms 5076 KB Output is correct
31 Correct 67 ms 26400 KB Output is correct
32 Correct 4 ms 5716 KB Output is correct
33 Correct 90 ms 31876 KB Output is correct
34 Correct 76 ms 26284 KB Output is correct
35 Correct 5 ms 5716 KB Output is correct
36 Correct 85 ms 26920 KB Output is correct
37 Correct 4 ms 5756 KB Output is correct
38 Correct 4 ms 5716 KB Output is correct
39 Correct 57 ms 26484 KB Output is correct
40 Correct 4 ms 5864 KB Output is correct
41 Correct 72 ms 26088 KB Output is correct
42 Correct 78 ms 28080 KB Output is correct
43 Correct 3 ms 5032 KB Output is correct
44 Correct 81 ms 32016 KB Output is correct
45 Correct 74 ms 29152 KB Output is correct
46 Correct 4 ms 5716 KB Output is correct
47 Correct 4 ms 5716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 26560 KB Output is correct
2 Correct 64 ms 29800 KB Output is correct
3 Correct 5 ms 5812 KB Output is correct
4 Correct 4 ms 5716 KB Output is correct
5 Correct 3 ms 5032 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 4 ms 5716 KB Output is correct
8 Correct 91 ms 28080 KB Output is correct
9 Correct 5 ms 5716 KB Output is correct
10 Correct 82 ms 26812 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 73 ms 26668 KB Output is correct
13 Correct 86 ms 27908 KB Output is correct
14 Correct 80 ms 29452 KB Output is correct
15 Correct 58 ms 26548 KB Output is correct
16 Correct 4 ms 5716 KB Output is correct
17 Correct 3 ms 5204 KB Output is correct
18 Correct 68 ms 29548 KB Output is correct
19 Correct 93 ms 32560 KB Output is correct
20 Correct 4 ms 5716 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 65 ms 28100 KB Output is correct
23 Correct 5 ms 5688 KB Output is correct
24 Correct 83 ms 27032 KB Output is correct
25 Correct 87 ms 31484 KB Output is correct
26 Correct 4 ms 5844 KB Output is correct
27 Correct 4 ms 5844 KB Output is correct
28 Correct 4 ms 5688 KB Output is correct
29 Correct 4 ms 5684 KB Output is correct
30 Correct 4 ms 5716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5032 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 3 ms 5036 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5080 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5040 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5036 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 2 ms 5076 KB Output is correct
26 Correct 5 ms 5716 KB Output is correct
27 Correct 5 ms 5716 KB Output is correct
28 Correct 4 ms 5936 KB Output is correct
29 Correct 4 ms 5844 KB Output is correct
30 Correct 5 ms 5688 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 4 ms 5940 KB Output is correct
33 Correct 3 ms 5032 KB Output is correct
34 Correct 4 ms 5716 KB Output is correct
35 Correct 4 ms 5812 KB Output is correct
36 Correct 4 ms 5688 KB Output is correct
37 Correct 5 ms 5716 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 4 ms 5716 KB Output is correct
40 Correct 5 ms 5716 KB Output is correct
41 Correct 4 ms 5716 KB Output is correct
42 Correct 6 ms 5716 KB Output is correct
43 Correct 4 ms 5948 KB Output is correct
44 Correct 3 ms 5076 KB Output is correct
45 Correct 4 ms 5816 KB Output is correct
46 Correct 4 ms 5732 KB Output is correct
47 Correct 3 ms 5036 KB Output is correct
48 Correct 5 ms 5716 KB Output is correct
49 Correct 4 ms 5844 KB Output is correct
50 Correct 4 ms 5816 KB Output is correct
51 Correct 5 ms 5688 KB Output is correct
52 Correct 4 ms 5684 KB Output is correct
53 Correct 5 ms 5768 KB Output is correct
54 Correct 4 ms 5716 KB Output is correct
55 Correct 3 ms 5076 KB Output is correct
56 Correct 53 ms 26628 KB Output is correct
57 Correct 74 ms 26256 KB Output is correct
58 Correct 4 ms 5716 KB Output is correct
59 Correct 3 ms 5076 KB Output is correct
60 Correct 3 ms 5076 KB Output is correct
61 Correct 67 ms 26400 KB Output is correct
62 Correct 4 ms 5716 KB Output is correct
63 Correct 90 ms 31876 KB Output is correct
64 Correct 76 ms 26284 KB Output is correct
65 Correct 5 ms 5716 KB Output is correct
66 Correct 85 ms 26920 KB Output is correct
67 Correct 4 ms 5756 KB Output is correct
68 Correct 4 ms 5716 KB Output is correct
69 Correct 57 ms 26484 KB Output is correct
70 Correct 4 ms 5864 KB Output is correct
71 Correct 72 ms 26088 KB Output is correct
72 Correct 78 ms 28080 KB Output is correct
73 Correct 3 ms 5032 KB Output is correct
74 Correct 81 ms 32016 KB Output is correct
75 Correct 74 ms 29152 KB Output is correct
76 Correct 4 ms 5716 KB Output is correct
77 Correct 4 ms 5716 KB Output is correct
78 Correct 52 ms 26560 KB Output is correct
79 Correct 64 ms 29800 KB Output is correct
80 Correct 5 ms 5812 KB Output is correct
81 Correct 4 ms 5716 KB Output is correct
82 Correct 3 ms 5032 KB Output is correct
83 Correct 3 ms 5076 KB Output is correct
84 Correct 4 ms 5716 KB Output is correct
85 Correct 91 ms 28080 KB Output is correct
86 Correct 5 ms 5716 KB Output is correct
87 Correct 82 ms 26812 KB Output is correct
88 Correct 3 ms 5076 KB Output is correct
89 Correct 73 ms 26668 KB Output is correct
90 Correct 86 ms 27908 KB Output is correct
91 Correct 80 ms 29452 KB Output is correct
92 Correct 58 ms 26548 KB Output is correct
93 Correct 4 ms 5716 KB Output is correct
94 Correct 3 ms 5204 KB Output is correct
95 Correct 68 ms 29548 KB Output is correct
96 Correct 93 ms 32560 KB Output is correct
97 Correct 4 ms 5716 KB Output is correct
98 Correct 3 ms 5076 KB Output is correct
99 Correct 65 ms 28100 KB Output is correct
100 Correct 5 ms 5688 KB Output is correct
101 Correct 83 ms 27032 KB Output is correct
102 Correct 87 ms 31484 KB Output is correct
103 Correct 4 ms 5844 KB Output is correct
104 Correct 4 ms 5844 KB Output is correct
105 Correct 4 ms 5688 KB Output is correct
106 Correct 4 ms 5684 KB Output is correct
107 Correct 4 ms 5716 KB Output is correct
108 Runtime error 166 ms 71612 KB Execution killed with signal 11
109 Halted 0 ms 0 KB -