//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int mxn=2000+5;
const int inf=1e9;
vector<int> adj[mxn],adj2[mxn],adjr[mxn];
bool visited[mxn];
int c[mxn];
vector<vector<int>> belong(mxn);
int n,k;
vector<vector<int>> mp(mxn,vector<int>(mxn));
vector<bool> did(mxn);
vector<int> ord;
int cnt[mxn];
int scc[mxn];
int in[mxn];
int top[mxn];
bool no[mxn];
int up[mxn][20];
int depth[mxn];
int lca(int a,int b){
if(depth[a]<depth[b]) swap(a,b);
int len=depth[a]-depth[b];
for(int i=0;i<20;i++){
if(len&(1<<i)) a=up[a][i];
}
if(a==b) return a;
for(int i=19;i>=0;i--){
int ta=up[a][i];
int tb=up[b][i];
if(ta!=tb){
a=ta;
b=tb;
}
}
return up[a][0];
}
void dfs(int v){
visited[v]=true;
for(auto u:adj2[v]){
if(visited[u]) continue;
dfs(u);
}
ord.push_back(v);
}
void dfsr(int v,int id){
visited[v]=true;
scc[v]=id;
cnt[id]++;
for(auto u:adjr[v]){
if(visited[u]) continue;
dfsr(u,id);
}
}
void dfs1(int v,int p=0){
depth[v]=depth[p]+1;
up[v][0]=p;
for(auto u:adj[v]){
if(u==p) continue;
dfs1(u,v);
}
}
int main() {_
cin>>n>>k;
for(int i=0;i<n-1;i++){
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for(int i=1;i<=n;i++){
cin>>c[i];
c[i]--;
belong[c[i]].push_back(i);
}
dfs1(1);
for(int j=1;j<20;j++){
for(int i=1;i<=n;i++){
up[i][j]=up[up[i][j-1]][j-1];
}
}
for(int i=0;i<k;i++){
for(int j=0;j<k;j++){
did[j]=false;
}
for(int j=1;j<=n;j++){
visited[j]=false;
}
int node=belong[i][0];
for(int j=1;j<belong[i].size();j++){
node=lca(node,belong[i][j]);
}
vector<int> vec;
for(auto v:belong[i]){
while(!visited[v] and v!=up[node][0]){
vec.push_back(v);
visited[v]=true;
v=up[v][0];
}
}
for(auto v:vec){
int x=c[v];
if(x!=i and !did[x]){
did[x]=true;
for(auto u:belong[x]){
if(!visited[u]){
adj2[i].push_back(x);
adjr[x].push_back(i);
break;
}
}
}
}
}
fill(visited,visited+mxn,false);
for(int i=0;i<k;i++){
if(!visited[i]){
dfs(i);
}
}
reverse(all(ord));
fill(visited,visited+mxn,false);
int sccnt=0;
for(int i=0;i<k;i++){
if(!visited[ord[i]]){
dfsr(ord[i],sccnt);
sccnt++;
}
}
for(int i=0;i<k;i++){
for(auto u:adj2[i]){
//cout<<i+1<<' '<<u+1<<'\n';
if(scc[i]==scc[u]) continue;
in[scc[i]]++;
}
}
int ans=inf;
for(int i=0;i<sccnt;i++){
if(in[i]==0) ans=min(ans,cnt[i]-1);
}
cout<<ans<<'\n';
return 0;
}
//maybe its multiset not set
//yeeorz
//laborz
Compilation message
capital_city.cpp: In function 'int main()':
capital_city.cpp:110:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | for(int j=1;j<belong[i].size();j++){
| ~^~~~~~~~~~~~~~~~~
capital_city.cpp: In function 'void setIO(std::string)':
capital_city.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
16472 KB |
Output is correct |
2 |
Correct |
11 ms |
16476 KB |
Output is correct |
3 |
Correct |
8 ms |
16476 KB |
Output is correct |
4 |
Incorrect |
8 ms |
16472 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
16472 KB |
Output is correct |
2 |
Correct |
11 ms |
16476 KB |
Output is correct |
3 |
Correct |
8 ms |
16476 KB |
Output is correct |
4 |
Incorrect |
8 ms |
16472 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
20 ms |
32860 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
16472 KB |
Output is correct |
2 |
Correct |
11 ms |
16476 KB |
Output is correct |
3 |
Correct |
8 ms |
16476 KB |
Output is correct |
4 |
Incorrect |
8 ms |
16472 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |