# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
919126 |
2024-01-31T10:49:16 Z |
ttamx |
Mergers (JOI19_mergers) |
C++14 |
|
884 ms |
113228 KB |
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int n,k;
pair<int,int> edge[N];
vector<int> adj[N],comp[N];
int sz[N],hv[N],lv[N],hd[N],pa[N];
int a[N],deg[N];
int fa[N];
int fp(int u){
return fa[u]=fa[u]==u?u:fp(fa[u]);
}
void uni(int u,int v){
fa[fp(u)]=fp(v);
}
void dfs(int u,int p=0){
pa[u]=p;
sz[u]=1;
for(auto v:adj[u])if(v!=p){
lv[v]=lv[u]+1;
dfs(v,u);
sz[u]+=sz[v];
if(sz[v]>sz[hv[u]])hv[u]=v;
}
}
void hld(int u,int p=0){
if(!hd[u])hd[u]=u;
if(hv[u])hd[hv[u]]=hd[u],hld(hv[u],u);
for(auto v:adj[u])if(v!=p&&v!=hv[u])hld(v,u);
}
int lca(int u,int v){
while(hd[u]!=hd[v]){
if(lv[hd[u]]<lv[hd[v]])swap(u,v);
u=pa[hd[u]];
}
return lv[u]<lv[v]?u:v;
}
void dfs2(int u,int p=0){
for(auto v:adj[u])if(v!=p){
dfs2(v,u);
a[u]+=a[v];
if(a[v])uni(u,v);
}
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> k;
for(int i=1;i<n;i++){
auto &[u,v]=edge[i];
cin >> u >> v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
for(int i=1;i<=n;i++){
int x;
cin >> x;
comp[x].emplace_back(i);
}
dfs(1);
hld(1);
for(int i=1;i<=k;i++){
for(int j=1;j<comp[i].size();j++){
int u=comp[i][j-1],v=comp[i][j];
a[u]++,a[v]++,a[lca(u,v)]-=2;
}
}
iota(fa+1,fa+n+1,1);
dfs2(1);
for(int i=1;i<n;i++){
auto [u,v]=edge[i];
u=fp(u),v=fp(v);
if(u!=v)deg[u]++,deg[v]++;
}
int ans=0;
for(int i=1;i<=n;i++)ans+=deg[i]==1;
cout << (ans+1)/2;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:59:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
59 | auto &[u,v]=edge[i];
| ^
mergers.cpp:72:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for(int j=1;j<comp[i].size();j++){
| ~^~~~~~~~~~~~~~~
mergers.cpp:80:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
80 | auto [u,v]=edge[i];
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
33112 KB |
Output is correct |
2 |
Correct |
8 ms |
33116 KB |
Output is correct |
3 |
Correct |
9 ms |
33500 KB |
Output is correct |
4 |
Correct |
9 ms |
33112 KB |
Output is correct |
5 |
Correct |
8 ms |
31064 KB |
Output is correct |
6 |
Correct |
8 ms |
31068 KB |
Output is correct |
7 |
Correct |
7 ms |
29020 KB |
Output is correct |
8 |
Correct |
8 ms |
31064 KB |
Output is correct |
9 |
Correct |
9 ms |
33112 KB |
Output is correct |
10 |
Correct |
8 ms |
33116 KB |
Output is correct |
11 |
Correct |
8 ms |
33116 KB |
Output is correct |
12 |
Correct |
9 ms |
33112 KB |
Output is correct |
13 |
Correct |
8 ms |
33112 KB |
Output is correct |
14 |
Correct |
9 ms |
33116 KB |
Output is correct |
15 |
Correct |
9 ms |
33112 KB |
Output is correct |
16 |
Correct |
8 ms |
31068 KB |
Output is correct |
17 |
Correct |
8 ms |
33112 KB |
Output is correct |
18 |
Correct |
8 ms |
31068 KB |
Output is correct |
19 |
Correct |
9 ms |
31064 KB |
Output is correct |
20 |
Correct |
8 ms |
31068 KB |
Output is correct |
21 |
Correct |
7 ms |
31064 KB |
Output is correct |
22 |
Correct |
8 ms |
31064 KB |
Output is correct |
23 |
Correct |
8 ms |
31064 KB |
Output is correct |
24 |
Correct |
8 ms |
33116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
33112 KB |
Output is correct |
2 |
Correct |
8 ms |
33116 KB |
Output is correct |
3 |
Correct |
9 ms |
33500 KB |
Output is correct |
4 |
Correct |
9 ms |
33112 KB |
Output is correct |
5 |
Correct |
8 ms |
31064 KB |
Output is correct |
6 |
Correct |
8 ms |
31068 KB |
Output is correct |
7 |
Correct |
7 ms |
29020 KB |
Output is correct |
8 |
Correct |
8 ms |
31064 KB |
Output is correct |
9 |
Correct |
9 ms |
33112 KB |
Output is correct |
10 |
Correct |
8 ms |
33116 KB |
Output is correct |
11 |
Correct |
8 ms |
33116 KB |
Output is correct |
12 |
Correct |
9 ms |
33112 KB |
Output is correct |
13 |
Correct |
8 ms |
33112 KB |
Output is correct |
14 |
Correct |
9 ms |
33116 KB |
Output is correct |
15 |
Correct |
9 ms |
33112 KB |
Output is correct |
16 |
Correct |
8 ms |
31068 KB |
Output is correct |
17 |
Correct |
8 ms |
33112 KB |
Output is correct |
18 |
Correct |
8 ms |
31068 KB |
Output is correct |
19 |
Correct |
9 ms |
31064 KB |
Output is correct |
20 |
Correct |
8 ms |
31068 KB |
Output is correct |
21 |
Correct |
7 ms |
31064 KB |
Output is correct |
22 |
Correct |
8 ms |
31064 KB |
Output is correct |
23 |
Correct |
8 ms |
31064 KB |
Output is correct |
24 |
Correct |
8 ms |
33116 KB |
Output is correct |
25 |
Correct |
9 ms |
33112 KB |
Output is correct |
26 |
Correct |
10 ms |
33372 KB |
Output is correct |
27 |
Correct |
9 ms |
33368 KB |
Output is correct |
28 |
Correct |
10 ms |
33628 KB |
Output is correct |
29 |
Correct |
10 ms |
33556 KB |
Output is correct |
30 |
Correct |
10 ms |
33368 KB |
Output is correct |
31 |
Correct |
8 ms |
33112 KB |
Output is correct |
32 |
Correct |
10 ms |
33624 KB |
Output is correct |
33 |
Correct |
8 ms |
33112 KB |
Output is correct |
34 |
Correct |
9 ms |
31320 KB |
Output is correct |
35 |
Correct |
9 ms |
31320 KB |
Output is correct |
36 |
Correct |
9 ms |
33368 KB |
Output is correct |
37 |
Correct |
11 ms |
33368 KB |
Output is correct |
38 |
Correct |
8 ms |
33112 KB |
Output is correct |
39 |
Correct |
10 ms |
33368 KB |
Output is correct |
40 |
Correct |
10 ms |
31252 KB |
Output is correct |
41 |
Correct |
10 ms |
33368 KB |
Output is correct |
42 |
Correct |
9 ms |
33372 KB |
Output is correct |
43 |
Correct |
9 ms |
31576 KB |
Output is correct |
44 |
Correct |
8 ms |
33116 KB |
Output is correct |
45 |
Correct |
11 ms |
33624 KB |
Output is correct |
46 |
Correct |
10 ms |
33372 KB |
Output is correct |
47 |
Correct |
8 ms |
33112 KB |
Output is correct |
48 |
Correct |
10 ms |
33372 KB |
Output is correct |
49 |
Correct |
10 ms |
31320 KB |
Output is correct |
50 |
Correct |
9 ms |
31576 KB |
Output is correct |
51 |
Correct |
10 ms |
33368 KB |
Output is correct |
52 |
Correct |
9 ms |
31576 KB |
Output is correct |
53 |
Correct |
10 ms |
33368 KB |
Output is correct |
54 |
Correct |
10 ms |
31320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
33112 KB |
Output is correct |
2 |
Correct |
8 ms |
33116 KB |
Output is correct |
3 |
Correct |
9 ms |
33500 KB |
Output is correct |
4 |
Correct |
9 ms |
33112 KB |
Output is correct |
5 |
Correct |
8 ms |
31064 KB |
Output is correct |
6 |
Correct |
8 ms |
31068 KB |
Output is correct |
7 |
Correct |
7 ms |
29020 KB |
Output is correct |
8 |
Correct |
8 ms |
31064 KB |
Output is correct |
9 |
Correct |
9 ms |
33112 KB |
Output is correct |
10 |
Correct |
8 ms |
33116 KB |
Output is correct |
11 |
Correct |
8 ms |
33116 KB |
Output is correct |
12 |
Correct |
9 ms |
33112 KB |
Output is correct |
13 |
Correct |
8 ms |
33112 KB |
Output is correct |
14 |
Correct |
9 ms |
33116 KB |
Output is correct |
15 |
Correct |
9 ms |
33112 KB |
Output is correct |
16 |
Correct |
8 ms |
31068 KB |
Output is correct |
17 |
Correct |
8 ms |
33112 KB |
Output is correct |
18 |
Correct |
8 ms |
31068 KB |
Output is correct |
19 |
Correct |
9 ms |
31064 KB |
Output is correct |
20 |
Correct |
8 ms |
31068 KB |
Output is correct |
21 |
Correct |
7 ms |
31064 KB |
Output is correct |
22 |
Correct |
8 ms |
31064 KB |
Output is correct |
23 |
Correct |
8 ms |
31064 KB |
Output is correct |
24 |
Correct |
8 ms |
33116 KB |
Output is correct |
25 |
Correct |
9 ms |
33112 KB |
Output is correct |
26 |
Correct |
44 ms |
41364 KB |
Output is correct |
27 |
Correct |
66 ms |
40528 KB |
Output is correct |
28 |
Correct |
9 ms |
33368 KB |
Output is correct |
29 |
Correct |
8 ms |
33116 KB |
Output is correct |
30 |
Correct |
8 ms |
33112 KB |
Output is correct |
31 |
Correct |
54 ms |
40276 KB |
Output is correct |
32 |
Correct |
10 ms |
31516 KB |
Output is correct |
33 |
Correct |
61 ms |
50516 KB |
Output is correct |
34 |
Correct |
61 ms |
41808 KB |
Output is correct |
35 |
Correct |
9 ms |
33368 KB |
Output is correct |
36 |
Correct |
69 ms |
42740 KB |
Output is correct |
37 |
Correct |
9 ms |
31320 KB |
Output is correct |
38 |
Correct |
12 ms |
33368 KB |
Output is correct |
39 |
Correct |
46 ms |
42812 KB |
Output is correct |
40 |
Correct |
9 ms |
31576 KB |
Output is correct |
41 |
Correct |
65 ms |
41676 KB |
Output is correct |
42 |
Correct |
75 ms |
44728 KB |
Output is correct |
43 |
Correct |
8 ms |
33112 KB |
Output is correct |
44 |
Correct |
79 ms |
50660 KB |
Output is correct |
45 |
Correct |
62 ms |
46160 KB |
Output is correct |
46 |
Correct |
9 ms |
31320 KB |
Output is correct |
47 |
Correct |
9 ms |
31320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
41416 KB |
Output is correct |
2 |
Correct |
55 ms |
40904 KB |
Output is correct |
3 |
Correct |
12 ms |
33368 KB |
Output is correct |
4 |
Correct |
10 ms |
33368 KB |
Output is correct |
5 |
Correct |
9 ms |
33112 KB |
Output is correct |
6 |
Correct |
9 ms |
33112 KB |
Output is correct |
7 |
Correct |
9 ms |
31320 KB |
Output is correct |
8 |
Correct |
76 ms |
41508 KB |
Output is correct |
9 |
Correct |
9 ms |
33368 KB |
Output is correct |
10 |
Correct |
57 ms |
42068 KB |
Output is correct |
11 |
Correct |
10 ms |
33112 KB |
Output is correct |
12 |
Correct |
55 ms |
41808 KB |
Output is correct |
13 |
Correct |
67 ms |
43088 KB |
Output is correct |
14 |
Correct |
65 ms |
43088 KB |
Output is correct |
15 |
Correct |
45 ms |
42564 KB |
Output is correct |
16 |
Correct |
10 ms |
33364 KB |
Output is correct |
17 |
Correct |
9 ms |
33328 KB |
Output is correct |
18 |
Correct |
52 ms |
44368 KB |
Output is correct |
19 |
Correct |
80 ms |
47940 KB |
Output is correct |
20 |
Correct |
11 ms |
33360 KB |
Output is correct |
21 |
Correct |
9 ms |
33112 KB |
Output is correct |
22 |
Correct |
57 ms |
43976 KB |
Output is correct |
23 |
Correct |
10 ms |
33368 KB |
Output is correct |
24 |
Correct |
65 ms |
42224 KB |
Output is correct |
25 |
Correct |
70 ms |
46144 KB |
Output is correct |
26 |
Correct |
9 ms |
31320 KB |
Output is correct |
27 |
Correct |
10 ms |
31580 KB |
Output is correct |
28 |
Correct |
10 ms |
33368 KB |
Output is correct |
29 |
Correct |
9 ms |
31324 KB |
Output is correct |
30 |
Correct |
12 ms |
33368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
33112 KB |
Output is correct |
2 |
Correct |
8 ms |
33116 KB |
Output is correct |
3 |
Correct |
9 ms |
33500 KB |
Output is correct |
4 |
Correct |
9 ms |
33112 KB |
Output is correct |
5 |
Correct |
8 ms |
31064 KB |
Output is correct |
6 |
Correct |
8 ms |
31068 KB |
Output is correct |
7 |
Correct |
7 ms |
29020 KB |
Output is correct |
8 |
Correct |
8 ms |
31064 KB |
Output is correct |
9 |
Correct |
9 ms |
33112 KB |
Output is correct |
10 |
Correct |
8 ms |
33116 KB |
Output is correct |
11 |
Correct |
8 ms |
33116 KB |
Output is correct |
12 |
Correct |
9 ms |
33112 KB |
Output is correct |
13 |
Correct |
8 ms |
33112 KB |
Output is correct |
14 |
Correct |
9 ms |
33116 KB |
Output is correct |
15 |
Correct |
9 ms |
33112 KB |
Output is correct |
16 |
Correct |
8 ms |
31068 KB |
Output is correct |
17 |
Correct |
8 ms |
33112 KB |
Output is correct |
18 |
Correct |
8 ms |
31068 KB |
Output is correct |
19 |
Correct |
9 ms |
31064 KB |
Output is correct |
20 |
Correct |
8 ms |
31068 KB |
Output is correct |
21 |
Correct |
7 ms |
31064 KB |
Output is correct |
22 |
Correct |
8 ms |
31064 KB |
Output is correct |
23 |
Correct |
8 ms |
31064 KB |
Output is correct |
24 |
Correct |
8 ms |
33116 KB |
Output is correct |
25 |
Correct |
9 ms |
33112 KB |
Output is correct |
26 |
Correct |
10 ms |
33372 KB |
Output is correct |
27 |
Correct |
9 ms |
33368 KB |
Output is correct |
28 |
Correct |
10 ms |
33628 KB |
Output is correct |
29 |
Correct |
10 ms |
33556 KB |
Output is correct |
30 |
Correct |
10 ms |
33368 KB |
Output is correct |
31 |
Correct |
8 ms |
33112 KB |
Output is correct |
32 |
Correct |
10 ms |
33624 KB |
Output is correct |
33 |
Correct |
8 ms |
33112 KB |
Output is correct |
34 |
Correct |
9 ms |
31320 KB |
Output is correct |
35 |
Correct |
9 ms |
31320 KB |
Output is correct |
36 |
Correct |
9 ms |
33368 KB |
Output is correct |
37 |
Correct |
11 ms |
33368 KB |
Output is correct |
38 |
Correct |
8 ms |
33112 KB |
Output is correct |
39 |
Correct |
10 ms |
33368 KB |
Output is correct |
40 |
Correct |
10 ms |
31252 KB |
Output is correct |
41 |
Correct |
10 ms |
33368 KB |
Output is correct |
42 |
Correct |
9 ms |
33372 KB |
Output is correct |
43 |
Correct |
9 ms |
31576 KB |
Output is correct |
44 |
Correct |
8 ms |
33116 KB |
Output is correct |
45 |
Correct |
11 ms |
33624 KB |
Output is correct |
46 |
Correct |
10 ms |
33372 KB |
Output is correct |
47 |
Correct |
8 ms |
33112 KB |
Output is correct |
48 |
Correct |
10 ms |
33372 KB |
Output is correct |
49 |
Correct |
10 ms |
31320 KB |
Output is correct |
50 |
Correct |
9 ms |
31576 KB |
Output is correct |
51 |
Correct |
10 ms |
33368 KB |
Output is correct |
52 |
Correct |
9 ms |
31576 KB |
Output is correct |
53 |
Correct |
10 ms |
33368 KB |
Output is correct |
54 |
Correct |
10 ms |
31320 KB |
Output is correct |
55 |
Correct |
9 ms |
33112 KB |
Output is correct |
56 |
Correct |
44 ms |
41364 KB |
Output is correct |
57 |
Correct |
66 ms |
40528 KB |
Output is correct |
58 |
Correct |
9 ms |
33368 KB |
Output is correct |
59 |
Correct |
8 ms |
33116 KB |
Output is correct |
60 |
Correct |
8 ms |
33112 KB |
Output is correct |
61 |
Correct |
54 ms |
40276 KB |
Output is correct |
62 |
Correct |
10 ms |
31516 KB |
Output is correct |
63 |
Correct |
61 ms |
50516 KB |
Output is correct |
64 |
Correct |
61 ms |
41808 KB |
Output is correct |
65 |
Correct |
9 ms |
33368 KB |
Output is correct |
66 |
Correct |
69 ms |
42740 KB |
Output is correct |
67 |
Correct |
9 ms |
31320 KB |
Output is correct |
68 |
Correct |
12 ms |
33368 KB |
Output is correct |
69 |
Correct |
46 ms |
42812 KB |
Output is correct |
70 |
Correct |
9 ms |
31576 KB |
Output is correct |
71 |
Correct |
65 ms |
41676 KB |
Output is correct |
72 |
Correct |
75 ms |
44728 KB |
Output is correct |
73 |
Correct |
8 ms |
33112 KB |
Output is correct |
74 |
Correct |
79 ms |
50660 KB |
Output is correct |
75 |
Correct |
62 ms |
46160 KB |
Output is correct |
76 |
Correct |
9 ms |
31320 KB |
Output is correct |
77 |
Correct |
9 ms |
31320 KB |
Output is correct |
78 |
Correct |
42 ms |
41416 KB |
Output is correct |
79 |
Correct |
55 ms |
40904 KB |
Output is correct |
80 |
Correct |
12 ms |
33368 KB |
Output is correct |
81 |
Correct |
10 ms |
33368 KB |
Output is correct |
82 |
Correct |
9 ms |
33112 KB |
Output is correct |
83 |
Correct |
9 ms |
33112 KB |
Output is correct |
84 |
Correct |
9 ms |
31320 KB |
Output is correct |
85 |
Correct |
76 ms |
41508 KB |
Output is correct |
86 |
Correct |
9 ms |
33368 KB |
Output is correct |
87 |
Correct |
57 ms |
42068 KB |
Output is correct |
88 |
Correct |
10 ms |
33112 KB |
Output is correct |
89 |
Correct |
55 ms |
41808 KB |
Output is correct |
90 |
Correct |
67 ms |
43088 KB |
Output is correct |
91 |
Correct |
65 ms |
43088 KB |
Output is correct |
92 |
Correct |
45 ms |
42564 KB |
Output is correct |
93 |
Correct |
10 ms |
33364 KB |
Output is correct |
94 |
Correct |
9 ms |
33328 KB |
Output is correct |
95 |
Correct |
52 ms |
44368 KB |
Output is correct |
96 |
Correct |
80 ms |
47940 KB |
Output is correct |
97 |
Correct |
11 ms |
33360 KB |
Output is correct |
98 |
Correct |
9 ms |
33112 KB |
Output is correct |
99 |
Correct |
57 ms |
43976 KB |
Output is correct |
100 |
Correct |
10 ms |
33368 KB |
Output is correct |
101 |
Correct |
65 ms |
42224 KB |
Output is correct |
102 |
Correct |
70 ms |
46144 KB |
Output is correct |
103 |
Correct |
9 ms |
31320 KB |
Output is correct |
104 |
Correct |
10 ms |
31580 KB |
Output is correct |
105 |
Correct |
10 ms |
33368 KB |
Output is correct |
106 |
Correct |
9 ms |
31324 KB |
Output is correct |
107 |
Correct |
12 ms |
33368 KB |
Output is correct |
108 |
Correct |
446 ms |
82804 KB |
Output is correct |
109 |
Correct |
765 ms |
91068 KB |
Output is correct |
110 |
Correct |
742 ms |
104380 KB |
Output is correct |
111 |
Correct |
884 ms |
113228 KB |
Output is correct |
112 |
Correct |
748 ms |
101120 KB |
Output is correct |
113 |
Correct |
421 ms |
85724 KB |
Output is correct |
114 |
Correct |
552 ms |
69480 KB |
Output is correct |
115 |
Correct |
607 ms |
69188 KB |
Output is correct |
116 |
Correct |
625 ms |
74172 KB |
Output is correct |
117 |
Correct |
535 ms |
85160 KB |
Output is correct |
118 |
Correct |
595 ms |
70656 KB |
Output is correct |
119 |
Correct |
644 ms |
85072 KB |
Output is correct |
120 |
Correct |
728 ms |
101968 KB |
Output is correct |
121 |
Correct |
630 ms |
85072 KB |
Output is correct |
122 |
Correct |
703 ms |
78928 KB |
Output is correct |
123 |
Correct |
418 ms |
85548 KB |
Output is correct |
124 |
Correct |
754 ms |
84304 KB |
Output is correct |