//#include<i_am_noob_orz>
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define int ll
#define ull unsigned long long
#define pii pair<int,int>
#define X first
#define Y second
#define mod ((ll)1e9+7)
#define pb push_back
#define mp make_pair
#define abs(x) ((x)>0?(x):(-(x)))
#define F(n) Fi(i,n)
#define Fi(i,n) Fl(i,0,n)
#define Fl(i,l,n) for(int i=l;i<n;i++)
#define memres(a) memset(a,0,sizeof(a))
#define all(a) a.begin(),a.end()
#define sz(a) ((int)a.size())
#define ceiling(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define bit_count(x) __builtin_popcountll((x))
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define jimmy_is_kind false
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> rbtree;
//#define LOCAL
#ifdef LOCAL
#define bug(...) cerr<<"#"<<__LINE__<<' '<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do(T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr<<x<<", "; _do(y...);}
#define IOS()
#else
#define IOS() ios_base::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
#define bug(...)
#endif
int add(int a,int b){return(a+b>=mod?a+b-mod:a+b);}
int sub(int a,int b){return(a<b?a+mod-b:a-b);}
int po(int a,int b){
if(b==0)return 1;
if(b==1)return(a%mod);
int tem=po(a,b/2);
if(b&1)return(((tem*tem)%mod)*a)%mod;
else return(tem*tem)%mod;
}
int GCD(int a,int b){
int x=0;
int ra,rb;
while(a&&b){
if(((a&1)==0)&&((b&1)==0)){
a>>=1,b>>=1,x++;
}
else if((a^b)&1){
if(a&1)b>>=1;
else a>>=1;
}
else{
ra=abs(a-b),rb=min(a,b);
a=ra,b=rb;
}
}
return max(a,b)<<x;
}
int gcd(int a,int b){if(b==0)return a;return gcd(b,a%b);}
int n,k,dep[200010],ord[200010],pa[200010][19],inde=0,ind,id=0,pt[200010][19],co[200010],cnt[200010],out[200010];
vector<int> g[200010],rg[4000010],cl[200010];
void dfs(int p,int f,int d){
dep[p]=d;
ord[p]=inde++;
pa[p][0]=f;
for(int u:g[p]){
if(u==f)continue;
dfs(u,p,d+1);
}
}
bool cmp(int x,int y){
return ord[x]<ord[y];
}
void ad_ed(int p,int l){
int tp=p;
for(int i=19;i>=0;i--){
if((1ll<<i)&l)rg[p].pb(pt[tp][i]),tp=pa[tp][i];
}
}
void lca_ed(int x,int y){
if(x==y)return;
int rx=x,ry=y;
int xd=dep[x],yd=dep[y];
if(xd>yd)swap(x,y),swap(xd,yd);
int len=yd-xd;
for(int i=19;i>=0;i--){
if((1ll<<i)&len)y=pa[y][i];
}
int r;
if(x==y)r=dep[x];
else{
for(int i=19;i>=0;i--){
if(pa[x][i]==-1)continue;
if(pa[x][i]!=pa[y][i])x=pa[x][i],y=pa[y][i];
}
r=pa[x][0];
r=dep[r];
}
int xl=xd-r,yl=yd-r;
bug(rx,xl,ry,yl);
if(xl!=0)ad_ed(rx,xl);
if(yl!=0)ad_ed(ry,yl);
}
vector<int> sta;
int dfn[4000010],low[4000010],ind1=0,gr[4000010];
bool inst[4000010]={0};
void tarjan(int p){
dfn[p]=low[p]=++ind1;
inst[p]=1;
sta.pb(p);
for(int u:rg[p]){
if(dfn[u]==0){
tarjan(u);
low[p]=min(low[p],low[u]);
}
else if(inst[u]){
low[p]=min(low[p],dfn[u]);
}
}
if(low[p]==dfn[p]){
id++;
while(sta.back()!=p){
inst[sta.back()]=0;
gr[sta.back()]=id;
sta.pop_back();
}
assert(sta.back()==p);
inst[p]=0;
gr[p]=id;
sta.pop_back();
}
}
signed main(){
IOS();
cin>>n>>k;
F(n-1){
int x,y;
cin>>x>>y;
--x,--y;
g[x].pb(y);
g[y].pb(x);
}
dfs(0,-1,0);
ind=n;
for(int j=0;j<18;j++)F(n){if(pa[i][j]!=-1)pa[i][j+1]=pa[pa[i][j]][j];else pa[i][j+1]=-1;}
F(n){
if(i==0)continue;
pt[i][0]=ind++;
rg[pt[i][0]].pb(i);
rg[pt[i][0]].pb(pa[i][0]);
for(int j=1;(1ll<<j)<=dep[i];j++){
pt[i][j]=ind++;
rg[pt[i][j]].pb(i);
rg[pt[i][j]].pb(pt[i][j-1]);
rg[pt[i][j]].pb(pt[pa[i][j-1]][j-1]);
}
}
F(n){
int x;
cin>>x;
--x;
cl[x].pb(i);
co[i]=x;
}
F(k){
sort(all(cl[i]),cmp);
if(sz(cl[i])>=2){
cl[i].pb(cl[i][0]);
Fi(j,sz(cl[i])-1){
bug(cl[i][j],cl[i][j+1]);
rg[cl[i][j]].pb(cl[i][j+1]);
lca_ed(cl[i][j],cl[i][j+1]);
}
}
}
memres(dfn);
memres(low);
memres(cnt);
F(n){
if(dfn[i]==0)tarjan(i);
}
memres(out);
F(ind){
if(gr[i]==0)continue;
for(int j:rg[i]){
if(gr[j]==0)continue;
if(gr[i]!=gr[j]){
out[gr[i]]++;
}
}
}
F(k){
if(sz(cl[i]))cnt[gr[cl[i][0]]]++;
}
int ans=n;
for(int i=1;i<id;i++)bug(cnt[i],out[i]);
for(int i=1;i<id;i++)if(cnt[i]!=0&&out[i]==0)ans=min(ans,cnt[i]),bug(cnt[i],out[i]);
cout<<ans-1<<endl;
return 0;
}
Compilation message
capital_city.cpp: In function 'int main()':
capital_city.cpp:215:85: error: expected primary-expression before ';' token
215 | for(int i=1;i<id;i++)if(cnt[i]!=0&&out[i]==0)ans=min(ans,cnt[i]),bug(cnt[i],out[i]);
| ^