# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
280106 |
2020-08-22T13:36:11 Z |
3zp |
Mergers (JOI19_mergers) |
C++14 |
|
982 ms |
71404 KB |
#include<bits/stdc++.h>
using namespace std;
const int mN = 500009;
vector<int> V[mN];
int L[mN],R[mN], A[mN], St[mN], En[mN], S[mN], Good[mN], Deg[mN], Sz[mN], Min[mN], Max[mN];
int c = 1;
int good_cnt = 0;
void dfs1(int x, int p){
L[x] = c;
R[x] = c;
A[c] = x;
c++;
for(int y : V[x]){
if(y == p) continue;
dfs1(y, x);
R[x] = R[y];
}
}
void dfs2(int x, int p){
if(Good[x]) Sz[x] = 1;
for(int y : V[x]){
if(y == p) continue;
dfs2(y, x);
Sz[x] += Sz[y];
}
if(x > 1 && (Good[x] || (Sz[x] > 0 && Sz[x] < good_cnt))){
Deg[x]++;
Deg[p]++;
}
}
void dfs3(int x, int p){
Min[x] = St[S[x]];
Max[x] = En[S[x]];
for(int y : V[x]){
if(y == p) continue;
dfs3(y, x);
Min[x] = min(Min[x], Min[y]);
Max[x] = max(Max[x], Max[y]);
}
if(x > 1 && Min[x] == L[x] && Max[x] == R[x]){
Good[x] = 1;
good_cnt++;
}
}
main(){
int n, k;
scanf("%d %d", &n, &k);
for(int i = 1; i < n; i++){
int a, b;
scanf("%d %d", &a, &b);
V[a].push_back(b);
V[b].push_back(a);
}
for(int i = 1; i <= n; i++)
scanf("%d", &S[i]);
dfs1(1, 0);
for(int i = 1; i <= n; i++){
int x = A[i];
if(!St[S[x]]) St[S[x]] = i;
En[S[x]] = i;
}
dfs3(1, 0);
dfs2(1, 0);
int leaf_cnt = 0;
for(int i = 1; i <= n; i++)
if(Deg[i] == 1) leaf_cnt++;
printf("%d\n", (leaf_cnt + 1) / 2 );
}
Compilation message
mergers.cpp:47:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
47 | main(){
| ^
mergers.cpp: In function 'int main()':
mergers.cpp:50:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
50 | scanf("%d %d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:53:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
53 | scanf("%d %d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:58:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
58 | scanf("%d", &S[i]);
| ~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
7 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12160 KB |
Output is correct |
5 |
Correct |
8 ms |
12136 KB |
Output is correct |
6 |
Correct |
9 ms |
12160 KB |
Output is correct |
7 |
Correct |
8 ms |
12160 KB |
Output is correct |
8 |
Correct |
8 ms |
12160 KB |
Output is correct |
9 |
Correct |
8 ms |
12192 KB |
Output is correct |
10 |
Correct |
9 ms |
12160 KB |
Output is correct |
11 |
Correct |
8 ms |
12160 KB |
Output is correct |
12 |
Correct |
8 ms |
12160 KB |
Output is correct |
13 |
Correct |
8 ms |
12160 KB |
Output is correct |
14 |
Correct |
8 ms |
12160 KB |
Output is correct |
15 |
Correct |
8 ms |
12160 KB |
Output is correct |
16 |
Correct |
8 ms |
12160 KB |
Output is correct |
17 |
Correct |
8 ms |
12160 KB |
Output is correct |
18 |
Correct |
8 ms |
12160 KB |
Output is correct |
19 |
Correct |
8 ms |
12160 KB |
Output is correct |
20 |
Correct |
8 ms |
12160 KB |
Output is correct |
21 |
Correct |
7 ms |
12160 KB |
Output is correct |
22 |
Correct |
8 ms |
12160 KB |
Output is correct |
23 |
Correct |
8 ms |
12160 KB |
Output is correct |
24 |
Correct |
8 ms |
12160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
7 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12160 KB |
Output is correct |
5 |
Correct |
8 ms |
12136 KB |
Output is correct |
6 |
Correct |
9 ms |
12160 KB |
Output is correct |
7 |
Correct |
8 ms |
12160 KB |
Output is correct |
8 |
Correct |
8 ms |
12160 KB |
Output is correct |
9 |
Correct |
8 ms |
12192 KB |
Output is correct |
10 |
Correct |
9 ms |
12160 KB |
Output is correct |
11 |
Correct |
8 ms |
12160 KB |
Output is correct |
12 |
Correct |
8 ms |
12160 KB |
Output is correct |
13 |
Correct |
8 ms |
12160 KB |
Output is correct |
14 |
Correct |
8 ms |
12160 KB |
Output is correct |
15 |
Correct |
8 ms |
12160 KB |
Output is correct |
16 |
Correct |
8 ms |
12160 KB |
Output is correct |
17 |
Correct |
8 ms |
12160 KB |
Output is correct |
18 |
Correct |
8 ms |
12160 KB |
Output is correct |
19 |
Correct |
8 ms |
12160 KB |
Output is correct |
20 |
Correct |
8 ms |
12160 KB |
Output is correct |
21 |
Correct |
7 ms |
12160 KB |
Output is correct |
22 |
Correct |
8 ms |
12160 KB |
Output is correct |
23 |
Correct |
8 ms |
12160 KB |
Output is correct |
24 |
Correct |
8 ms |
12160 KB |
Output is correct |
25 |
Correct |
8 ms |
12160 KB |
Output is correct |
26 |
Correct |
9 ms |
12416 KB |
Output is correct |
27 |
Correct |
10 ms |
12416 KB |
Output is correct |
28 |
Correct |
10 ms |
12544 KB |
Output is correct |
29 |
Correct |
10 ms |
12416 KB |
Output is correct |
30 |
Correct |
11 ms |
12416 KB |
Output is correct |
31 |
Correct |
8 ms |
12160 KB |
Output is correct |
32 |
Correct |
10 ms |
12544 KB |
Output is correct |
33 |
Correct |
8 ms |
12160 KB |
Output is correct |
34 |
Correct |
9 ms |
12288 KB |
Output is correct |
35 |
Correct |
10 ms |
12416 KB |
Output is correct |
36 |
Correct |
10 ms |
12416 KB |
Output is correct |
37 |
Correct |
10 ms |
12416 KB |
Output is correct |
38 |
Correct |
8 ms |
12160 KB |
Output is correct |
39 |
Correct |
10 ms |
12416 KB |
Output is correct |
40 |
Correct |
10 ms |
12288 KB |
Output is correct |
41 |
Correct |
10 ms |
12416 KB |
Output is correct |
42 |
Correct |
9 ms |
12416 KB |
Output is correct |
43 |
Correct |
9 ms |
12544 KB |
Output is correct |
44 |
Correct |
8 ms |
12180 KB |
Output is correct |
45 |
Correct |
11 ms |
12416 KB |
Output is correct |
46 |
Correct |
10 ms |
12416 KB |
Output is correct |
47 |
Correct |
7 ms |
12160 KB |
Output is correct |
48 |
Correct |
10 ms |
12416 KB |
Output is correct |
49 |
Correct |
9 ms |
12416 KB |
Output is correct |
50 |
Correct |
11 ms |
12416 KB |
Output is correct |
51 |
Correct |
9 ms |
12416 KB |
Output is correct |
52 |
Correct |
10 ms |
12416 KB |
Output is correct |
53 |
Correct |
9 ms |
12416 KB |
Output is correct |
54 |
Correct |
9 ms |
12288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
7 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12160 KB |
Output is correct |
5 |
Correct |
8 ms |
12136 KB |
Output is correct |
6 |
Correct |
9 ms |
12160 KB |
Output is correct |
7 |
Correct |
8 ms |
12160 KB |
Output is correct |
8 |
Correct |
8 ms |
12160 KB |
Output is correct |
9 |
Correct |
8 ms |
12192 KB |
Output is correct |
10 |
Correct |
9 ms |
12160 KB |
Output is correct |
11 |
Correct |
8 ms |
12160 KB |
Output is correct |
12 |
Correct |
8 ms |
12160 KB |
Output is correct |
13 |
Correct |
8 ms |
12160 KB |
Output is correct |
14 |
Correct |
8 ms |
12160 KB |
Output is correct |
15 |
Correct |
8 ms |
12160 KB |
Output is correct |
16 |
Correct |
8 ms |
12160 KB |
Output is correct |
17 |
Correct |
8 ms |
12160 KB |
Output is correct |
18 |
Correct |
8 ms |
12160 KB |
Output is correct |
19 |
Correct |
8 ms |
12160 KB |
Output is correct |
20 |
Correct |
8 ms |
12160 KB |
Output is correct |
21 |
Correct |
7 ms |
12160 KB |
Output is correct |
22 |
Correct |
8 ms |
12160 KB |
Output is correct |
23 |
Correct |
8 ms |
12160 KB |
Output is correct |
24 |
Correct |
8 ms |
12160 KB |
Output is correct |
25 |
Correct |
8 ms |
12160 KB |
Output is correct |
26 |
Correct |
78 ms |
19484 KB |
Output is correct |
27 |
Correct |
102 ms |
19576 KB |
Output is correct |
28 |
Correct |
12 ms |
12416 KB |
Output is correct |
29 |
Correct |
9 ms |
12160 KB |
Output is correct |
30 |
Correct |
8 ms |
12160 KB |
Output is correct |
31 |
Correct |
126 ms |
20216 KB |
Output is correct |
32 |
Correct |
10 ms |
12288 KB |
Output is correct |
33 |
Correct |
148 ms |
25212 KB |
Output is correct |
34 |
Correct |
124 ms |
19576 KB |
Output is correct |
35 |
Correct |
10 ms |
12416 KB |
Output is correct |
36 |
Correct |
120 ms |
20600 KB |
Output is correct |
37 |
Correct |
10 ms |
12288 KB |
Output is correct |
38 |
Correct |
11 ms |
12416 KB |
Output is correct |
39 |
Correct |
73 ms |
19408 KB |
Output is correct |
40 |
Correct |
10 ms |
12544 KB |
Output is correct |
41 |
Correct |
108 ms |
19488 KB |
Output is correct |
42 |
Correct |
143 ms |
21368 KB |
Output is correct |
43 |
Correct |
9 ms |
12160 KB |
Output is correct |
44 |
Correct |
146 ms |
25336 KB |
Output is correct |
45 |
Correct |
128 ms |
22460 KB |
Output is correct |
46 |
Correct |
10 ms |
12416 KB |
Output is correct |
47 |
Correct |
11 ms |
12288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
19440 KB |
Output is correct |
2 |
Correct |
79 ms |
21744 KB |
Output is correct |
3 |
Correct |
10 ms |
12416 KB |
Output is correct |
4 |
Correct |
10 ms |
12416 KB |
Output is correct |
5 |
Correct |
8 ms |
12160 KB |
Output is correct |
6 |
Correct |
8 ms |
12160 KB |
Output is correct |
7 |
Correct |
9 ms |
12288 KB |
Output is correct |
8 |
Correct |
124 ms |
21028 KB |
Output is correct |
9 |
Correct |
10 ms |
12416 KB |
Output is correct |
10 |
Correct |
114 ms |
20412 KB |
Output is correct |
11 |
Correct |
8 ms |
12160 KB |
Output is correct |
12 |
Correct |
128 ms |
20412 KB |
Output is correct |
13 |
Correct |
127 ms |
20980 KB |
Output is correct |
14 |
Correct |
119 ms |
21368 KB |
Output is correct |
15 |
Correct |
85 ms |
19372 KB |
Output is correct |
16 |
Correct |
10 ms |
12416 KB |
Output is correct |
17 |
Correct |
8 ms |
12160 KB |
Output is correct |
18 |
Correct |
85 ms |
21744 KB |
Output is correct |
19 |
Correct |
139 ms |
24512 KB |
Output is correct |
20 |
Correct |
10 ms |
12416 KB |
Output is correct |
21 |
Correct |
8 ms |
12160 KB |
Output is correct |
22 |
Correct |
79 ms |
21360 KB |
Output is correct |
23 |
Correct |
10 ms |
12416 KB |
Output is correct |
24 |
Correct |
123 ms |
20472 KB |
Output is correct |
25 |
Correct |
125 ms |
23416 KB |
Output is correct |
26 |
Correct |
10 ms |
12416 KB |
Output is correct |
27 |
Correct |
10 ms |
12416 KB |
Output is correct |
28 |
Correct |
9 ms |
12416 KB |
Output is correct |
29 |
Correct |
9 ms |
12416 KB |
Output is correct |
30 |
Correct |
10 ms |
12448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12160 KB |
Output is correct |
3 |
Correct |
7 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12160 KB |
Output is correct |
5 |
Correct |
8 ms |
12136 KB |
Output is correct |
6 |
Correct |
9 ms |
12160 KB |
Output is correct |
7 |
Correct |
8 ms |
12160 KB |
Output is correct |
8 |
Correct |
8 ms |
12160 KB |
Output is correct |
9 |
Correct |
8 ms |
12192 KB |
Output is correct |
10 |
Correct |
9 ms |
12160 KB |
Output is correct |
11 |
Correct |
8 ms |
12160 KB |
Output is correct |
12 |
Correct |
8 ms |
12160 KB |
Output is correct |
13 |
Correct |
8 ms |
12160 KB |
Output is correct |
14 |
Correct |
8 ms |
12160 KB |
Output is correct |
15 |
Correct |
8 ms |
12160 KB |
Output is correct |
16 |
Correct |
8 ms |
12160 KB |
Output is correct |
17 |
Correct |
8 ms |
12160 KB |
Output is correct |
18 |
Correct |
8 ms |
12160 KB |
Output is correct |
19 |
Correct |
8 ms |
12160 KB |
Output is correct |
20 |
Correct |
8 ms |
12160 KB |
Output is correct |
21 |
Correct |
7 ms |
12160 KB |
Output is correct |
22 |
Correct |
8 ms |
12160 KB |
Output is correct |
23 |
Correct |
8 ms |
12160 KB |
Output is correct |
24 |
Correct |
8 ms |
12160 KB |
Output is correct |
25 |
Correct |
8 ms |
12160 KB |
Output is correct |
26 |
Correct |
9 ms |
12416 KB |
Output is correct |
27 |
Correct |
10 ms |
12416 KB |
Output is correct |
28 |
Correct |
10 ms |
12544 KB |
Output is correct |
29 |
Correct |
10 ms |
12416 KB |
Output is correct |
30 |
Correct |
11 ms |
12416 KB |
Output is correct |
31 |
Correct |
8 ms |
12160 KB |
Output is correct |
32 |
Correct |
10 ms |
12544 KB |
Output is correct |
33 |
Correct |
8 ms |
12160 KB |
Output is correct |
34 |
Correct |
9 ms |
12288 KB |
Output is correct |
35 |
Correct |
10 ms |
12416 KB |
Output is correct |
36 |
Correct |
10 ms |
12416 KB |
Output is correct |
37 |
Correct |
10 ms |
12416 KB |
Output is correct |
38 |
Correct |
8 ms |
12160 KB |
Output is correct |
39 |
Correct |
10 ms |
12416 KB |
Output is correct |
40 |
Correct |
10 ms |
12288 KB |
Output is correct |
41 |
Correct |
10 ms |
12416 KB |
Output is correct |
42 |
Correct |
9 ms |
12416 KB |
Output is correct |
43 |
Correct |
9 ms |
12544 KB |
Output is correct |
44 |
Correct |
8 ms |
12180 KB |
Output is correct |
45 |
Correct |
11 ms |
12416 KB |
Output is correct |
46 |
Correct |
10 ms |
12416 KB |
Output is correct |
47 |
Correct |
7 ms |
12160 KB |
Output is correct |
48 |
Correct |
10 ms |
12416 KB |
Output is correct |
49 |
Correct |
9 ms |
12416 KB |
Output is correct |
50 |
Correct |
11 ms |
12416 KB |
Output is correct |
51 |
Correct |
9 ms |
12416 KB |
Output is correct |
52 |
Correct |
10 ms |
12416 KB |
Output is correct |
53 |
Correct |
9 ms |
12416 KB |
Output is correct |
54 |
Correct |
9 ms |
12288 KB |
Output is correct |
55 |
Correct |
8 ms |
12160 KB |
Output is correct |
56 |
Correct |
78 ms |
19484 KB |
Output is correct |
57 |
Correct |
102 ms |
19576 KB |
Output is correct |
58 |
Correct |
12 ms |
12416 KB |
Output is correct |
59 |
Correct |
9 ms |
12160 KB |
Output is correct |
60 |
Correct |
8 ms |
12160 KB |
Output is correct |
61 |
Correct |
126 ms |
20216 KB |
Output is correct |
62 |
Correct |
10 ms |
12288 KB |
Output is correct |
63 |
Correct |
148 ms |
25212 KB |
Output is correct |
64 |
Correct |
124 ms |
19576 KB |
Output is correct |
65 |
Correct |
10 ms |
12416 KB |
Output is correct |
66 |
Correct |
120 ms |
20600 KB |
Output is correct |
67 |
Correct |
10 ms |
12288 KB |
Output is correct |
68 |
Correct |
11 ms |
12416 KB |
Output is correct |
69 |
Correct |
73 ms |
19408 KB |
Output is correct |
70 |
Correct |
10 ms |
12544 KB |
Output is correct |
71 |
Correct |
108 ms |
19488 KB |
Output is correct |
72 |
Correct |
143 ms |
21368 KB |
Output is correct |
73 |
Correct |
9 ms |
12160 KB |
Output is correct |
74 |
Correct |
146 ms |
25336 KB |
Output is correct |
75 |
Correct |
128 ms |
22460 KB |
Output is correct |
76 |
Correct |
10 ms |
12416 KB |
Output is correct |
77 |
Correct |
11 ms |
12288 KB |
Output is correct |
78 |
Correct |
83 ms |
19440 KB |
Output is correct |
79 |
Correct |
79 ms |
21744 KB |
Output is correct |
80 |
Correct |
10 ms |
12416 KB |
Output is correct |
81 |
Correct |
10 ms |
12416 KB |
Output is correct |
82 |
Correct |
8 ms |
12160 KB |
Output is correct |
83 |
Correct |
8 ms |
12160 KB |
Output is correct |
84 |
Correct |
9 ms |
12288 KB |
Output is correct |
85 |
Correct |
124 ms |
21028 KB |
Output is correct |
86 |
Correct |
10 ms |
12416 KB |
Output is correct |
87 |
Correct |
114 ms |
20412 KB |
Output is correct |
88 |
Correct |
8 ms |
12160 KB |
Output is correct |
89 |
Correct |
128 ms |
20412 KB |
Output is correct |
90 |
Correct |
127 ms |
20980 KB |
Output is correct |
91 |
Correct |
119 ms |
21368 KB |
Output is correct |
92 |
Correct |
85 ms |
19372 KB |
Output is correct |
93 |
Correct |
10 ms |
12416 KB |
Output is correct |
94 |
Correct |
8 ms |
12160 KB |
Output is correct |
95 |
Correct |
85 ms |
21744 KB |
Output is correct |
96 |
Correct |
139 ms |
24512 KB |
Output is correct |
97 |
Correct |
10 ms |
12416 KB |
Output is correct |
98 |
Correct |
8 ms |
12160 KB |
Output is correct |
99 |
Correct |
79 ms |
21360 KB |
Output is correct |
100 |
Correct |
10 ms |
12416 KB |
Output is correct |
101 |
Correct |
123 ms |
20472 KB |
Output is correct |
102 |
Correct |
125 ms |
23416 KB |
Output is correct |
103 |
Correct |
10 ms |
12416 KB |
Output is correct |
104 |
Correct |
10 ms |
12416 KB |
Output is correct |
105 |
Correct |
9 ms |
12416 KB |
Output is correct |
106 |
Correct |
9 ms |
12416 KB |
Output is correct |
107 |
Correct |
10 ms |
12448 KB |
Output is correct |
108 |
Correct |
499 ms |
52484 KB |
Output is correct |
109 |
Correct |
904 ms |
59448 KB |
Output is correct |
110 |
Correct |
902 ms |
68472 KB |
Output is correct |
111 |
Correct |
982 ms |
71404 KB |
Output is correct |
112 |
Correct |
895 ms |
63608 KB |
Output is correct |
113 |
Correct |
524 ms |
54244 KB |
Output is correct |
114 |
Correct |
745 ms |
45328 KB |
Output is correct |
115 |
Correct |
750 ms |
45344 KB |
Output is correct |
116 |
Correct |
790 ms |
49656 KB |
Output is correct |
117 |
Correct |
822 ms |
53084 KB |
Output is correct |
118 |
Correct |
764 ms |
48232 KB |
Output is correct |
119 |
Correct |
850 ms |
53520 KB |
Output is correct |
120 |
Correct |
897 ms |
64376 KB |
Output is correct |
121 |
Correct |
946 ms |
53344 KB |
Output is correct |
122 |
Correct |
833 ms |
51860 KB |
Output is correct |
123 |
Correct |
524 ms |
54884 KB |
Output is correct |
124 |
Correct |
936 ms |
52792 KB |
Output is correct |