Submission #128983

# Submission time Handle Problem Language Result Execution time Memory
128983 2019-07-11T11:47:06 Z PeppaPig Mergers (JOI19_mergers) C++14
100 / 100
2278 ms 125056 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 24 ms 23800 KB Output is correct
2 Correct 23 ms 23928 KB Output is correct
3 Correct 23 ms 23928 KB Output is correct
4 Correct 24 ms 23800 KB Output is correct
5 Correct 23 ms 23872 KB Output is correct
6 Correct 24 ms 23800 KB Output is correct
7 Correct 23 ms 23800 KB Output is correct
8 Correct 23 ms 23928 KB Output is correct
9 Correct 23 ms 23800 KB Output is correct
10 Correct 26 ms 23824 KB Output is correct
11 Correct 29 ms 23928 KB Output is correct
12 Correct 23 ms 23800 KB Output is correct
13 Correct 23 ms 23800 KB Output is correct
14 Correct 23 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 23800 KB Output is correct
20 Correct 24 ms 23800 KB Output is correct
21 Correct 24 ms 23800 KB Output is correct
22 Correct 23 ms 23928 KB Output is correct
23 Correct 24 ms 23800 KB Output is correct
24 Correct 23 ms 23800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23800 KB Output is correct
2 Correct 23 ms 23928 KB Output is correct
3 Correct 23 ms 23928 KB Output is correct
4 Correct 24 ms 23800 KB Output is correct
5 Correct 23 ms 23872 KB Output is correct
6 Correct 24 ms 23800 KB Output is correct
7 Correct 23 ms 23800 KB Output is correct
8 Correct 23 ms 23928 KB Output is correct
9 Correct 23 ms 23800 KB Output is correct
10 Correct 26 ms 23824 KB Output is correct
11 Correct 29 ms 23928 KB Output is correct
12 Correct 23 ms 23800 KB Output is correct
13 Correct 23 ms 23800 KB Output is correct
14 Correct 23 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 23800 KB Output is correct
20 Correct 24 ms 23800 KB Output is correct
21 Correct 24 ms 23800 KB Output is correct
22 Correct 23 ms 23928 KB Output is correct
23 Correct 24 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 26 ms 24312 KB Output is correct
27 Correct 26 ms 24312 KB Output is correct
28 Correct 27 ms 24440 KB Output is correct
29 Correct 26 ms 24312 KB Output is correct
30 Correct 27 ms 24312 KB Output is correct
31 Correct 29 ms 23848 KB Output is correct
32 Correct 26 ms 24568 KB Output is correct
33 Correct 23 ms 23892 KB Output is correct
34 Correct 25 ms 24316 KB Output is correct
35 Correct 27 ms 24440 KB Output is correct
36 Correct 26 ms 24312 KB Output is correct
37 Correct 27 ms 24312 KB Output is correct
38 Correct 23 ms 23800 KB Output is correct
39 Correct 27 ms 24312 KB Output is correct
40 Correct 26 ms 24312 KB Output is correct
41 Correct 26 ms 24288 KB Output is correct
42 Correct 26 ms 24312 KB Output is correct
43 Correct 32 ms 24440 KB Output is correct
44 Correct 28 ms 23800 KB Output is correct
45 Correct 25 ms 24440 KB Output is correct
46 Correct 31 ms 24312 KB Output is correct
47 Correct 24 ms 23800 KB Output is correct
48 Correct 26 ms 24312 KB Output is correct
49 Correct 26 ms 24412 KB Output is correct
50 Correct 26 ms 24440 KB Output is correct
51 Correct 26 ms 24312 KB Output is correct
52 Correct 26 ms 24312 KB Output is correct
53 Correct 26 ms 24256 KB Output is correct
54 Correct 27 ms 24312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23800 KB Output is correct
2 Correct 23 ms 23928 KB Output is correct
3 Correct 23 ms 23928 KB Output is correct
4 Correct 24 ms 23800 KB Output is correct
5 Correct 23 ms 23872 KB Output is correct
6 Correct 24 ms 23800 KB Output is correct
7 Correct 23 ms 23800 KB Output is correct
8 Correct 23 ms 23928 KB Output is correct
9 Correct 23 ms 23800 KB Output is correct
10 Correct 26 ms 23824 KB Output is correct
11 Correct 29 ms 23928 KB Output is correct
12 Correct 23 ms 23800 KB Output is correct
13 Correct 23 ms 23800 KB Output is correct
14 Correct 23 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 23800 KB Output is correct
20 Correct 24 ms 23800 KB Output is correct
21 Correct 24 ms 23800 KB Output is correct
22 Correct 23 ms 23928 KB Output is correct
23 Correct 24 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 133 ms 37940 KB Output is correct
27 Correct 177 ms 37740 KB Output is correct
28 Correct 26 ms 24284 KB Output is correct
29 Correct 23 ms 23928 KB Output is correct
30 Correct 24 ms 23800 KB Output is correct
31 Correct 175 ms 37612 KB Output is correct
32 Correct 26 ms 24312 KB Output is correct
33 Correct 275 ms 43344 KB Output is correct
34 Correct 239 ms 37480 KB Output is correct
35 Correct 29 ms 24328 KB Output is correct
36 Correct 229 ms 38048 KB Output is correct
37 Correct 26 ms 24316 KB Output is correct
38 Correct 27 ms 24312 KB Output is correct
39 Correct 139 ms 37864 KB Output is correct
40 Correct 27 ms 24440 KB Output is correct
41 Correct 179 ms 37952 KB Output is correct
42 Correct 280 ms 40172 KB Output is correct
43 Correct 24 ms 23932 KB Output is correct
44 Correct 286 ms 43908 KB Output is correct
45 Correct 237 ms 40428 KB Output is correct
46 Correct 26 ms 24244 KB Output is correct
47 Correct 27 ms 24312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 137 ms 37764 KB Output is correct
2 Correct 129 ms 40420 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 23800 KB Output is correct
6 Correct 24 ms 23928 KB Output is correct
7 Correct 26 ms 24312 KB Output is correct
8 Correct 195 ms 38888 KB Output is correct
9 Correct 27 ms 24312 KB Output is correct
10 Correct 177 ms 37740 KB Output is correct
11 Correct 23 ms 23800 KB Output is correct
12 Correct 206 ms 37676 KB Output is correct
13 Correct 195 ms 38700 KB Output is correct
14 Correct 163 ms 40684 KB Output is correct
15 Correct 144 ms 37972 KB Output is correct
16 Correct 26 ms 24316 KB Output is correct
17 Correct 23 ms 23928 KB Output is correct
18 Correct 151 ms 40524 KB Output is correct
19 Correct 198 ms 43752 KB Output is correct
20 Correct 27 ms 24312 KB Output is correct
21 Correct 24 ms 23928 KB Output is correct
22 Correct 148 ms 39076 KB Output is correct
23 Correct 26 ms 24312 KB Output is correct
24 Correct 207 ms 37948 KB Output is correct
25 Correct 174 ms 42700 KB Output is correct
26 Correct 28 ms 24440 KB Output is correct
27 Correct 35 ms 24440 KB Output is correct
28 Correct 27 ms 24312 KB Output is correct
29 Correct 26 ms 24304 KB Output is correct
30 Correct 31 ms 24312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23800 KB Output is correct
2 Correct 23 ms 23928 KB Output is correct
3 Correct 23 ms 23928 KB Output is correct
4 Correct 24 ms 23800 KB Output is correct
5 Correct 23 ms 23872 KB Output is correct
6 Correct 24 ms 23800 KB Output is correct
7 Correct 23 ms 23800 KB Output is correct
8 Correct 23 ms 23928 KB Output is correct
9 Correct 23 ms 23800 KB Output is correct
10 Correct 26 ms 23824 KB Output is correct
11 Correct 29 ms 23928 KB Output is correct
12 Correct 23 ms 23800 KB Output is correct
13 Correct 23 ms 23800 KB Output is correct
14 Correct 23 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 23800 KB Output is correct
20 Correct 24 ms 23800 KB Output is correct
21 Correct 24 ms 23800 KB Output is correct
22 Correct 23 ms 23928 KB Output is correct
23 Correct 24 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 26 ms 24312 KB Output is correct
27 Correct 26 ms 24312 KB Output is correct
28 Correct 27 ms 24440 KB Output is correct
29 Correct 26 ms 24312 KB Output is correct
30 Correct 27 ms 24312 KB Output is correct
31 Correct 29 ms 23848 KB Output is correct
32 Correct 26 ms 24568 KB Output is correct
33 Correct 23 ms 23892 KB Output is correct
34 Correct 25 ms 24316 KB Output is correct
35 Correct 27 ms 24440 KB Output is correct
36 Correct 26 ms 24312 KB Output is correct
37 Correct 27 ms 24312 KB Output is correct
38 Correct 23 ms 23800 KB Output is correct
39 Correct 27 ms 24312 KB Output is correct
40 Correct 26 ms 24312 KB Output is correct
41 Correct 26 ms 24288 KB Output is correct
42 Correct 26 ms 24312 KB Output is correct
43 Correct 32 ms 24440 KB Output is correct
44 Correct 28 ms 23800 KB Output is correct
45 Correct 25 ms 24440 KB Output is correct
46 Correct 31 ms 24312 KB Output is correct
47 Correct 24 ms 23800 KB Output is correct
48 Correct 26 ms 24312 KB Output is correct
49 Correct 26 ms 24412 KB Output is correct
50 Correct 26 ms 24440 KB Output is correct
51 Correct 26 ms 24312 KB Output is correct
52 Correct 26 ms 24312 KB Output is correct
53 Correct 26 ms 24256 KB Output is correct
54 Correct 27 ms 24312 KB Output is correct
55 Correct 23 ms 23928 KB Output is correct
56 Correct 133 ms 37940 KB Output is correct
57 Correct 177 ms 37740 KB Output is correct
58 Correct 26 ms 24284 KB Output is correct
59 Correct 23 ms 23928 KB Output is correct
60 Correct 24 ms 23800 KB Output is correct
61 Correct 175 ms 37612 KB Output is correct
62 Correct 26 ms 24312 KB Output is correct
63 Correct 275 ms 43344 KB Output is correct
64 Correct 239 ms 37480 KB Output is correct
65 Correct 29 ms 24328 KB Output is correct
66 Correct 229 ms 38048 KB Output is correct
67 Correct 26 ms 24316 KB Output is correct
68 Correct 27 ms 24312 KB Output is correct
69 Correct 139 ms 37864 KB Output is correct
70 Correct 27 ms 24440 KB Output is correct
71 Correct 179 ms 37952 KB Output is correct
72 Correct 280 ms 40172 KB Output is correct
73 Correct 24 ms 23932 KB Output is correct
74 Correct 286 ms 43908 KB Output is correct
75 Correct 237 ms 40428 KB Output is correct
76 Correct 26 ms 24244 KB Output is correct
77 Correct 27 ms 24312 KB Output is correct
78 Correct 137 ms 37764 KB Output is correct
79 Correct 129 ms 40420 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 23800 KB Output is correct
83 Correct 24 ms 23928 KB Output is correct
84 Correct 26 ms 24312 KB Output is correct
85 Correct 195 ms 38888 KB Output is correct
86 Correct 27 ms 24312 KB Output is correct
87 Correct 177 ms 37740 KB Output is correct
88 Correct 23 ms 23800 KB Output is correct
89 Correct 206 ms 37676 KB Output is correct
90 Correct 195 ms 38700 KB Output is correct
91 Correct 163 ms 40684 KB Output is correct
92 Correct 144 ms 37972 KB Output is correct
93 Correct 26 ms 24316 KB Output is correct
94 Correct 23 ms 23928 KB Output is correct
95 Correct 151 ms 40524 KB Output is correct
96 Correct 198 ms 43752 KB Output is correct
97 Correct 27 ms 24312 KB Output is correct
98 Correct 24 ms 23928 KB Output is correct
99 Correct 148 ms 39076 KB Output is correct
100 Correct 26 ms 24312 KB Output is correct
101 Correct 207 ms 37948 KB Output is correct
102 Correct 174 ms 42700 KB Output is correct
103 Correct 28 ms 24440 KB Output is correct
104 Correct 35 ms 24440 KB Output is correct
105 Correct 27 ms 24312 KB Output is correct
106 Correct 26 ms 24304 KB Output is correct
107 Correct 31 ms 24312 KB Output is correct
108 Correct 896 ms 97248 KB Output is correct
109 Correct 2181 ms 107116 KB Output is correct
110 Correct 2278 ms 116488 KB Output is correct
111 Correct 1226 ms 125056 KB Output is correct
112 Correct 1230 ms 116368 KB Output is correct
113 Correct 868 ms 106156 KB Output is correct
114 Correct 1172 ms 92412 KB Output is correct
115 Correct 1115 ms 93104 KB Output is correct
116 Correct 1267 ms 94044 KB Output is correct
117 Correct 1002 ms 106904 KB Output is correct
118 Correct 1051 ms 90972 KB Output is correct
119 Correct 1032 ms 106752 KB Output is correct
120 Correct 1172 ms 117800 KB Output is correct
121 Correct 1051 ms 106812 KB Output is correct
122 Correct 1173 ms 99036 KB Output is correct
123 Correct 790 ms 106272 KB Output is correct
124 Correct 1114 ms 104284 KB Output is correct