#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=17;
const int M=4e6+5e5;
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){
vis[v]=1;
spojna[v]=s;
if(v<=k-1) ile[s]++;
for(auto u:graf2[v])
{
if(!vis[u]) dfs3(u,s);
else if(spojna[u]!=s) kan[spojna[u]]=1;
}
}
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);
int res=INT_MAX;
rep(i,k) 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:88: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]
88 | for(int j=1;j<vv.size();j++){
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
115380 KB |
Output is correct |
2 |
Correct |
55 ms |
115380 KB |
Output is correct |
3 |
Correct |
54 ms |
115408 KB |
Output is correct |
4 |
Correct |
56 ms |
115436 KB |
Output is correct |
5 |
Correct |
56 ms |
115396 KB |
Output is correct |
6 |
Correct |
59 ms |
115384 KB |
Output is correct |
7 |
Correct |
60 ms |
115440 KB |
Output is correct |
8 |
Correct |
54 ms |
115336 KB |
Output is correct |
9 |
Correct |
53 ms |
115348 KB |
Output is correct |
10 |
Correct |
54 ms |
115380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
115380 KB |
Output is correct |
2 |
Correct |
55 ms |
115380 KB |
Output is correct |
3 |
Correct |
54 ms |
115408 KB |
Output is correct |
4 |
Correct |
56 ms |
115436 KB |
Output is correct |
5 |
Correct |
56 ms |
115396 KB |
Output is correct |
6 |
Correct |
59 ms |
115384 KB |
Output is correct |
7 |
Correct |
60 ms |
115440 KB |
Output is correct |
8 |
Correct |
54 ms |
115336 KB |
Output is correct |
9 |
Correct |
53 ms |
115348 KB |
Output is correct |
10 |
Correct |
54 ms |
115380 KB |
Output is correct |
11 |
Correct |
63 ms |
118140 KB |
Output is correct |
12 |
Correct |
63 ms |
118212 KB |
Output is correct |
13 |
Correct |
77 ms |
118084 KB |
Output is correct |
14 |
Correct |
76 ms |
118204 KB |
Output is correct |
15 |
Correct |
62 ms |
117884 KB |
Output is correct |
16 |
Correct |
63 ms |
117828 KB |
Output is correct |
17 |
Correct |
62 ms |
117960 KB |
Output is correct |
18 |
Correct |
69 ms |
117760 KB |
Output is correct |
19 |
Correct |
61 ms |
117956 KB |
Output is correct |
20 |
Correct |
68 ms |
117804 KB |
Output is correct |
21 |
Correct |
62 ms |
117776 KB |
Output is correct |
22 |
Correct |
62 ms |
117988 KB |
Output is correct |
23 |
Correct |
78 ms |
118952 KB |
Output is correct |
24 |
Correct |
64 ms |
117748 KB |
Output is correct |
25 |
Correct |
64 ms |
117744 KB |
Output is correct |
26 |
Correct |
60 ms |
117712 KB |
Output is correct |
27 |
Correct |
91 ms |
117856 KB |
Output is correct |
28 |
Correct |
75 ms |
117856 KB |
Output is correct |
29 |
Correct |
66 ms |
117828 KB |
Output is correct |
30 |
Correct |
74 ms |
117860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2993 ms |
384052 KB |
Output is correct |
2 |
Correct |
2277 ms |
375864 KB |
Output is correct |
3 |
Correct |
2589 ms |
378216 KB |
Output is correct |
4 |
Correct |
2319 ms |
375880 KB |
Output is correct |
5 |
Correct |
2740 ms |
389292 KB |
Output is correct |
6 |
Correct |
2230 ms |
375640 KB |
Output is correct |
7 |
Correct |
2524 ms |
377256 KB |
Output is correct |
8 |
Correct |
1983 ms |
370084 KB |
Output is correct |
9 |
Correct |
1911 ms |
341092 KB |
Output is correct |
10 |
Correct |
1826 ms |
337724 KB |
Output is correct |
11 |
Correct |
2001 ms |
341504 KB |
Output is correct |
12 |
Correct |
1921 ms |
344776 KB |
Output is correct |
13 |
Correct |
1986 ms |
338148 KB |
Output is correct |
14 |
Correct |
1914 ms |
344536 KB |
Output is correct |
15 |
Correct |
2005 ms |
346092 KB |
Output is correct |
16 |
Correct |
1926 ms |
339176 KB |
Output is correct |
17 |
Correct |
2077 ms |
340084 KB |
Output is correct |
18 |
Correct |
1911 ms |
340464 KB |
Output is correct |
19 |
Correct |
2005 ms |
345368 KB |
Output is correct |
20 |
Correct |
1915 ms |
344292 KB |
Output is correct |
21 |
Correct |
58 ms |
115396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
115380 KB |
Output is correct |
2 |
Correct |
55 ms |
115380 KB |
Output is correct |
3 |
Correct |
54 ms |
115408 KB |
Output is correct |
4 |
Correct |
56 ms |
115436 KB |
Output is correct |
5 |
Correct |
56 ms |
115396 KB |
Output is correct |
6 |
Correct |
59 ms |
115384 KB |
Output is correct |
7 |
Correct |
60 ms |
115440 KB |
Output is correct |
8 |
Correct |
54 ms |
115336 KB |
Output is correct |
9 |
Correct |
53 ms |
115348 KB |
Output is correct |
10 |
Correct |
54 ms |
115380 KB |
Output is correct |
11 |
Correct |
63 ms |
118140 KB |
Output is correct |
12 |
Correct |
63 ms |
118212 KB |
Output is correct |
13 |
Correct |
77 ms |
118084 KB |
Output is correct |
14 |
Correct |
76 ms |
118204 KB |
Output is correct |
15 |
Correct |
62 ms |
117884 KB |
Output is correct |
16 |
Correct |
63 ms |
117828 KB |
Output is correct |
17 |
Correct |
62 ms |
117960 KB |
Output is correct |
18 |
Correct |
69 ms |
117760 KB |
Output is correct |
19 |
Correct |
61 ms |
117956 KB |
Output is correct |
20 |
Correct |
68 ms |
117804 KB |
Output is correct |
21 |
Correct |
62 ms |
117776 KB |
Output is correct |
22 |
Correct |
62 ms |
117988 KB |
Output is correct |
23 |
Correct |
78 ms |
118952 KB |
Output is correct |
24 |
Correct |
64 ms |
117748 KB |
Output is correct |
25 |
Correct |
64 ms |
117744 KB |
Output is correct |
26 |
Correct |
60 ms |
117712 KB |
Output is correct |
27 |
Correct |
91 ms |
117856 KB |
Output is correct |
28 |
Correct |
75 ms |
117856 KB |
Output is correct |
29 |
Correct |
66 ms |
117828 KB |
Output is correct |
30 |
Correct |
74 ms |
117860 KB |
Output is correct |
31 |
Correct |
2993 ms |
384052 KB |
Output is correct |
32 |
Correct |
2277 ms |
375864 KB |
Output is correct |
33 |
Correct |
2589 ms |
378216 KB |
Output is correct |
34 |
Correct |
2319 ms |
375880 KB |
Output is correct |
35 |
Correct |
2740 ms |
389292 KB |
Output is correct |
36 |
Correct |
2230 ms |
375640 KB |
Output is correct |
37 |
Correct |
2524 ms |
377256 KB |
Output is correct |
38 |
Correct |
1983 ms |
370084 KB |
Output is correct |
39 |
Correct |
1911 ms |
341092 KB |
Output is correct |
40 |
Correct |
1826 ms |
337724 KB |
Output is correct |
41 |
Correct |
2001 ms |
341504 KB |
Output is correct |
42 |
Correct |
1921 ms |
344776 KB |
Output is correct |
43 |
Correct |
1986 ms |
338148 KB |
Output is correct |
44 |
Correct |
1914 ms |
344536 KB |
Output is correct |
45 |
Correct |
2005 ms |
346092 KB |
Output is correct |
46 |
Correct |
1926 ms |
339176 KB |
Output is correct |
47 |
Correct |
2077 ms |
340084 KB |
Output is correct |
48 |
Correct |
1911 ms |
340464 KB |
Output is correct |
49 |
Correct |
2005 ms |
345368 KB |
Output is correct |
50 |
Correct |
1915 ms |
344292 KB |
Output is correct |
51 |
Correct |
58 ms |
115396 KB |
Output is correct |
52 |
Correct |
1655 ms |
399504 KB |
Output is correct |
53 |
Correct |
1543 ms |
374240 KB |
Output is correct |
54 |
Correct |
1543 ms |
374904 KB |
Output is correct |
55 |
Correct |
1508 ms |
376960 KB |
Output is correct |
56 |
Correct |
1532 ms |
384864 KB |
Output is correct |
57 |
Correct |
1555 ms |
370344 KB |
Output is correct |
58 |
Correct |
1944 ms |
359636 KB |
Output is correct |
59 |
Correct |
1914 ms |
360280 KB |
Output is correct |
60 |
Correct |
2131 ms |
375408 KB |
Output is correct |
61 |
Correct |
2080 ms |
370240 KB |
Output is correct |
62 |
Correct |
2205 ms |
376168 KB |
Output is correct |
63 |
Correct |
2201 ms |
376100 KB |
Output is correct |
64 |
Correct |
2127 ms |
371380 KB |
Output is correct |
65 |
Correct |
2205 ms |
375412 KB |
Output is correct |
66 |
Correct |
1373 ms |
354648 KB |
Output is correct |
67 |
Correct |
1456 ms |
370828 KB |
Output is correct |
68 |
Correct |
1410 ms |
354572 KB |
Output is correct |
69 |
Correct |
1350 ms |
353012 KB |
Output is correct |
70 |
Correct |
1511 ms |
352512 KB |
Output is correct |
71 |
Correct |
1361 ms |
354700 KB |
Output is correct |
72 |
Correct |
1323 ms |
354560 KB |
Output is correct |
73 |
Correct |
1364 ms |
370936 KB |
Output is correct |
74 |
Correct |
1414 ms |
354384 KB |
Output is correct |
75 |
Correct |
1411 ms |
352760 KB |
Output is correct |
76 |
Correct |
1755 ms |
354816 KB |
Output is correct |
77 |
Correct |
1600 ms |
350580 KB |
Output is correct |
78 |
Correct |
1972 ms |
340544 KB |
Output is correct |
79 |
Correct |
1968 ms |
337648 KB |
Output is correct |
80 |
Correct |
1908 ms |
343960 KB |
Output is correct |
81 |
Correct |
1933 ms |
340960 KB |
Output is correct |
82 |
Correct |
2163 ms |
342284 KB |
Output is correct |
83 |
Correct |
1927 ms |
338040 KB |
Output is correct |
84 |
Correct |
2119 ms |
346492 KB |
Output is correct |
85 |
Correct |
1962 ms |
342704 KB |
Output is correct |
86 |
Correct |
1944 ms |
337388 KB |
Output is correct |
87 |
Correct |
1949 ms |
339008 KB |
Output is correct |
88 |
Correct |
2337 ms |
355452 KB |
Output is correct |
89 |
Correct |
2285 ms |
349260 KB |
Output is correct |
90 |
Correct |
2214 ms |
347844 KB |
Output is correct |
91 |
Correct |
2282 ms |
351320 KB |
Output is correct |
92 |
Correct |
2370 ms |
352136 KB |
Output is correct |
93 |
Correct |
2384 ms |
349844 KB |
Output is correct |
94 |
Correct |
2259 ms |
348880 KB |
Output is correct |
95 |
Correct |
2329 ms |
350920 KB |
Output is correct |
96 |
Correct |
2328 ms |
351556 KB |
Output is correct |
97 |
Correct |
2369 ms |
351740 KB |
Output is correct |