# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
169117 |
2019-12-18T12:43:53 Z |
Akashi |
Mergers (JOI19_mergers) |
C++14 |
|
1337 ms |
115804 KB |
#include <bits/stdc++.h>
using namespace std;
int n, k, Sol;
int h[500005], cul[500005], up[500005], root[500005], nr[500005], nr2[500005], cnt[500005];
bool viz[500005], tip[500005], mark[500005];
int d[500005][21];
vector <int> v[500005];
priority_queue <pair <int, int> > L;
void predfs(int nod = 1){
viz[nod] = 1;
bool leaf = true;
for(auto it : v[nod]){
if(viz[it]) continue ;
leaf = false;
h[it] = h[nod] + 1;
predfs(it);
d[it][0] = nod;
}
viz[nod] = 0;
if(leaf) L.push({h[nod], nod});
}
inline int lca(int x, int y){
if(h[x] < h[y]) swap(x, y);
int dif = h[x] - h[y];
int p = 1, k = 0;
while(dif){
if(dif & p) x = d[x][k], dif ^= p;
p = p << 1; ++k;
}
if(x == y) return x;
for(int t = 20; t >= 0 ; --t)
if(d[x][t] != d[y][t]) x = d[x][t], y = d[y][t];
return d[x][0];
}
void dfs(int nod, int rad, int type){
viz[nod] = 1;
for(auto it : v[nod]){
if(viz[it]) continue ;
if(mark[it]){
dfs(it, it, 1);
nr[rad] += nr[it] + nr2[it];
++cnt[rad];
}
else dfs(it, rad, 0);
}
if(type){
if(nod == 1){
if(cnt[nod] > 1) Sol += nr[nod] / 2 + nr[nod] % 2;
else{
if(nr[nod] > 1){
Sol += (nr[nod] - 1) / 2;
if(nr[nod] % 2 == 0) nr2[nod] = 1;
nr[nod] = 1;
}
Sol += nr2[nod] + nr[nod];
}
}
else if(nr[nod] > 1){
Sol += (nr[nod] - 1) / 2;
if(nr[nod] % 2 == 0) nr2[nod] = 1;
nr[nod] = 1;
}
}
}
int main()
{
// freopen("1.in", "r", stdin);
scanf("%d%d", &n, &k);
if(n == 1 || k == 1) {printf("0"); return 0;}
int x, y;
for(int i = 1; i < n ; ++i){
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
}
predfs();
for(int t = 1; t <= 20 ; ++t)
for(int i = 1; i <= n ; ++i)
d[i][t] = d[d[i][t - 1]][t - 1];
for(int i = 1; i <= n ; ++i){
scanf("%d", &x);
cul[i] = x;
if(root[x] == 0) root[x] = i;
else root[x] = lca(root[x], i);
}
for(int i = 1; i <= n ; ++i) up[i] = root[cul[i]];
int NR = 0;
while(!L.empty()){
int nod = L.top().second;
L.pop();
int TT = d[nod][0];
tip[TT] = max(tip[nod], tip[TT]);
if(up[nod] != nod) up[TT] = lca(up[nod], up[TT]);
else{
++NR;
mark[nod] = 1;
if(!tip[nod]) tip[TT] = 1, nr[nod] = 1;
}
if(!viz[TT] && TT != 1){
viz[TT] = 1;
L.push({h[TT], TT});
}
}
if(NR == 0) printf("0");
else{
memset(viz, 0, sizeof(viz));
mark[1] = 1;
dfs(1, 1, 1);
printf("%d", Sol);
}
return 0;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:82: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:86:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:97:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &x);
~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12536 KB |
Output is correct |
2 |
Correct |
12 ms |
12664 KB |
Output is correct |
3 |
Correct |
12 ms |
12664 KB |
Output is correct |
4 |
Correct |
12 ms |
12664 KB |
Output is correct |
5 |
Correct |
12 ms |
12148 KB |
Output is correct |
6 |
Correct |
12 ms |
12024 KB |
Output is correct |
7 |
Correct |
12 ms |
12028 KB |
Output is correct |
8 |
Correct |
13 ms |
12152 KB |
Output is correct |
9 |
Correct |
12 ms |
12664 KB |
Output is correct |
10 |
Correct |
12 ms |
12664 KB |
Output is correct |
11 |
Correct |
12 ms |
12636 KB |
Output is correct |
12 |
Correct |
13 ms |
12664 KB |
Output is correct |
13 |
Correct |
12 ms |
12664 KB |
Output is correct |
14 |
Correct |
12 ms |
12664 KB |
Output is correct |
15 |
Correct |
13 ms |
12664 KB |
Output is correct |
16 |
Correct |
13 ms |
12664 KB |
Output is correct |
17 |
Correct |
13 ms |
12536 KB |
Output is correct |
18 |
Correct |
12 ms |
12152 KB |
Output is correct |
19 |
Correct |
12 ms |
12536 KB |
Output is correct |
20 |
Correct |
12 ms |
12536 KB |
Output is correct |
21 |
Correct |
12 ms |
12152 KB |
Output is correct |
22 |
Correct |
12 ms |
12152 KB |
Output is correct |
23 |
Correct |
12 ms |
12152 KB |
Output is correct |
24 |
Correct |
13 ms |
12664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12536 KB |
Output is correct |
2 |
Correct |
12 ms |
12664 KB |
Output is correct |
3 |
Correct |
12 ms |
12664 KB |
Output is correct |
4 |
Correct |
12 ms |
12664 KB |
Output is correct |
5 |
Correct |
12 ms |
12148 KB |
Output is correct |
6 |
Correct |
12 ms |
12024 KB |
Output is correct |
7 |
Correct |
12 ms |
12028 KB |
Output is correct |
8 |
Correct |
13 ms |
12152 KB |
Output is correct |
9 |
Correct |
12 ms |
12664 KB |
Output is correct |
10 |
Correct |
12 ms |
12664 KB |
Output is correct |
11 |
Correct |
12 ms |
12636 KB |
Output is correct |
12 |
Correct |
13 ms |
12664 KB |
Output is correct |
13 |
Correct |
12 ms |
12664 KB |
Output is correct |
14 |
Correct |
12 ms |
12664 KB |
Output is correct |
15 |
Correct |
13 ms |
12664 KB |
Output is correct |
16 |
Correct |
13 ms |
12664 KB |
Output is correct |
17 |
Correct |
13 ms |
12536 KB |
Output is correct |
18 |
Correct |
12 ms |
12152 KB |
Output is correct |
19 |
Correct |
12 ms |
12536 KB |
Output is correct |
20 |
Correct |
12 ms |
12536 KB |
Output is correct |
21 |
Correct |
12 ms |
12152 KB |
Output is correct |
22 |
Correct |
12 ms |
12152 KB |
Output is correct |
23 |
Correct |
12 ms |
12152 KB |
Output is correct |
24 |
Correct |
13 ms |
12664 KB |
Output is correct |
25 |
Correct |
12 ms |
12664 KB |
Output is correct |
26 |
Correct |
16 ms |
13048 KB |
Output is correct |
27 |
Correct |
16 ms |
13048 KB |
Output is correct |
28 |
Correct |
16 ms |
13148 KB |
Output is correct |
29 |
Correct |
16 ms |
13048 KB |
Output is correct |
30 |
Correct |
15 ms |
13048 KB |
Output is correct |
31 |
Correct |
13 ms |
12664 KB |
Output is correct |
32 |
Correct |
18 ms |
13176 KB |
Output is correct |
33 |
Correct |
12 ms |
12664 KB |
Output is correct |
34 |
Correct |
15 ms |
12536 KB |
Output is correct |
35 |
Correct |
15 ms |
13048 KB |
Output is correct |
36 |
Correct |
16 ms |
13052 KB |
Output is correct |
37 |
Correct |
16 ms |
13176 KB |
Output is correct |
38 |
Correct |
12 ms |
12664 KB |
Output is correct |
39 |
Correct |
16 ms |
13176 KB |
Output is correct |
40 |
Correct |
12 ms |
12152 KB |
Output is correct |
41 |
Correct |
16 ms |
13176 KB |
Output is correct |
42 |
Correct |
15 ms |
13176 KB |
Output is correct |
43 |
Correct |
12 ms |
12152 KB |
Output is correct |
44 |
Correct |
12 ms |
12664 KB |
Output is correct |
45 |
Correct |
16 ms |
13176 KB |
Output is correct |
46 |
Correct |
16 ms |
13048 KB |
Output is correct |
47 |
Correct |
13 ms |
12664 KB |
Output is correct |
48 |
Correct |
16 ms |
13176 KB |
Output is correct |
49 |
Correct |
15 ms |
13176 KB |
Output is correct |
50 |
Correct |
16 ms |
13176 KB |
Output is correct |
51 |
Correct |
16 ms |
13048 KB |
Output is correct |
52 |
Correct |
15 ms |
12664 KB |
Output is correct |
53 |
Correct |
15 ms |
13176 KB |
Output is correct |
54 |
Correct |
18 ms |
12540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12536 KB |
Output is correct |
2 |
Correct |
12 ms |
12664 KB |
Output is correct |
3 |
Correct |
12 ms |
12664 KB |
Output is correct |
4 |
Correct |
12 ms |
12664 KB |
Output is correct |
5 |
Correct |
12 ms |
12148 KB |
Output is correct |
6 |
Correct |
12 ms |
12024 KB |
Output is correct |
7 |
Correct |
12 ms |
12028 KB |
Output is correct |
8 |
Correct |
13 ms |
12152 KB |
Output is correct |
9 |
Correct |
12 ms |
12664 KB |
Output is correct |
10 |
Correct |
12 ms |
12664 KB |
Output is correct |
11 |
Correct |
12 ms |
12636 KB |
Output is correct |
12 |
Correct |
13 ms |
12664 KB |
Output is correct |
13 |
Correct |
12 ms |
12664 KB |
Output is correct |
14 |
Correct |
12 ms |
12664 KB |
Output is correct |
15 |
Correct |
13 ms |
12664 KB |
Output is correct |
16 |
Correct |
13 ms |
12664 KB |
Output is correct |
17 |
Correct |
13 ms |
12536 KB |
Output is correct |
18 |
Correct |
12 ms |
12152 KB |
Output is correct |
19 |
Correct |
12 ms |
12536 KB |
Output is correct |
20 |
Correct |
12 ms |
12536 KB |
Output is correct |
21 |
Correct |
12 ms |
12152 KB |
Output is correct |
22 |
Correct |
12 ms |
12152 KB |
Output is correct |
23 |
Correct |
12 ms |
12152 KB |
Output is correct |
24 |
Correct |
13 ms |
12664 KB |
Output is correct |
25 |
Correct |
13 ms |
12664 KB |
Output is correct |
26 |
Correct |
123 ms |
26092 KB |
Output is correct |
27 |
Correct |
165 ms |
25460 KB |
Output is correct |
28 |
Correct |
15 ms |
13048 KB |
Output is correct |
29 |
Correct |
13 ms |
12664 KB |
Output is correct |
30 |
Correct |
12 ms |
12664 KB |
Output is correct |
31 |
Correct |
164 ms |
26188 KB |
Output is correct |
32 |
Correct |
14 ms |
12540 KB |
Output is correct |
33 |
Correct |
172 ms |
32092 KB |
Output is correct |
34 |
Correct |
166 ms |
25844 KB |
Output is correct |
35 |
Correct |
16 ms |
13048 KB |
Output is correct |
36 |
Correct |
191 ms |
27896 KB |
Output is correct |
37 |
Correct |
12 ms |
12280 KB |
Output is correct |
38 |
Correct |
15 ms |
13052 KB |
Output is correct |
39 |
Correct |
125 ms |
27496 KB |
Output is correct |
40 |
Correct |
12 ms |
12228 KB |
Output is correct |
41 |
Correct |
14 ms |
12152 KB |
Output is correct |
42 |
Correct |
179 ms |
29176 KB |
Output is correct |
43 |
Correct |
13 ms |
12664 KB |
Output is correct |
44 |
Correct |
12 ms |
12024 KB |
Output is correct |
45 |
Correct |
170 ms |
30756 KB |
Output is correct |
46 |
Correct |
15 ms |
12536 KB |
Output is correct |
47 |
Correct |
15 ms |
12536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
26104 KB |
Output is correct |
2 |
Correct |
125 ms |
27332 KB |
Output is correct |
3 |
Correct |
16 ms |
13048 KB |
Output is correct |
4 |
Correct |
16 ms |
13096 KB |
Output is correct |
5 |
Correct |
13 ms |
12664 KB |
Output is correct |
6 |
Correct |
12 ms |
12664 KB |
Output is correct |
7 |
Correct |
15 ms |
12580 KB |
Output is correct |
8 |
Correct |
186 ms |
27376 KB |
Output is correct |
9 |
Correct |
16 ms |
13048 KB |
Output is correct |
10 |
Correct |
170 ms |
27120 KB |
Output is correct |
11 |
Correct |
13 ms |
12664 KB |
Output is correct |
12 |
Correct |
202 ms |
28068 KB |
Output is correct |
13 |
Correct |
185 ms |
29156 KB |
Output is correct |
14 |
Correct |
171 ms |
29268 KB |
Output is correct |
15 |
Correct |
124 ms |
27544 KB |
Output is correct |
16 |
Correct |
15 ms |
13048 KB |
Output is correct |
17 |
Correct |
15 ms |
12664 KB |
Output is correct |
18 |
Correct |
128 ms |
29116 KB |
Output is correct |
19 |
Correct |
156 ms |
32248 KB |
Output is correct |
20 |
Correct |
15 ms |
13176 KB |
Output is correct |
21 |
Correct |
13 ms |
12664 KB |
Output is correct |
22 |
Correct |
124 ms |
28780 KB |
Output is correct |
23 |
Correct |
16 ms |
13176 KB |
Output is correct |
24 |
Correct |
185 ms |
28408 KB |
Output is correct |
25 |
Correct |
182 ms |
31900 KB |
Output is correct |
26 |
Correct |
15 ms |
13176 KB |
Output is correct |
27 |
Correct |
16 ms |
13176 KB |
Output is correct |
28 |
Correct |
15 ms |
13048 KB |
Output is correct |
29 |
Correct |
15 ms |
12664 KB |
Output is correct |
30 |
Correct |
16 ms |
13176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12536 KB |
Output is correct |
2 |
Correct |
12 ms |
12664 KB |
Output is correct |
3 |
Correct |
12 ms |
12664 KB |
Output is correct |
4 |
Correct |
12 ms |
12664 KB |
Output is correct |
5 |
Correct |
12 ms |
12148 KB |
Output is correct |
6 |
Correct |
12 ms |
12024 KB |
Output is correct |
7 |
Correct |
12 ms |
12028 KB |
Output is correct |
8 |
Correct |
13 ms |
12152 KB |
Output is correct |
9 |
Correct |
12 ms |
12664 KB |
Output is correct |
10 |
Correct |
12 ms |
12664 KB |
Output is correct |
11 |
Correct |
12 ms |
12636 KB |
Output is correct |
12 |
Correct |
13 ms |
12664 KB |
Output is correct |
13 |
Correct |
12 ms |
12664 KB |
Output is correct |
14 |
Correct |
12 ms |
12664 KB |
Output is correct |
15 |
Correct |
13 ms |
12664 KB |
Output is correct |
16 |
Correct |
13 ms |
12664 KB |
Output is correct |
17 |
Correct |
13 ms |
12536 KB |
Output is correct |
18 |
Correct |
12 ms |
12152 KB |
Output is correct |
19 |
Correct |
12 ms |
12536 KB |
Output is correct |
20 |
Correct |
12 ms |
12536 KB |
Output is correct |
21 |
Correct |
12 ms |
12152 KB |
Output is correct |
22 |
Correct |
12 ms |
12152 KB |
Output is correct |
23 |
Correct |
12 ms |
12152 KB |
Output is correct |
24 |
Correct |
13 ms |
12664 KB |
Output is correct |
25 |
Correct |
12 ms |
12664 KB |
Output is correct |
26 |
Correct |
16 ms |
13048 KB |
Output is correct |
27 |
Correct |
16 ms |
13048 KB |
Output is correct |
28 |
Correct |
16 ms |
13148 KB |
Output is correct |
29 |
Correct |
16 ms |
13048 KB |
Output is correct |
30 |
Correct |
15 ms |
13048 KB |
Output is correct |
31 |
Correct |
13 ms |
12664 KB |
Output is correct |
32 |
Correct |
18 ms |
13176 KB |
Output is correct |
33 |
Correct |
12 ms |
12664 KB |
Output is correct |
34 |
Correct |
15 ms |
12536 KB |
Output is correct |
35 |
Correct |
15 ms |
13048 KB |
Output is correct |
36 |
Correct |
16 ms |
13052 KB |
Output is correct |
37 |
Correct |
16 ms |
13176 KB |
Output is correct |
38 |
Correct |
12 ms |
12664 KB |
Output is correct |
39 |
Correct |
16 ms |
13176 KB |
Output is correct |
40 |
Correct |
12 ms |
12152 KB |
Output is correct |
41 |
Correct |
16 ms |
13176 KB |
Output is correct |
42 |
Correct |
15 ms |
13176 KB |
Output is correct |
43 |
Correct |
12 ms |
12152 KB |
Output is correct |
44 |
Correct |
12 ms |
12664 KB |
Output is correct |
45 |
Correct |
16 ms |
13176 KB |
Output is correct |
46 |
Correct |
16 ms |
13048 KB |
Output is correct |
47 |
Correct |
13 ms |
12664 KB |
Output is correct |
48 |
Correct |
16 ms |
13176 KB |
Output is correct |
49 |
Correct |
15 ms |
13176 KB |
Output is correct |
50 |
Correct |
16 ms |
13176 KB |
Output is correct |
51 |
Correct |
16 ms |
13048 KB |
Output is correct |
52 |
Correct |
15 ms |
12664 KB |
Output is correct |
53 |
Correct |
15 ms |
13176 KB |
Output is correct |
54 |
Correct |
18 ms |
12540 KB |
Output is correct |
55 |
Correct |
13 ms |
12664 KB |
Output is correct |
56 |
Correct |
123 ms |
26092 KB |
Output is correct |
57 |
Correct |
165 ms |
25460 KB |
Output is correct |
58 |
Correct |
15 ms |
13048 KB |
Output is correct |
59 |
Correct |
13 ms |
12664 KB |
Output is correct |
60 |
Correct |
12 ms |
12664 KB |
Output is correct |
61 |
Correct |
164 ms |
26188 KB |
Output is correct |
62 |
Correct |
14 ms |
12540 KB |
Output is correct |
63 |
Correct |
172 ms |
32092 KB |
Output is correct |
64 |
Correct |
166 ms |
25844 KB |
Output is correct |
65 |
Correct |
16 ms |
13048 KB |
Output is correct |
66 |
Correct |
191 ms |
27896 KB |
Output is correct |
67 |
Correct |
12 ms |
12280 KB |
Output is correct |
68 |
Correct |
15 ms |
13052 KB |
Output is correct |
69 |
Correct |
125 ms |
27496 KB |
Output is correct |
70 |
Correct |
12 ms |
12228 KB |
Output is correct |
71 |
Correct |
14 ms |
12152 KB |
Output is correct |
72 |
Correct |
179 ms |
29176 KB |
Output is correct |
73 |
Correct |
13 ms |
12664 KB |
Output is correct |
74 |
Correct |
12 ms |
12024 KB |
Output is correct |
75 |
Correct |
170 ms |
30756 KB |
Output is correct |
76 |
Correct |
15 ms |
12536 KB |
Output is correct |
77 |
Correct |
15 ms |
12536 KB |
Output is correct |
78 |
Correct |
116 ms |
26104 KB |
Output is correct |
79 |
Correct |
125 ms |
27332 KB |
Output is correct |
80 |
Correct |
16 ms |
13048 KB |
Output is correct |
81 |
Correct |
16 ms |
13096 KB |
Output is correct |
82 |
Correct |
13 ms |
12664 KB |
Output is correct |
83 |
Correct |
12 ms |
12664 KB |
Output is correct |
84 |
Correct |
15 ms |
12580 KB |
Output is correct |
85 |
Correct |
186 ms |
27376 KB |
Output is correct |
86 |
Correct |
16 ms |
13048 KB |
Output is correct |
87 |
Correct |
170 ms |
27120 KB |
Output is correct |
88 |
Correct |
13 ms |
12664 KB |
Output is correct |
89 |
Correct |
202 ms |
28068 KB |
Output is correct |
90 |
Correct |
185 ms |
29156 KB |
Output is correct |
91 |
Correct |
171 ms |
29268 KB |
Output is correct |
92 |
Correct |
124 ms |
27544 KB |
Output is correct |
93 |
Correct |
15 ms |
13048 KB |
Output is correct |
94 |
Correct |
15 ms |
12664 KB |
Output is correct |
95 |
Correct |
128 ms |
29116 KB |
Output is correct |
96 |
Correct |
156 ms |
32248 KB |
Output is correct |
97 |
Correct |
15 ms |
13176 KB |
Output is correct |
98 |
Correct |
13 ms |
12664 KB |
Output is correct |
99 |
Correct |
124 ms |
28780 KB |
Output is correct |
100 |
Correct |
16 ms |
13176 KB |
Output is correct |
101 |
Correct |
185 ms |
28408 KB |
Output is correct |
102 |
Correct |
182 ms |
31900 KB |
Output is correct |
103 |
Correct |
15 ms |
13176 KB |
Output is correct |
104 |
Correct |
16 ms |
13176 KB |
Output is correct |
105 |
Correct |
15 ms |
13048 KB |
Output is correct |
106 |
Correct |
15 ms |
12664 KB |
Output is correct |
107 |
Correct |
16 ms |
13176 KB |
Output is correct |
108 |
Correct |
829 ms |
94480 KB |
Output is correct |
109 |
Correct |
1337 ms |
101940 KB |
Output is correct |
110 |
Correct |
12 ms |
12028 KB |
Output is correct |
111 |
Correct |
1198 ms |
115804 KB |
Output is correct |
112 |
Correct |
1309 ms |
109904 KB |
Output is correct |
113 |
Correct |
816 ms |
95832 KB |
Output is correct |
114 |
Correct |
1099 ms |
86156 KB |
Output is correct |
115 |
Correct |
12 ms |
12024 KB |
Output is correct |
116 |
Correct |
1185 ms |
91240 KB |
Output is correct |
117 |
Correct |
1113 ms |
96620 KB |
Output is correct |
118 |
Correct |
1116 ms |
89072 KB |
Output is correct |
119 |
Correct |
1107 ms |
96776 KB |
Output is correct |
120 |
Correct |
1210 ms |
110732 KB |
Output is correct |
121 |
Correct |
1134 ms |
96744 KB |
Output is correct |
122 |
Correct |
1244 ms |
95336 KB |
Output is correct |
123 |
Correct |
787 ms |
95708 KB |
Output is correct |
124 |
Correct |
1225 ms |
93048 KB |
Output is correct |