# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
919122 |
2024-01-31T10:32:52 Z |
ttamx |
Mergers (JOI19_mergers) |
C++14 |
|
69 ms |
40688 KB |
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int n,k;
vector<int> adj[N],comp[N];
int sz[N],hv[N],lv[N],hd[N],pa[N];
int a[N],dp[N];
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){
dp[u]=0;
for(auto v:adj[u])if(v!=p){
dfs2(v,u);
a[u]+=a[v];
dp[u]+=max(dp[v],a[v]?0:1);
}
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> k;
for(int i=1;i<n;i++){
int u,v;
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);
}
if(n==1)cout << 0,exit(0);
if(n==2)cout << (n==k),exit(0);
int rt=1;
for(int i=2;i<=n;i++)if(adj[i].size()>1)rt=i;
dfs(rt);
hld(rt);
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;
}
}
dfs2(rt);
cout << (dp[rt]+1)/2;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:67:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int j=1;j<comp[i].size();j++){
| ~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
31320 KB |
Output is correct |
2 |
Correct |
8 ms |
31320 KB |
Output is correct |
3 |
Correct |
8 ms |
31320 KB |
Output is correct |
4 |
Correct |
8 ms |
31320 KB |
Output is correct |
5 |
Correct |
8 ms |
31324 KB |
Output is correct |
6 |
Correct |
8 ms |
31320 KB |
Output is correct |
7 |
Correct |
6 ms |
24920 KB |
Output is correct |
8 |
Correct |
8 ms |
31320 KB |
Output is correct |
9 |
Correct |
8 ms |
31320 KB |
Output is correct |
10 |
Correct |
9 ms |
31320 KB |
Output is correct |
11 |
Correct |
9 ms |
31296 KB |
Output is correct |
12 |
Correct |
8 ms |
31320 KB |
Output is correct |
13 |
Correct |
9 ms |
31320 KB |
Output is correct |
14 |
Correct |
9 ms |
31320 KB |
Output is correct |
15 |
Correct |
8 ms |
31320 KB |
Output is correct |
16 |
Correct |
7 ms |
29272 KB |
Output is correct |
17 |
Correct |
8 ms |
31320 KB |
Output is correct |
18 |
Correct |
8 ms |
31320 KB |
Output is correct |
19 |
Correct |
7 ms |
29272 KB |
Output is correct |
20 |
Correct |
8 ms |
29272 KB |
Output is correct |
21 |
Correct |
8 ms |
31320 KB |
Output is correct |
22 |
Correct |
8 ms |
31320 KB |
Output is correct |
23 |
Correct |
8 ms |
31320 KB |
Output is correct |
24 |
Correct |
8 ms |
31324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
31320 KB |
Output is correct |
2 |
Correct |
8 ms |
31320 KB |
Output is correct |
3 |
Correct |
8 ms |
31320 KB |
Output is correct |
4 |
Correct |
8 ms |
31320 KB |
Output is correct |
5 |
Correct |
8 ms |
31324 KB |
Output is correct |
6 |
Correct |
8 ms |
31320 KB |
Output is correct |
7 |
Correct |
6 ms |
24920 KB |
Output is correct |
8 |
Correct |
8 ms |
31320 KB |
Output is correct |
9 |
Correct |
8 ms |
31320 KB |
Output is correct |
10 |
Correct |
9 ms |
31320 KB |
Output is correct |
11 |
Correct |
9 ms |
31296 KB |
Output is correct |
12 |
Correct |
8 ms |
31320 KB |
Output is correct |
13 |
Correct |
9 ms |
31320 KB |
Output is correct |
14 |
Correct |
9 ms |
31320 KB |
Output is correct |
15 |
Correct |
8 ms |
31320 KB |
Output is correct |
16 |
Correct |
7 ms |
29272 KB |
Output is correct |
17 |
Correct |
8 ms |
31320 KB |
Output is correct |
18 |
Correct |
8 ms |
31320 KB |
Output is correct |
19 |
Correct |
7 ms |
29272 KB |
Output is correct |
20 |
Correct |
8 ms |
29272 KB |
Output is correct |
21 |
Correct |
8 ms |
31320 KB |
Output is correct |
22 |
Correct |
8 ms |
31320 KB |
Output is correct |
23 |
Correct |
8 ms |
31320 KB |
Output is correct |
24 |
Correct |
8 ms |
31324 KB |
Output is correct |
25 |
Correct |
9 ms |
31320 KB |
Output is correct |
26 |
Correct |
10 ms |
31576 KB |
Output is correct |
27 |
Correct |
10 ms |
31320 KB |
Output is correct |
28 |
Correct |
9 ms |
31576 KB |
Output is correct |
29 |
Correct |
10 ms |
31576 KB |
Output is correct |
30 |
Correct |
9 ms |
31320 KB |
Output is correct |
31 |
Correct |
8 ms |
31320 KB |
Output is correct |
32 |
Correct |
11 ms |
31576 KB |
Output is correct |
33 |
Correct |
8 ms |
31320 KB |
Output is correct |
34 |
Correct |
9 ms |
31320 KB |
Output is correct |
35 |
Correct |
9 ms |
29528 KB |
Output is correct |
36 |
Incorrect |
10 ms |
31320 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
31320 KB |
Output is correct |
2 |
Correct |
8 ms |
31320 KB |
Output is correct |
3 |
Correct |
8 ms |
31320 KB |
Output is correct |
4 |
Correct |
8 ms |
31320 KB |
Output is correct |
5 |
Correct |
8 ms |
31324 KB |
Output is correct |
6 |
Correct |
8 ms |
31320 KB |
Output is correct |
7 |
Correct |
6 ms |
24920 KB |
Output is correct |
8 |
Correct |
8 ms |
31320 KB |
Output is correct |
9 |
Correct |
8 ms |
31320 KB |
Output is correct |
10 |
Correct |
9 ms |
31320 KB |
Output is correct |
11 |
Correct |
9 ms |
31296 KB |
Output is correct |
12 |
Correct |
8 ms |
31320 KB |
Output is correct |
13 |
Correct |
9 ms |
31320 KB |
Output is correct |
14 |
Correct |
9 ms |
31320 KB |
Output is correct |
15 |
Correct |
8 ms |
31320 KB |
Output is correct |
16 |
Correct |
7 ms |
29272 KB |
Output is correct |
17 |
Correct |
8 ms |
31320 KB |
Output is correct |
18 |
Correct |
8 ms |
31320 KB |
Output is correct |
19 |
Correct |
7 ms |
29272 KB |
Output is correct |
20 |
Correct |
8 ms |
29272 KB |
Output is correct |
21 |
Correct |
8 ms |
31320 KB |
Output is correct |
22 |
Correct |
8 ms |
31320 KB |
Output is correct |
23 |
Correct |
8 ms |
31320 KB |
Output is correct |
24 |
Correct |
8 ms |
31324 KB |
Output is correct |
25 |
Correct |
9 ms |
31320 KB |
Output is correct |
26 |
Correct |
40 ms |
39368 KB |
Output is correct |
27 |
Correct |
69 ms |
39504 KB |
Output is correct |
28 |
Correct |
9 ms |
31548 KB |
Output is correct |
29 |
Correct |
8 ms |
31320 KB |
Output is correct |
30 |
Correct |
9 ms |
31320 KB |
Output is correct |
31 |
Incorrect |
56 ms |
39248 KB |
Output isn't correct |
32 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
37832 KB |
Output is correct |
2 |
Correct |
49 ms |
40400 KB |
Output is correct |
3 |
Correct |
9 ms |
31320 KB |
Output is correct |
4 |
Correct |
9 ms |
31320 KB |
Output is correct |
5 |
Correct |
8 ms |
31320 KB |
Output is correct |
6 |
Correct |
8 ms |
31320 KB |
Output is correct |
7 |
Correct |
9 ms |
31320 KB |
Output is correct |
8 |
Correct |
63 ms |
40688 KB |
Output is correct |
9 |
Incorrect |
9 ms |
31320 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
31320 KB |
Output is correct |
2 |
Correct |
8 ms |
31320 KB |
Output is correct |
3 |
Correct |
8 ms |
31320 KB |
Output is correct |
4 |
Correct |
8 ms |
31320 KB |
Output is correct |
5 |
Correct |
8 ms |
31324 KB |
Output is correct |
6 |
Correct |
8 ms |
31320 KB |
Output is correct |
7 |
Correct |
6 ms |
24920 KB |
Output is correct |
8 |
Correct |
8 ms |
31320 KB |
Output is correct |
9 |
Correct |
8 ms |
31320 KB |
Output is correct |
10 |
Correct |
9 ms |
31320 KB |
Output is correct |
11 |
Correct |
9 ms |
31296 KB |
Output is correct |
12 |
Correct |
8 ms |
31320 KB |
Output is correct |
13 |
Correct |
9 ms |
31320 KB |
Output is correct |
14 |
Correct |
9 ms |
31320 KB |
Output is correct |
15 |
Correct |
8 ms |
31320 KB |
Output is correct |
16 |
Correct |
7 ms |
29272 KB |
Output is correct |
17 |
Correct |
8 ms |
31320 KB |
Output is correct |
18 |
Correct |
8 ms |
31320 KB |
Output is correct |
19 |
Correct |
7 ms |
29272 KB |
Output is correct |
20 |
Correct |
8 ms |
29272 KB |
Output is correct |
21 |
Correct |
8 ms |
31320 KB |
Output is correct |
22 |
Correct |
8 ms |
31320 KB |
Output is correct |
23 |
Correct |
8 ms |
31320 KB |
Output is correct |
24 |
Correct |
8 ms |
31324 KB |
Output is correct |
25 |
Correct |
9 ms |
31320 KB |
Output is correct |
26 |
Correct |
10 ms |
31576 KB |
Output is correct |
27 |
Correct |
10 ms |
31320 KB |
Output is correct |
28 |
Correct |
9 ms |
31576 KB |
Output is correct |
29 |
Correct |
10 ms |
31576 KB |
Output is correct |
30 |
Correct |
9 ms |
31320 KB |
Output is correct |
31 |
Correct |
8 ms |
31320 KB |
Output is correct |
32 |
Correct |
11 ms |
31576 KB |
Output is correct |
33 |
Correct |
8 ms |
31320 KB |
Output is correct |
34 |
Correct |
9 ms |
31320 KB |
Output is correct |
35 |
Correct |
9 ms |
29528 KB |
Output is correct |
36 |
Incorrect |
10 ms |
31320 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |