#include<bits/stdc++.h>
#define fi first
#define se second
#define rep(a, b) for(int a = 0; a < (b); a++)
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<ll> vl;
const int N=2e5+10;
const int L=19;
const int M=4e6+N+10;
vi graf[N],r[N],graf2[M];
int t[N],gl[N],spojna[M],ile[M],pre[N],post[M],kon[M];
pair<int,int> jump[N][L+1];
int l=0,l2=0,n,k;
bitset<M>kan;
bitset<M>vis;
void add(int a,int b){
graf2[a].push_back(b);
}
void dfs(int v,int o,int g){
pre[v]=++l2;
gl[v]=g;
jump[v][0]={o,++l};
add(l,t[o-1]);
for(int j=1;j<=L;j++) jump[v][j].fi=jump[jump[v][j-1].fi][j-1].fi,jump[v][j].se=++l,add(l,jump[v][j-1].se),add(l,jump[jump[v][j-1].fi][j-1].se);
for(auto u:graf[v]){
if(u==o) continue;
dfs(u,v,g+1);
}
}
int lca(int a,int b){
if(gl[a]<gl[b]) swap(a,b);
for(int i=L;i>=0;i--) if(gl[jump[a][i].fi]>=gl[b]) a=jump[a][i].fi;
if(a==b) return a;
int res=0;
for(int i=L;i>=0;i--){
if(jump[a][i].fi==jump[b][i].fi) res=jump[a][i].fi;
else a=jump[a][i].fi,b=jump[b][i].fi;
}
return res;
}
void dod(int a,int l,int x){
for(int i=L;i>=0;i--) if(gl[jump[a][i].fi]>=gl[l]) add(x,jump[a][i].se),a=jump[a][i].fi;
}
void dfs2(int v){
if(vis[v]) return;
vis[v]=1;
for(auto u:graf2[v]) dfs2(u);
post[l2++]=v;
}
void dfs3(int v,int s){
if(vis[v]) return;
vis[v]=1;
spojna[v]=s;
if(v<=k-1) ile[s]++;
for(auto u:graf2[v]) dfs3(u,s);
}
void odw(){
rep(i,l+1) kon[i]=graf2[i].size();
rep(i,l+1) {
for(int j=kon[i]-1;j>=0;j--) graf2[graf2[i][j]].push_back(i),swap(graf2[i][j],graf2[i][graf2[i].size()-1]),graf2[i].pop_back();
graf2[i].shrink_to_fit();
}
}
void solve(){
cin>>n>>k;
rep(i,n-1){
int a,b;
cin>>a>>b;
graf[a].push_back(b),graf[b].push_back(a);
}
l=k-1;
rep(i,n){
cin>>t[i];
t[i]--;
r[t[i]].push_back(i+1);
}
dfs(1,1,1);
vector<pair<int,int> >vv;
rep(i,k){
vv.clear();
for(auto u:r[i]) vv.push_back({pre[u],u});
sort(vv.begin(),vv.end());
for(int j=1;j<vv.size();j++){
int l=lca(vv[j].se,vv[j-1].se);
dod(vv[j].se,l,i),dod(vv[j-1].se,l,i);
}
}
l2=0;
rep(i,l+1) dfs2(i);
rep(i,l+1) vis[i]=0;
odw();
int curr=0;
for(int i=l;i>=0;i--) if(!vis[post[i]]) dfs3(post[i],++curr);
odw();
rep(i,l+1){
bool b=0;
for(auto u:graf2[i]) if(spojna[i]!=spojna[u]) b=1;
if(!kan[spojna[i]]) kan[spojna[i]]=b;
}
int res=INT_MAX;
rep(i,l+1) if(!kan[spojna[i]]) res=min(res,ile[spojna[i]]);
cout<<res-1<<"\n";
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
solve();
}
Compilation message
capital_city.cpp: In function 'void solve()':
capital_city.cpp:85:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for(int j=1;j<vv.size();j++){
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
108364 KB |
Output is correct |
2 |
Correct |
49 ms |
108352 KB |
Output is correct |
3 |
Correct |
49 ms |
108356 KB |
Output is correct |
4 |
Correct |
49 ms |
108368 KB |
Output is correct |
5 |
Correct |
49 ms |
108392 KB |
Output is correct |
6 |
Correct |
48 ms |
108356 KB |
Output is correct |
7 |
Correct |
48 ms |
108416 KB |
Output is correct |
8 |
Correct |
50 ms |
108312 KB |
Output is correct |
9 |
Correct |
48 ms |
108388 KB |
Output is correct |
10 |
Correct |
54 ms |
108364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
108364 KB |
Output is correct |
2 |
Correct |
49 ms |
108352 KB |
Output is correct |
3 |
Correct |
49 ms |
108356 KB |
Output is correct |
4 |
Correct |
49 ms |
108368 KB |
Output is correct |
5 |
Correct |
49 ms |
108392 KB |
Output is correct |
6 |
Correct |
48 ms |
108356 KB |
Output is correct |
7 |
Correct |
48 ms |
108416 KB |
Output is correct |
8 |
Correct |
50 ms |
108312 KB |
Output is correct |
9 |
Correct |
48 ms |
108388 KB |
Output is correct |
10 |
Correct |
54 ms |
108364 KB |
Output is correct |
11 |
Correct |
67 ms |
111428 KB |
Output is correct |
12 |
Correct |
62 ms |
111488 KB |
Output is correct |
13 |
Correct |
63 ms |
111412 KB |
Output is correct |
14 |
Correct |
66 ms |
111340 KB |
Output is correct |
15 |
Correct |
62 ms |
111172 KB |
Output is correct |
16 |
Correct |
61 ms |
111116 KB |
Output is correct |
17 |
Correct |
74 ms |
111352 KB |
Output is correct |
18 |
Correct |
63 ms |
110964 KB |
Output is correct |
19 |
Correct |
62 ms |
111220 KB |
Output is correct |
20 |
Correct |
62 ms |
111076 KB |
Output is correct |
21 |
Correct |
61 ms |
110972 KB |
Output is correct |
22 |
Correct |
62 ms |
111280 KB |
Output is correct |
23 |
Correct |
65 ms |
112196 KB |
Output is correct |
24 |
Correct |
68 ms |
111172 KB |
Output is correct |
25 |
Correct |
62 ms |
111044 KB |
Output is correct |
26 |
Correct |
65 ms |
110984 KB |
Output is correct |
27 |
Correct |
76 ms |
111048 KB |
Output is correct |
28 |
Correct |
63 ms |
111080 KB |
Output is correct |
29 |
Correct |
64 ms |
111032 KB |
Output is correct |
30 |
Correct |
67 ms |
111172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3097 ms |
405484 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
108364 KB |
Output is correct |
2 |
Correct |
49 ms |
108352 KB |
Output is correct |
3 |
Correct |
49 ms |
108356 KB |
Output is correct |
4 |
Correct |
49 ms |
108368 KB |
Output is correct |
5 |
Correct |
49 ms |
108392 KB |
Output is correct |
6 |
Correct |
48 ms |
108356 KB |
Output is correct |
7 |
Correct |
48 ms |
108416 KB |
Output is correct |
8 |
Correct |
50 ms |
108312 KB |
Output is correct |
9 |
Correct |
48 ms |
108388 KB |
Output is correct |
10 |
Correct |
54 ms |
108364 KB |
Output is correct |
11 |
Correct |
67 ms |
111428 KB |
Output is correct |
12 |
Correct |
62 ms |
111488 KB |
Output is correct |
13 |
Correct |
63 ms |
111412 KB |
Output is correct |
14 |
Correct |
66 ms |
111340 KB |
Output is correct |
15 |
Correct |
62 ms |
111172 KB |
Output is correct |
16 |
Correct |
61 ms |
111116 KB |
Output is correct |
17 |
Correct |
74 ms |
111352 KB |
Output is correct |
18 |
Correct |
63 ms |
110964 KB |
Output is correct |
19 |
Correct |
62 ms |
111220 KB |
Output is correct |
20 |
Correct |
62 ms |
111076 KB |
Output is correct |
21 |
Correct |
61 ms |
110972 KB |
Output is correct |
22 |
Correct |
62 ms |
111280 KB |
Output is correct |
23 |
Correct |
65 ms |
112196 KB |
Output is correct |
24 |
Correct |
68 ms |
111172 KB |
Output is correct |
25 |
Correct |
62 ms |
111044 KB |
Output is correct |
26 |
Correct |
65 ms |
110984 KB |
Output is correct |
27 |
Correct |
76 ms |
111048 KB |
Output is correct |
28 |
Correct |
63 ms |
111080 KB |
Output is correct |
29 |
Correct |
64 ms |
111032 KB |
Output is correct |
30 |
Correct |
67 ms |
111172 KB |
Output is correct |
31 |
Execution timed out |
3097 ms |
405484 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |