# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
534333 |
2022-03-08T05:07:38 Z |
79brue |
Mergers (JOI19_mergers) |
C++14 |
|
1190 ms |
155608 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct UnionFind{
int par[500002];
void init(int n){
for(int i=1; i<=n; i++) par[i] = i;
}
int find(int x){
return x==par[x] ? x : (par[x] = find(par[x]));
}
void merge(int x, int y){
x = find(x), y = find(y);
par[x] = y;
}
} dsu;
int n, k;
int arr[500002];
vector<int> link[500002];
int deg[500002], leafCnt;
int in[500002], inCnt;
int par[500002], depth[500002];
int LCADB[500002][20];
vector<int> vec[500002];
int sum[500002];
void dfs(int x, int p=-1){
in[x] = ++inCnt;
vec[arr[x]].push_back(x);
for(auto y: link[x]){
if(y == p) continue;
par[y] = LCADB[y][0] = x;
depth[y] = depth[x]+1;
dfs(y, x);
}
}
void dfs2(int x, int p=-1){
for(auto y: link[x]){
if(y == p) continue;
dfs2(y, x);
sum[in[x]] += sum[in[y]];
}
if(sum[in[x]]){
dsu.merge(x, p);
}
}
int getLCA(int x, int y){
if(depth[x] < depth[y]) swap(x, y);
for(int d=0; d<20; d++) if((depth[x]-depth[y])&(1<<d)) x=LCADB[x][d];
if(x==y) return x;
for(int d=19; d>=0; d--) if(LCADB[x][d] != LCADB[y][d]) x = LCADB[x][d], y = LCADB[y][d];
return par[x];
}
int main(){
scanf("%d %d", &n, &k);
dsu.init(n);
for(int i=1; i<n; i++){
int x, y;
scanf("%d %d", &x, &y);
link[x].push_back(y);
link[y].push_back(x);
}
for(int i=1; i<=n; i++) scanf("%d", &arr[i]);
dfs(1);
for(int d=1; d<20; d++) for(int i=1; i<=n; i++) LCADB[i][d] = LCADB[LCADB[i][d-1]][d-1];
for(int i=1; i<=k; i++){
sort(vec[i].begin(), vec[i].end(), [&](int x, int y){
return in[x] < in[y];
});
if(!vec[i].empty()) vec[i].push_back(vec[i][0]);
for(int j=0; j<(int)vec[i].size()-1; j++){
int x = vec[i][j], y = vec[i][j+1], z = getLCA(x, y);
sum[in[x]]++, sum[in[y]]++, sum[in[z]]-=2;
}
}
dfs2(1);
for(int i=1; i<=n; i++){
for(auto j: link[i]){
if(dsu.find(i) != dsu.find(j)) deg[dsu.find(i)]++;
}
}
for(int i=1; i<=n; i++) if(deg[i] == 1) leafCnt++;
printf("%d\n", (leafCnt + 1) / 2);
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:67:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | scanf("%d %d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:71:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:75:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | for(int i=1; i<=n; i++) scanf("%d", &arr[i]);
| ~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
13 ms |
23756 KB |
Output is correct |
3 |
Correct |
12 ms |
23756 KB |
Output is correct |
4 |
Correct |
13 ms |
23820 KB |
Output is correct |
5 |
Correct |
14 ms |
23808 KB |
Output is correct |
6 |
Correct |
12 ms |
23756 KB |
Output is correct |
7 |
Correct |
13 ms |
23756 KB |
Output is correct |
8 |
Correct |
17 ms |
23800 KB |
Output is correct |
9 |
Correct |
13 ms |
23800 KB |
Output is correct |
10 |
Correct |
16 ms |
23756 KB |
Output is correct |
11 |
Correct |
14 ms |
23784 KB |
Output is correct |
12 |
Correct |
12 ms |
23796 KB |
Output is correct |
13 |
Correct |
13 ms |
23760 KB |
Output is correct |
14 |
Correct |
12 ms |
23748 KB |
Output is correct |
15 |
Correct |
12 ms |
23756 KB |
Output is correct |
16 |
Correct |
12 ms |
23756 KB |
Output is correct |
17 |
Correct |
14 ms |
23788 KB |
Output is correct |
18 |
Correct |
13 ms |
23756 KB |
Output is correct |
19 |
Correct |
12 ms |
23800 KB |
Output is correct |
20 |
Correct |
13 ms |
23756 KB |
Output is correct |
21 |
Correct |
16 ms |
23756 KB |
Output is correct |
22 |
Correct |
13 ms |
23824 KB |
Output is correct |
23 |
Correct |
16 ms |
23804 KB |
Output is correct |
24 |
Correct |
12 ms |
23756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
13 ms |
23756 KB |
Output is correct |
3 |
Correct |
12 ms |
23756 KB |
Output is correct |
4 |
Correct |
13 ms |
23820 KB |
Output is correct |
5 |
Correct |
14 ms |
23808 KB |
Output is correct |
6 |
Correct |
12 ms |
23756 KB |
Output is correct |
7 |
Correct |
13 ms |
23756 KB |
Output is correct |
8 |
Correct |
17 ms |
23800 KB |
Output is correct |
9 |
Correct |
13 ms |
23800 KB |
Output is correct |
10 |
Correct |
16 ms |
23756 KB |
Output is correct |
11 |
Correct |
14 ms |
23784 KB |
Output is correct |
12 |
Correct |
12 ms |
23796 KB |
Output is correct |
13 |
Correct |
13 ms |
23760 KB |
Output is correct |
14 |
Correct |
12 ms |
23748 KB |
Output is correct |
15 |
Correct |
12 ms |
23756 KB |
Output is correct |
16 |
Correct |
12 ms |
23756 KB |
Output is correct |
17 |
Correct |
14 ms |
23788 KB |
Output is correct |
18 |
Correct |
13 ms |
23756 KB |
Output is correct |
19 |
Correct |
12 ms |
23800 KB |
Output is correct |
20 |
Correct |
13 ms |
23756 KB |
Output is correct |
21 |
Correct |
16 ms |
23756 KB |
Output is correct |
22 |
Correct |
13 ms |
23824 KB |
Output is correct |
23 |
Correct |
16 ms |
23804 KB |
Output is correct |
24 |
Correct |
12 ms |
23756 KB |
Output is correct |
25 |
Correct |
13 ms |
23852 KB |
Output is correct |
26 |
Correct |
16 ms |
24304 KB |
Output is correct |
27 |
Correct |
15 ms |
24320 KB |
Output is correct |
28 |
Correct |
15 ms |
24488 KB |
Output is correct |
29 |
Correct |
15 ms |
24268 KB |
Output is correct |
30 |
Correct |
15 ms |
24292 KB |
Output is correct |
31 |
Correct |
13 ms |
23792 KB |
Output is correct |
32 |
Correct |
16 ms |
24576 KB |
Output is correct |
33 |
Correct |
13 ms |
23804 KB |
Output is correct |
34 |
Correct |
15 ms |
24288 KB |
Output is correct |
35 |
Correct |
16 ms |
24384 KB |
Output is correct |
36 |
Correct |
15 ms |
24260 KB |
Output is correct |
37 |
Correct |
15 ms |
24332 KB |
Output is correct |
38 |
Correct |
13 ms |
23928 KB |
Output is correct |
39 |
Correct |
16 ms |
24332 KB |
Output is correct |
40 |
Correct |
20 ms |
24220 KB |
Output is correct |
41 |
Correct |
16 ms |
24184 KB |
Output is correct |
42 |
Correct |
15 ms |
24268 KB |
Output is correct |
43 |
Correct |
18 ms |
24540 KB |
Output is correct |
44 |
Correct |
12 ms |
23756 KB |
Output is correct |
45 |
Correct |
16 ms |
24396 KB |
Output is correct |
46 |
Correct |
18 ms |
24324 KB |
Output is correct |
47 |
Correct |
16 ms |
23832 KB |
Output is correct |
48 |
Correct |
15 ms |
24268 KB |
Output is correct |
49 |
Correct |
17 ms |
24324 KB |
Output is correct |
50 |
Correct |
16 ms |
24396 KB |
Output is correct |
51 |
Correct |
15 ms |
24264 KB |
Output is correct |
52 |
Correct |
15 ms |
24268 KB |
Output is correct |
53 |
Correct |
15 ms |
24268 KB |
Output is correct |
54 |
Correct |
15 ms |
24164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
13 ms |
23756 KB |
Output is correct |
3 |
Correct |
12 ms |
23756 KB |
Output is correct |
4 |
Correct |
13 ms |
23820 KB |
Output is correct |
5 |
Correct |
14 ms |
23808 KB |
Output is correct |
6 |
Correct |
12 ms |
23756 KB |
Output is correct |
7 |
Correct |
13 ms |
23756 KB |
Output is correct |
8 |
Correct |
17 ms |
23800 KB |
Output is correct |
9 |
Correct |
13 ms |
23800 KB |
Output is correct |
10 |
Correct |
16 ms |
23756 KB |
Output is correct |
11 |
Correct |
14 ms |
23784 KB |
Output is correct |
12 |
Correct |
12 ms |
23796 KB |
Output is correct |
13 |
Correct |
13 ms |
23760 KB |
Output is correct |
14 |
Correct |
12 ms |
23748 KB |
Output is correct |
15 |
Correct |
12 ms |
23756 KB |
Output is correct |
16 |
Correct |
12 ms |
23756 KB |
Output is correct |
17 |
Correct |
14 ms |
23788 KB |
Output is correct |
18 |
Correct |
13 ms |
23756 KB |
Output is correct |
19 |
Correct |
12 ms |
23800 KB |
Output is correct |
20 |
Correct |
13 ms |
23756 KB |
Output is correct |
21 |
Correct |
16 ms |
23756 KB |
Output is correct |
22 |
Correct |
13 ms |
23824 KB |
Output is correct |
23 |
Correct |
16 ms |
23804 KB |
Output is correct |
24 |
Correct |
12 ms |
23756 KB |
Output is correct |
25 |
Correct |
12 ms |
23756 KB |
Output is correct |
26 |
Correct |
115 ms |
39532 KB |
Output is correct |
27 |
Correct |
129 ms |
39168 KB |
Output is correct |
28 |
Correct |
18 ms |
24292 KB |
Output is correct |
29 |
Correct |
15 ms |
23724 KB |
Output is correct |
30 |
Correct |
16 ms |
23800 KB |
Output is correct |
31 |
Correct |
120 ms |
39188 KB |
Output is correct |
32 |
Correct |
18 ms |
24268 KB |
Output is correct |
33 |
Correct |
138 ms |
50668 KB |
Output is correct |
34 |
Correct |
178 ms |
39152 KB |
Output is correct |
35 |
Correct |
16 ms |
24268 KB |
Output is correct |
36 |
Correct |
138 ms |
40432 KB |
Output is correct |
37 |
Correct |
20 ms |
24232 KB |
Output is correct |
38 |
Correct |
14 ms |
24196 KB |
Output is correct |
39 |
Correct |
99 ms |
39420 KB |
Output is correct |
40 |
Correct |
14 ms |
24652 KB |
Output is correct |
41 |
Correct |
112 ms |
39004 KB |
Output is correct |
42 |
Correct |
178 ms |
42900 KB |
Output is correct |
43 |
Correct |
14 ms |
23756 KB |
Output is correct |
44 |
Correct |
150 ms |
51048 KB |
Output is correct |
45 |
Correct |
163 ms |
45144 KB |
Output is correct |
46 |
Correct |
17 ms |
24196 KB |
Output is correct |
47 |
Correct |
14 ms |
24268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
160 ms |
38520 KB |
Output is correct |
2 |
Correct |
142 ms |
41504 KB |
Output is correct |
3 |
Correct |
20 ms |
24292 KB |
Output is correct |
4 |
Correct |
19 ms |
24188 KB |
Output is correct |
5 |
Correct |
17 ms |
23796 KB |
Output is correct |
6 |
Correct |
15 ms |
23756 KB |
Output is correct |
7 |
Correct |
15 ms |
24200 KB |
Output is correct |
8 |
Correct |
182 ms |
41024 KB |
Output is correct |
9 |
Correct |
16 ms |
24200 KB |
Output is correct |
10 |
Correct |
137 ms |
39664 KB |
Output is correct |
11 |
Correct |
14 ms |
23788 KB |
Output is correct |
12 |
Correct |
128 ms |
39636 KB |
Output is correct |
13 |
Correct |
141 ms |
40888 KB |
Output is correct |
14 |
Correct |
136 ms |
42440 KB |
Output is correct |
15 |
Correct |
110 ms |
39456 KB |
Output is correct |
16 |
Correct |
17 ms |
24232 KB |
Output is correct |
17 |
Correct |
13 ms |
23756 KB |
Output is correct |
18 |
Correct |
114 ms |
42476 KB |
Output is correct |
19 |
Correct |
148 ms |
48708 KB |
Output is correct |
20 |
Correct |
15 ms |
24268 KB |
Output is correct |
21 |
Correct |
14 ms |
23800 KB |
Output is correct |
22 |
Correct |
114 ms |
41188 KB |
Output is correct |
23 |
Correct |
15 ms |
24268 KB |
Output is correct |
24 |
Correct |
136 ms |
39928 KB |
Output is correct |
25 |
Correct |
164 ms |
46568 KB |
Output is correct |
26 |
Correct |
17 ms |
24396 KB |
Output is correct |
27 |
Correct |
16 ms |
24428 KB |
Output is correct |
28 |
Correct |
16 ms |
24228 KB |
Output is correct |
29 |
Correct |
19 ms |
24256 KB |
Output is correct |
30 |
Correct |
16 ms |
24352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
13 ms |
23756 KB |
Output is correct |
3 |
Correct |
12 ms |
23756 KB |
Output is correct |
4 |
Correct |
13 ms |
23820 KB |
Output is correct |
5 |
Correct |
14 ms |
23808 KB |
Output is correct |
6 |
Correct |
12 ms |
23756 KB |
Output is correct |
7 |
Correct |
13 ms |
23756 KB |
Output is correct |
8 |
Correct |
17 ms |
23800 KB |
Output is correct |
9 |
Correct |
13 ms |
23800 KB |
Output is correct |
10 |
Correct |
16 ms |
23756 KB |
Output is correct |
11 |
Correct |
14 ms |
23784 KB |
Output is correct |
12 |
Correct |
12 ms |
23796 KB |
Output is correct |
13 |
Correct |
13 ms |
23760 KB |
Output is correct |
14 |
Correct |
12 ms |
23748 KB |
Output is correct |
15 |
Correct |
12 ms |
23756 KB |
Output is correct |
16 |
Correct |
12 ms |
23756 KB |
Output is correct |
17 |
Correct |
14 ms |
23788 KB |
Output is correct |
18 |
Correct |
13 ms |
23756 KB |
Output is correct |
19 |
Correct |
12 ms |
23800 KB |
Output is correct |
20 |
Correct |
13 ms |
23756 KB |
Output is correct |
21 |
Correct |
16 ms |
23756 KB |
Output is correct |
22 |
Correct |
13 ms |
23824 KB |
Output is correct |
23 |
Correct |
16 ms |
23804 KB |
Output is correct |
24 |
Correct |
12 ms |
23756 KB |
Output is correct |
25 |
Correct |
13 ms |
23852 KB |
Output is correct |
26 |
Correct |
16 ms |
24304 KB |
Output is correct |
27 |
Correct |
15 ms |
24320 KB |
Output is correct |
28 |
Correct |
15 ms |
24488 KB |
Output is correct |
29 |
Correct |
15 ms |
24268 KB |
Output is correct |
30 |
Correct |
15 ms |
24292 KB |
Output is correct |
31 |
Correct |
13 ms |
23792 KB |
Output is correct |
32 |
Correct |
16 ms |
24576 KB |
Output is correct |
33 |
Correct |
13 ms |
23804 KB |
Output is correct |
34 |
Correct |
15 ms |
24288 KB |
Output is correct |
35 |
Correct |
16 ms |
24384 KB |
Output is correct |
36 |
Correct |
15 ms |
24260 KB |
Output is correct |
37 |
Correct |
15 ms |
24332 KB |
Output is correct |
38 |
Correct |
13 ms |
23928 KB |
Output is correct |
39 |
Correct |
16 ms |
24332 KB |
Output is correct |
40 |
Correct |
20 ms |
24220 KB |
Output is correct |
41 |
Correct |
16 ms |
24184 KB |
Output is correct |
42 |
Correct |
15 ms |
24268 KB |
Output is correct |
43 |
Correct |
18 ms |
24540 KB |
Output is correct |
44 |
Correct |
12 ms |
23756 KB |
Output is correct |
45 |
Correct |
16 ms |
24396 KB |
Output is correct |
46 |
Correct |
18 ms |
24324 KB |
Output is correct |
47 |
Correct |
16 ms |
23832 KB |
Output is correct |
48 |
Correct |
15 ms |
24268 KB |
Output is correct |
49 |
Correct |
17 ms |
24324 KB |
Output is correct |
50 |
Correct |
16 ms |
24396 KB |
Output is correct |
51 |
Correct |
15 ms |
24264 KB |
Output is correct |
52 |
Correct |
15 ms |
24268 KB |
Output is correct |
53 |
Correct |
15 ms |
24268 KB |
Output is correct |
54 |
Correct |
15 ms |
24164 KB |
Output is correct |
55 |
Correct |
12 ms |
23756 KB |
Output is correct |
56 |
Correct |
115 ms |
39532 KB |
Output is correct |
57 |
Correct |
129 ms |
39168 KB |
Output is correct |
58 |
Correct |
18 ms |
24292 KB |
Output is correct |
59 |
Correct |
15 ms |
23724 KB |
Output is correct |
60 |
Correct |
16 ms |
23800 KB |
Output is correct |
61 |
Correct |
120 ms |
39188 KB |
Output is correct |
62 |
Correct |
18 ms |
24268 KB |
Output is correct |
63 |
Correct |
138 ms |
50668 KB |
Output is correct |
64 |
Correct |
178 ms |
39152 KB |
Output is correct |
65 |
Correct |
16 ms |
24268 KB |
Output is correct |
66 |
Correct |
138 ms |
40432 KB |
Output is correct |
67 |
Correct |
20 ms |
24232 KB |
Output is correct |
68 |
Correct |
14 ms |
24196 KB |
Output is correct |
69 |
Correct |
99 ms |
39420 KB |
Output is correct |
70 |
Correct |
14 ms |
24652 KB |
Output is correct |
71 |
Correct |
112 ms |
39004 KB |
Output is correct |
72 |
Correct |
178 ms |
42900 KB |
Output is correct |
73 |
Correct |
14 ms |
23756 KB |
Output is correct |
74 |
Correct |
150 ms |
51048 KB |
Output is correct |
75 |
Correct |
163 ms |
45144 KB |
Output is correct |
76 |
Correct |
17 ms |
24196 KB |
Output is correct |
77 |
Correct |
14 ms |
24268 KB |
Output is correct |
78 |
Correct |
160 ms |
38520 KB |
Output is correct |
79 |
Correct |
142 ms |
41504 KB |
Output is correct |
80 |
Correct |
20 ms |
24292 KB |
Output is correct |
81 |
Correct |
19 ms |
24188 KB |
Output is correct |
82 |
Correct |
17 ms |
23796 KB |
Output is correct |
83 |
Correct |
15 ms |
23756 KB |
Output is correct |
84 |
Correct |
15 ms |
24200 KB |
Output is correct |
85 |
Correct |
182 ms |
41024 KB |
Output is correct |
86 |
Correct |
16 ms |
24200 KB |
Output is correct |
87 |
Correct |
137 ms |
39664 KB |
Output is correct |
88 |
Correct |
14 ms |
23788 KB |
Output is correct |
89 |
Correct |
128 ms |
39636 KB |
Output is correct |
90 |
Correct |
141 ms |
40888 KB |
Output is correct |
91 |
Correct |
136 ms |
42440 KB |
Output is correct |
92 |
Correct |
110 ms |
39456 KB |
Output is correct |
93 |
Correct |
17 ms |
24232 KB |
Output is correct |
94 |
Correct |
13 ms |
23756 KB |
Output is correct |
95 |
Correct |
114 ms |
42476 KB |
Output is correct |
96 |
Correct |
148 ms |
48708 KB |
Output is correct |
97 |
Correct |
15 ms |
24268 KB |
Output is correct |
98 |
Correct |
14 ms |
23800 KB |
Output is correct |
99 |
Correct |
114 ms |
41188 KB |
Output is correct |
100 |
Correct |
15 ms |
24268 KB |
Output is correct |
101 |
Correct |
136 ms |
39928 KB |
Output is correct |
102 |
Correct |
164 ms |
46568 KB |
Output is correct |
103 |
Correct |
17 ms |
24396 KB |
Output is correct |
104 |
Correct |
16 ms |
24428 KB |
Output is correct |
105 |
Correct |
16 ms |
24228 KB |
Output is correct |
106 |
Correct |
19 ms |
24256 KB |
Output is correct |
107 |
Correct |
16 ms |
24352 KB |
Output is correct |
108 |
Correct |
860 ms |
111556 KB |
Output is correct |
109 |
Correct |
1039 ms |
129680 KB |
Output is correct |
110 |
Correct |
1088 ms |
147572 KB |
Output is correct |
111 |
Correct |
1190 ms |
155608 KB |
Output is correct |
112 |
Correct |
1139 ms |
139964 KB |
Output is correct |
113 |
Correct |
787 ms |
118300 KB |
Output is correct |
114 |
Correct |
964 ms |
100532 KB |
Output is correct |
115 |
Correct |
876 ms |
100484 KB |
Output is correct |
116 |
Correct |
1084 ms |
107224 KB |
Output is correct |
117 |
Correct |
932 ms |
118580 KB |
Output is correct |
118 |
Correct |
875 ms |
102332 KB |
Output is correct |
119 |
Correct |
948 ms |
118380 KB |
Output is correct |
120 |
Correct |
1016 ms |
140936 KB |
Output is correct |
121 |
Correct |
965 ms |
118428 KB |
Output is correct |
122 |
Correct |
1103 ms |
112452 KB |
Output is correct |
123 |
Correct |
806 ms |
119876 KB |
Output is correct |
124 |
Correct |
1034 ms |
116756 KB |
Output is correct |