#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define f first
#define s second
#define endl "\n"
const int N = 2e5 + 5, mod = 1e9 + 7; //!
int t, sz[N], f[N], n, vis[N], c[N], ans, p[N], col[N];
int cnt[N];
vector<int> rem, x[N];
vector<int> V[N];
queue<int> q;
void dfs0(int u,int p) {
sz[u] = 1;
for(int i = 0; i < V[u].size(); i++) {
if(V[u][i] == p || f[V[u][i]]) continue;
dfs0(V[u][i], u);
sz[u] += sz[V[u][i]];
}
}
int find(int u, int p, int SZ) {
for(int i = 0; i < V[u].size(); i++) {
if(V[u][i] == p || f[V[u][i]]) continue;
if(sz[V[u][i]] > SZ / 2) return find(V[u][i], u, SZ);
}
return u;
}
int dfs(int u,int C) {
rem.push_back(c[u]);
x[c[u]].push_back(u);
for(int i = 0; i < V[u].size(); i++) {
if(V[u][i] == p[u] || f[V[u][i]]) continue;
p[V[u][i]] = u;
dfs(V[u][i], C);
}
}
void decompose(int U) {
dfs0(U, 0);
int C = find(U, 0, sz[U]);
dfs(C, C);
int cn = 0;
q.push(C);
while(q.size()) {
int u = q.front();
q.pop();
if(cnt[c[u]] != x[c[u]].size()) cn = n;
if(vis[u] == C) continue;
if(col[c[u]] != C) {
cn++;
for(int j = 0; j < x[c[u]].size(); j++) {
q.push(x[c[u]][j]);
}
}
col[c[u]] = C; vis[u] = C;
if(u != C) q.push(p[u]);
}
while(rem.size()) x[rem.back()].clear(), rem.pop_back();
ans = min(ans, cn);
f[C] = 1;
for(int i = 0; i < V[C].size(); i++) {
if(!f[V[C][i]]) decompose(V[C][i]);
}
}
main() {
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int k;
cin >> n >> k;
for(int i = 2; i <= n; i++) {
int u, v;
cin >> u >> v;
V[u].push_back(v);
V[v].push_back(u);
}
for(int i = 1; i <= n; i++) cin >> c[i], cnt[c[i]]++;
ans = k;
decompose(1);
cout << --ans;
}
Compilation message
capital_city.cpp: In function 'void dfs0(long long int, long long int)':
capital_city.cpp:16:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for(int i = 0; i < V[u].size(); i++) {
| ~~^~~~~~~~~~~~~
capital_city.cpp: In function 'long long int find(long long int, long long int, long long int)':
capital_city.cpp:23:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for(int i = 0; i < V[u].size(); i++) {
| ~~^~~~~~~~~~~~~
capital_city.cpp: In function 'long long int dfs(long long int, long long int)':
capital_city.cpp:32:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(int i = 0; i < V[u].size(); i++) {
| ~~^~~~~~~~~~~~~
capital_city.cpp:37:1: warning: no return statement in function returning non-void [-Wreturn-type]
37 | }
| ^
capital_city.cpp: In function 'void decompose(long long int)':
capital_city.cpp:47:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | if(cnt[c[u]] != x[c[u]].size()) cn = n;
| ~~~~~~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:51:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int j = 0; j < x[c[u]].size(); j++) {
| ~~^~~~~~~~~~~~~~~~
capital_city.cpp:61:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int i = 0; i < V[C].size(); i++) {
| ~~^~~~~~~~~~~~~
capital_city.cpp: At global scope:
capital_city.cpp:65:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
65 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
13 ms |
19540 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
13 ms |
19540 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
157 ms |
79468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
13 ms |
19540 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |