#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int blo[N], col[N], par[N], vis[N], typ[N];
vector<int> co[N];
vector<int> E[N];
void color(int v, int p, int c){
col[v]=c;
for(int u:E[v]){
if(!blo[u] && u!=p){
color(u, v, c);
}
}
}
void dfs(int v){
for(int u:E[v]){
if(u!=par[v] && !blo[u]){
par[u]=v;
dfs(u);
}
}
}
int wsk=1;
int ans=N;
void solve(int v, int id){
//cout<<v<<" "<<id<<"\n";
par[v]=0;
dfs(v);
vector<int> V;
V.push_back(typ[v]);
vis[typ[v]]=1;
for(int i=0; i<V.size(); i++){
int c=V[i];
for(int v:co[c]){
if(col[v]!=id){
for(int u:V)vis[u]=0;
return;
}
if(par[v] && !vis[typ[par[v]]]){
vis[typ[par[v]]]=1;
V.push_back(typ[par[v]]);
}
}
}
//cout<<V.size()<<"\n";
ans=min(ans, (int)V.size());
for(int u:V)vis[u]=0;
}
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int main(){
int n, k;
cin>>n>>k;
for(int i=1; i<n; i++){
int u, v;
cin>>u>>v;
E[u].push_back(v);
E[v].push_back(u);
}
for(int i=1; i<=n; i++){
cin>>typ[i];
co[typ[i]].push_back(i);
}
vector<int> kol;
for(int i=1; i<=n; i++)kol.push_back(i);
shuffle(kol.begin(), kol.end(), rng);
for(int i:kol){
solve(i, col[i]);
blo[i]=1;
for(int u:E[i])color(u, i, wsk++);
}
cout<<ans-1<<"\n";
}
Compilation message
capital_city.cpp: In function 'void solve(int, int)':
capital_city.cpp:32:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(int i=0; i<V.size(); i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9872 KB |
Output is correct |
3 |
Correct |
5 ms |
9712 KB |
Output is correct |
4 |
Correct |
5 ms |
9624 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9688 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9712 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9872 KB |
Output is correct |
3 |
Correct |
5 ms |
9712 KB |
Output is correct |
4 |
Correct |
5 ms |
9624 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9688 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9712 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
14 ms |
9880 KB |
Output is correct |
12 |
Correct |
14 ms |
9812 KB |
Output is correct |
13 |
Correct |
16 ms |
9880 KB |
Output is correct |
14 |
Correct |
23 ms |
9888 KB |
Output is correct |
15 |
Incorrect |
8 ms |
9812 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
740 ms |
36192 KB |
Output is correct |
2 |
Correct |
387 ms |
37832 KB |
Output is correct |
3 |
Correct |
857 ms |
39076 KB |
Output is correct |
4 |
Correct |
380 ms |
38100 KB |
Output is correct |
5 |
Correct |
778 ms |
38908 KB |
Output is correct |
6 |
Correct |
399 ms |
38464 KB |
Output is correct |
7 |
Correct |
715 ms |
37856 KB |
Output is correct |
8 |
Correct |
365 ms |
36924 KB |
Output is correct |
9 |
Incorrect |
873 ms |
35372 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9872 KB |
Output is correct |
3 |
Correct |
5 ms |
9712 KB |
Output is correct |
4 |
Correct |
5 ms |
9624 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9688 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9712 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
14 ms |
9880 KB |
Output is correct |
12 |
Correct |
14 ms |
9812 KB |
Output is correct |
13 |
Correct |
16 ms |
9880 KB |
Output is correct |
14 |
Correct |
23 ms |
9888 KB |
Output is correct |
15 |
Incorrect |
8 ms |
9812 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |