Submission #128979

# Submission time Handle Problem Language Result Execution time Memory
128979 2019-07-11T11:43:58 Z PeppaPig Mergers (JOI19_mergers) C++14
100 / 100
2067 ms 127060 KB
#include <bits/stdc++.h>
 
using namespace std;
 
const int N = 5e5+5;
 
int n, k, ans;
int qs[N], col[N], deg[N];
vector<int> g[N], st[N];
vector<pair<int, int> > E;
 
int par[N][20], dep[N];
 
void init_lca(int u, int p) {
    par[u][0] = p, dep[u] = dep[p] + 1;
    for(int i = 1; i < 20; i++) par[u][i] = par[par[u][i-1]][i-1];
    for(int v : g[u]) if(v != p) init_lca(v, u);
}
 
int lca(int a, int b) {
    if(dep[a] < dep[b]) swap(a, b);
    for(int i = 19; ~i; i--) if(dep[par[a][i]] >= dep[b]) a = par[a][i];
    if(a == b) return a;
    for(int i = 19; ~i; i--) if(par[a][i] != par[b][i]) a = par[a][i], b = par[b][i];
    return par[a][0];
}
 
 
void solve_qs(int u, int p) {
    for(int v : g[u]) if(v != p) {
        solve_qs(v, u);
        qs[u] += qs[v];
    }
}
 
void flood(int u, int c) {
    col[u] = c;
    for(int v : g[u]) {
        if((!qs[v] && par[v][0] == u) || (par[u][0] == v && !qs[u])) continue;
        if(!col[v]) flood(v, c);
    }
}
 
 
int main() {
    scanf("%d %d", &n, &k);
    for(int i = 1, a, b; i < n; i++) {
        scanf("%d %d", &a, &b);
        g[a].emplace_back(b);
        g[b].emplace_back(a);
        E.emplace_back(a, b);
    }
    init_lca(1, 0);
    for(int i = 1, a; i <= n; i++) {
        scanf("%d", &a);
        st[a].emplace_back(i);
    }
    for(int i = 1; i <= k; i++) for(int j = 1; j < st[i].size(); j++) {
        int l = lca(st[i][j-1], st[i][j]);
        ++qs[st[i][j-1]];
        ++qs[st[i][j]];
        qs[l] -= 2;
    }
    solve_qs(1, 0);
    for(int i = 1, ptr = 0; i <= n; i++) if(!col[i]) flood(i, ++ptr);
    for(pair<int, int> p : E) {
        int a, b; tie(a, b) = p;
        if(col[a] != col[b]) ++deg[col[a]], ++deg[col[b]];
    }
    for(int i = 1; i <= n; i++) if(deg[i] == 1) ++ans;
    printf("%d\n", (ans + 1) / 2);
 
    return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:58:50: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 1; i <= k; i++) for(int j = 1; j < st[i].size(); j++) {
                                                ~~^~~~~~~~~~~~~~
mergers.cpp:46:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &k);
     ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:48:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &a, &b);
         ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:55:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &a);
         ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB Output is correct
2 Correct 23 ms 23800 KB Output is correct
3 Correct 24 ms 23900 KB Output is correct
4 Correct 28 ms 23804 KB Output is correct
5 Correct 23 ms 23800 KB Output is correct
6 Correct 23 ms 23800 KB Output is correct
7 Correct 24 ms 23800 KB Output is correct
8 Correct 23 ms 23800 KB Output is correct
9 Correct 23 ms 23800 KB Output is correct
10 Correct 23 ms 23800 KB Output is correct
11 Correct 24 ms 23800 KB Output is correct
12 Correct 24 ms 23800 KB Output is correct
13 Correct 23 ms 23928 KB Output is correct
14 Correct 24 ms 23800 KB Output is correct
15 Correct 24 ms 23928 KB Output is correct
16 Correct 23 ms 23800 KB Output is correct
17 Correct 24 ms 23800 KB Output is correct
18 Correct 23 ms 23928 KB Output is correct
19 Correct 23 ms 23840 KB Output is correct
20 Correct 23 ms 23800 KB Output is correct
21 Correct 23 ms 23900 KB Output is correct
22 Correct 23 ms 23804 KB Output is correct
23 Correct 23 ms 23800 KB Output is correct
24 Correct 23 ms 23800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB Output is correct
2 Correct 23 ms 23800 KB Output is correct
3 Correct 24 ms 23900 KB Output is correct
4 Correct 28 ms 23804 KB Output is correct
5 Correct 23 ms 23800 KB Output is correct
6 Correct 23 ms 23800 KB Output is correct
7 Correct 24 ms 23800 KB Output is correct
8 Correct 23 ms 23800 KB Output is correct
9 Correct 23 ms 23800 KB Output is correct
10 Correct 23 ms 23800 KB Output is correct
11 Correct 24 ms 23800 KB Output is correct
12 Correct 24 ms 23800 KB Output is correct
13 Correct 23 ms 23928 KB Output is correct
14 Correct 24 ms 23800 KB Output is correct
15 Correct 24 ms 23928 KB Output is correct
16 Correct 23 ms 23800 KB Output is correct
17 Correct 24 ms 23800 KB Output is correct
18 Correct 23 ms 23928 KB Output is correct
19 Correct 23 ms 23840 KB Output is correct
20 Correct 23 ms 23800 KB Output is correct
21 Correct 23 ms 23900 KB Output is correct
22 Correct 23 ms 23804 KB Output is correct
23 Correct 23 ms 23800 KB Output is correct
24 Correct 23 ms 23800 KB Output is correct
25 Correct 24 ms 23928 KB Output is correct
26 Correct 27 ms 24312 KB Output is correct
27 Correct 27 ms 24312 KB Output is correct
28 Correct 27 ms 24440 KB Output is correct
29 Correct 26 ms 24440 KB Output is correct
30 Correct 27 ms 24312 KB Output is correct
31 Correct 24 ms 23800 KB Output is correct
32 Correct 26 ms 24572 KB Output is correct
33 Correct 24 ms 23928 KB Output is correct
34 Correct 26 ms 24312 KB Output is correct
35 Correct 26 ms 24440 KB Output is correct
36 Correct 26 ms 24312 KB Output is correct
37 Correct 26 ms 24440 KB Output is correct
38 Correct 24 ms 23928 KB Output is correct
39 Correct 27 ms 24312 KB Output is correct
40 Correct 27 ms 24284 KB Output is correct
41 Correct 27 ms 24312 KB Output is correct
42 Correct 27 ms 24312 KB Output is correct
43 Correct 27 ms 24404 KB Output is correct
44 Correct 24 ms 23800 KB Output is correct
45 Correct 26 ms 24444 KB Output is correct
46 Correct 27 ms 24312 KB Output is correct
47 Correct 25 ms 23928 KB Output is correct
48 Correct 27 ms 24312 KB Output is correct
49 Correct 26 ms 24312 KB Output is correct
50 Correct 27 ms 24440 KB Output is correct
51 Correct 27 ms 24312 KB Output is correct
52 Correct 26 ms 24312 KB Output is correct
53 Correct 26 ms 24312 KB Output is correct
54 Correct 27 ms 24440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB Output is correct
2 Correct 23 ms 23800 KB Output is correct
3 Correct 24 ms 23900 KB Output is correct
4 Correct 28 ms 23804 KB Output is correct
5 Correct 23 ms 23800 KB Output is correct
6 Correct 23 ms 23800 KB Output is correct
7 Correct 24 ms 23800 KB Output is correct
8 Correct 23 ms 23800 KB Output is correct
9 Correct 23 ms 23800 KB Output is correct
10 Correct 23 ms 23800 KB Output is correct
11 Correct 24 ms 23800 KB Output is correct
12 Correct 24 ms 23800 KB Output is correct
13 Correct 23 ms 23928 KB Output is correct
14 Correct 24 ms 23800 KB Output is correct
15 Correct 24 ms 23928 KB Output is correct
16 Correct 23 ms 23800 KB Output is correct
17 Correct 24 ms 23800 KB Output is correct
18 Correct 23 ms 23928 KB Output is correct
19 Correct 23 ms 23840 KB Output is correct
20 Correct 23 ms 23800 KB Output is correct
21 Correct 23 ms 23900 KB Output is correct
22 Correct 23 ms 23804 KB Output is correct
23 Correct 23 ms 23800 KB Output is correct
24 Correct 23 ms 23800 KB Output is correct
25 Correct 23 ms 23928 KB Output is correct
26 Correct 128 ms 38592 KB Output is correct
27 Correct 199 ms 38352 KB Output is correct
28 Correct 27 ms 24224 KB Output is correct
29 Correct 28 ms 23928 KB Output is correct
30 Correct 24 ms 23928 KB Output is correct
31 Correct 172 ms 38252 KB Output is correct
32 Correct 27 ms 24312 KB Output is correct
33 Correct 291 ms 44012 KB Output is correct
34 Correct 169 ms 38664 KB Output is correct
35 Correct 30 ms 24216 KB Output is correct
36 Correct 261 ms 39148 KB Output is correct
37 Correct 27 ms 24312 KB Output is correct
38 Correct 31 ms 24312 KB Output is correct
39 Correct 149 ms 38912 KB Output is correct
40 Correct 26 ms 24440 KB Output is correct
41 Correct 188 ms 39132 KB Output is correct
42 Correct 247 ms 41068 KB Output is correct
43 Correct 24 ms 23800 KB Output is correct
44 Correct 282 ms 44908 KB Output is correct
45 Correct 260 ms 41496 KB Output is correct
46 Correct 26 ms 24312 KB Output is correct
47 Correct 27 ms 24312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 154 ms 38112 KB Output is correct
2 Correct 129 ms 40676 KB Output is correct
3 Correct 26 ms 24312 KB Output is correct
4 Correct 26 ms 24312 KB Output is correct
5 Correct 23 ms 23928 KB Output is correct
6 Correct 23 ms 23928 KB Output is correct
7 Correct 25 ms 24312 KB Output is correct
8 Correct 208 ms 39444 KB Output is correct
9 Correct 32 ms 24312 KB Output is correct
10 Correct 178 ms 38380 KB Output is correct
11 Correct 24 ms 23928 KB Output is correct
12 Correct 271 ms 38384 KB Output is correct
13 Correct 203 ms 39388 KB Output is correct
14 Correct 172 ms 41456 KB Output is correct
15 Correct 135 ms 38552 KB Output is correct
16 Correct 26 ms 24312 KB Output is correct
17 Correct 23 ms 23800 KB Output is correct
18 Correct 147 ms 41196 KB Output is correct
19 Correct 187 ms 44552 KB Output is correct
20 Correct 27 ms 24312 KB Output is correct
21 Correct 23 ms 23800 KB Output is correct
22 Correct 136 ms 39832 KB Output is correct
23 Correct 27 ms 24316 KB Output is correct
24 Correct 192 ms 38784 KB Output is correct
25 Correct 182 ms 43412 KB Output is correct
26 Correct 92 ms 24440 KB Output is correct
27 Correct 26 ms 24568 KB Output is correct
28 Correct 27 ms 24312 KB Output is correct
29 Correct 25 ms 24312 KB Output is correct
30 Correct 27 ms 24440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB Output is correct
2 Correct 23 ms 23800 KB Output is correct
3 Correct 24 ms 23900 KB Output is correct
4 Correct 28 ms 23804 KB Output is correct
5 Correct 23 ms 23800 KB Output is correct
6 Correct 23 ms 23800 KB Output is correct
7 Correct 24 ms 23800 KB Output is correct
8 Correct 23 ms 23800 KB Output is correct
9 Correct 23 ms 23800 KB Output is correct
10 Correct 23 ms 23800 KB Output is correct
11 Correct 24 ms 23800 KB Output is correct
12 Correct 24 ms 23800 KB Output is correct
13 Correct 23 ms 23928 KB Output is correct
14 Correct 24 ms 23800 KB Output is correct
15 Correct 24 ms 23928 KB Output is correct
16 Correct 23 ms 23800 KB Output is correct
17 Correct 24 ms 23800 KB Output is correct
18 Correct 23 ms 23928 KB Output is correct
19 Correct 23 ms 23840 KB Output is correct
20 Correct 23 ms 23800 KB Output is correct
21 Correct 23 ms 23900 KB Output is correct
22 Correct 23 ms 23804 KB Output is correct
23 Correct 23 ms 23800 KB Output is correct
24 Correct 23 ms 23800 KB Output is correct
25 Correct 24 ms 23928 KB Output is correct
26 Correct 27 ms 24312 KB Output is correct
27 Correct 27 ms 24312 KB Output is correct
28 Correct 27 ms 24440 KB Output is correct
29 Correct 26 ms 24440 KB Output is correct
30 Correct 27 ms 24312 KB Output is correct
31 Correct 24 ms 23800 KB Output is correct
32 Correct 26 ms 24572 KB Output is correct
33 Correct 24 ms 23928 KB Output is correct
34 Correct 26 ms 24312 KB Output is correct
35 Correct 26 ms 24440 KB Output is correct
36 Correct 26 ms 24312 KB Output is correct
37 Correct 26 ms 24440 KB Output is correct
38 Correct 24 ms 23928 KB Output is correct
39 Correct 27 ms 24312 KB Output is correct
40 Correct 27 ms 24284 KB Output is correct
41 Correct 27 ms 24312 KB Output is correct
42 Correct 27 ms 24312 KB Output is correct
43 Correct 27 ms 24404 KB Output is correct
44 Correct 24 ms 23800 KB Output is correct
45 Correct 26 ms 24444 KB Output is correct
46 Correct 27 ms 24312 KB Output is correct
47 Correct 25 ms 23928 KB Output is correct
48 Correct 27 ms 24312 KB Output is correct
49 Correct 26 ms 24312 KB Output is correct
50 Correct 27 ms 24440 KB Output is correct
51 Correct 27 ms 24312 KB Output is correct
52 Correct 26 ms 24312 KB Output is correct
53 Correct 26 ms 24312 KB Output is correct
54 Correct 27 ms 24440 KB Output is correct
55 Correct 23 ms 23928 KB Output is correct
56 Correct 128 ms 38592 KB Output is correct
57 Correct 199 ms 38352 KB Output is correct
58 Correct 27 ms 24224 KB Output is correct
59 Correct 28 ms 23928 KB Output is correct
60 Correct 24 ms 23928 KB Output is correct
61 Correct 172 ms 38252 KB Output is correct
62 Correct 27 ms 24312 KB Output is correct
63 Correct 291 ms 44012 KB Output is correct
64 Correct 169 ms 38664 KB Output is correct
65 Correct 30 ms 24216 KB Output is correct
66 Correct 261 ms 39148 KB Output is correct
67 Correct 27 ms 24312 KB Output is correct
68 Correct 31 ms 24312 KB Output is correct
69 Correct 149 ms 38912 KB Output is correct
70 Correct 26 ms 24440 KB Output is correct
71 Correct 188 ms 39132 KB Output is correct
72 Correct 247 ms 41068 KB Output is correct
73 Correct 24 ms 23800 KB Output is correct
74 Correct 282 ms 44908 KB Output is correct
75 Correct 260 ms 41496 KB Output is correct
76 Correct 26 ms 24312 KB Output is correct
77 Correct 27 ms 24312 KB Output is correct
78 Correct 154 ms 38112 KB Output is correct
79 Correct 129 ms 40676 KB Output is correct
80 Correct 26 ms 24312 KB Output is correct
81 Correct 26 ms 24312 KB Output is correct
82 Correct 23 ms 23928 KB Output is correct
83 Correct 23 ms 23928 KB Output is correct
84 Correct 25 ms 24312 KB Output is correct
85 Correct 208 ms 39444 KB Output is correct
86 Correct 32 ms 24312 KB Output is correct
87 Correct 178 ms 38380 KB Output is correct
88 Correct 24 ms 23928 KB Output is correct
89 Correct 271 ms 38384 KB Output is correct
90 Correct 203 ms 39388 KB Output is correct
91 Correct 172 ms 41456 KB Output is correct
92 Correct 135 ms 38552 KB Output is correct
93 Correct 26 ms 24312 KB Output is correct
94 Correct 23 ms 23800 KB Output is correct
95 Correct 147 ms 41196 KB Output is correct
96 Correct 187 ms 44552 KB Output is correct
97 Correct 27 ms 24312 KB Output is correct
98 Correct 23 ms 23800 KB Output is correct
99 Correct 136 ms 39832 KB Output is correct
100 Correct 27 ms 24316 KB Output is correct
101 Correct 192 ms 38784 KB Output is correct
102 Correct 182 ms 43412 KB Output is correct
103 Correct 92 ms 24440 KB Output is correct
104 Correct 26 ms 24568 KB Output is correct
105 Correct 27 ms 24312 KB Output is correct
106 Correct 25 ms 24312 KB Output is correct
107 Correct 27 ms 24440 KB Output is correct
108 Correct 825 ms 99160 KB Output is correct
109 Correct 1997 ms 109080 KB Output is correct
110 Correct 2067 ms 118260 KB Output is correct
111 Correct 1220 ms 127060 KB Output is correct
112 Correct 1226 ms 118492 KB Output is correct
113 Correct 888 ms 108212 KB Output is correct
114 Correct 1094 ms 94556 KB Output is correct
115 Correct 1106 ms 95060 KB Output is correct
116 Correct 1286 ms 96116 KB Output is correct
117 Correct 1014 ms 108800 KB Output is correct
118 Correct 1066 ms 93036 KB Output is correct
119 Correct 999 ms 108832 KB Output is correct
120 Correct 1244 ms 119816 KB Output is correct
121 Correct 1028 ms 108736 KB Output is correct
122 Correct 1185 ms 100916 KB Output is correct
123 Correct 720 ms 108208 KB Output is correct
124 Correct 1205 ms 106240 KB Output is correct