#include<bits/stdc++.h>
using namespace std;
bool M1;
#define PI 3.14159265358979323846
#define sz(a) (int)a.size()
#define all(x) x.begin(),x.end()
#define ii pair<int,int>
#define iii pair<int,ii>
#define iv pair<ii,ii>
#define se second
#define fi first
#define ffi fi.fi
#define sfi se.fi
#define sse se.se
#define fse fi.se
#define lt(i, c, d) for(int i = c; i <= d; ++i)
#define fl(i, c, d) for(int i = d; i >= c; --i)
#define pb push_back
#define emb emplace_back
#define emf emplace_front
#define em emplace
#define look_memory cerr<<abs(&M2-&M1)/1024.0/1024<<'\n'
#define look_time cerr << "TIME : " << clock() * 0.001 << "s" <<'\n'
const int N=1e6+5,lg=30,mod=1e9+7;
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
int Rand(int u,int v){
return u+rd()%(v-u+1);
}
int dx[]={1,0,-1,0,1,1,-1,-1};
int dy[]={0,-1,0,1,1,-1,1,-1};
int node,k,C[N],ans=1e9,query,edge,up[N][lg+5],h[N],tin[N],tout[N],timer;
vector<int>ver[N],mau[N];
void dfs(int u,int cha)
{
tin[u]=++timer;
for(auto v:ver[u])
{
if(v==cha)continue;
up[v][0]=u;
h[v]=h[u]+1;
dfs(v,u);
}
tout[u]=timer;
}
int lca(int u,int v)
{
if(h[u]<h[v])swap(u,v);
for(int j=lg;j>=0;--j)
{
if(h[u]-h[v]>=(1<<j))
u=up[u][j];
}
if(u==v)return u;
for(int j=lg;j>=0;--j)
{
if(up[u][j]!=up[v][j])
{
u=up[u][j];
v=up[v][j];
}
}
return up[u][0];
}
void build(){
dfs(1,-1);
for(int j=1;j<=lg;++j)
for(int i=1;i<=node;++i)up[i][j]=up[up[i][j-1]][j-1];
}
bool check[N];
void giai(int st){
for(int i=1;i<=k;++i)check[i]=false;
check[st]=true;
queue<int>p;
p.em(st);
int res=0;
while(!p.empty()){
int x=p.front();
++res;
p.pop();
for(int i=0;i<sz(mau[x]);++i){
int u=mau[x][i],v=mau[x][(i+1)%sz(mau[x])];
int l=lca(u,v);
while(u!=l){
if(!check[C[u]]){
p.em(C[u]);
check[C[u]]=true;
}
u=up[u][0];
}
while(v!=l){
if(!check[C[v]]){
p.em(C[v]);
check[C[v]]=true;
}
v=up[v][0];
}
if(!check[C[l]]){
p.em(C[l]);
check[C[l]]=true;
}
}
}
ans=min(ans,res-1);
}
bool M2;
void solve(){
cin >> node >> k;
for(int i=1;i<node;++i){
int u,v;
cin >> u >> v;
ver[u].emb(v);
ver[v].emb(u);
}
build();
for(int i=1;i<=node;++i){
cin >> C[i];
mau[C[i]].emb(i);
}
for(int i=1;i<=k;++i)sort(all(mau[i]));
for(int i=1;i<=k;++i){
giai(i);
}
cout << ans;
}
main()
{
srand(time(0));
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#define task "aws"
if(fopen(task".inp","r")){
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
int t=1;
// cin >> t;
while(t--){
solve();cout<<'\n';
}
look_memory;
look_time;
}
컴파일 시 표준 에러 (stderr) 메시지
capital_city.cpp:130:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
130 | main()
| ^~~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:138:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
138 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp:139:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
139 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |