#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "debug.h"
#else
#define debug(...) 0
#endif
#define pb push_back
#define i2 array<int, 2>
#define ll long long
const int N = 2e5 + 4;
int n, k, c[N], sz[N], root[N], par[N];
bool done[N], in[N];
vector<int> adj[N], V[N];
queue<int> q;
void fill_sz(int v,int p = 0) {
par[v] = p;
root[v] = (p == 0 ? v : root[p]);
for (int u : adj[v]) {
if (u != p && !done[u]) {
fill_sz(u, v);
sz[v] += sz[u];
}
}
}
int find_cen(int v, int siz, int p = 0) {
for (int u : adj[v]) {
if (!done[u] && u != p && sz[u] > siz / 2) {
return find_cen(u, siz, v);
}
}
return v;
}
int dfs(int v = 1) {
fill_sz(v);
v = find_cen(v, sz[v]);
done[v] = true;
q.push(c[v]);
vector<int> save;
int ans = 0;
while (q.size()) {
int col = q.front();
q.pop();
if (in[col] == 1) {
continue;
}
in[col] = 1;
for (int u : V[col]) {
if (root[u] != v) {
ans = k;
break;
}
if (par[u] != 0) {
if (!in[c[par[u]]]) {
q.push(c[par[u]]);
}
}
}
save.pb(col);
ans++;
}
for (int col : save) {
in[col] = 0;
}
for (int u : adj[v]) {
if (!done[u]) {
ans = min(ans, dfs(u));
}
}
return ans;
}
void solve() {
cin >> n >> k;
for (int i = 1; i < n; i++) {
int v, u;
cin >> v >> u;
adj[v].pb(u);
adj[u].pb(v);
}
for (int i = 1; i <= n; i++) {
cin >> c[i];
V[c[i]].pb(i);
}
cout << dfs() - 1;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
12948 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
3 ms |
12892 KB |
Output is correct |
7 |
Correct |
3 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12944 KB |
Output is correct |
9 |
Correct |
2 ms |
12888 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
12948 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
3 ms |
12892 KB |
Output is correct |
7 |
Correct |
3 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12944 KB |
Output is correct |
9 |
Correct |
2 ms |
12888 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
12892 KB |
Output is correct |
12 |
Correct |
3 ms |
12892 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
3 ms |
12892 KB |
Output is correct |
15 |
Correct |
9 ms |
13264 KB |
Output is correct |
16 |
Correct |
5 ms |
12892 KB |
Output is correct |
17 |
Correct |
9 ms |
13208 KB |
Output is correct |
18 |
Correct |
11 ms |
13148 KB |
Output is correct |
19 |
Correct |
9 ms |
13148 KB |
Output is correct |
20 |
Correct |
12 ms |
13148 KB |
Output is correct |
21 |
Correct |
12 ms |
13148 KB |
Output is correct |
22 |
Correct |
25 ms |
15928 KB |
Output is correct |
23 |
Correct |
13 ms |
13144 KB |
Output is correct |
24 |
Correct |
9 ms |
13400 KB |
Output is correct |
25 |
Correct |
14 ms |
13148 KB |
Output is correct |
26 |
Correct |
21 ms |
13280 KB |
Output is correct |
27 |
Correct |
17 ms |
13404 KB |
Output is correct |
28 |
Correct |
12 ms |
13148 KB |
Output is correct |
29 |
Correct |
11 ms |
13172 KB |
Output is correct |
30 |
Correct |
12 ms |
13148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3016 ms |
82064 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
12948 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
3 ms |
12892 KB |
Output is correct |
7 |
Correct |
3 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12944 KB |
Output is correct |
9 |
Correct |
2 ms |
12888 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
12892 KB |
Output is correct |
12 |
Correct |
3 ms |
12892 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
3 ms |
12892 KB |
Output is correct |
15 |
Correct |
9 ms |
13264 KB |
Output is correct |
16 |
Correct |
5 ms |
12892 KB |
Output is correct |
17 |
Correct |
9 ms |
13208 KB |
Output is correct |
18 |
Correct |
11 ms |
13148 KB |
Output is correct |
19 |
Correct |
9 ms |
13148 KB |
Output is correct |
20 |
Correct |
12 ms |
13148 KB |
Output is correct |
21 |
Correct |
12 ms |
13148 KB |
Output is correct |
22 |
Correct |
25 ms |
15928 KB |
Output is correct |
23 |
Correct |
13 ms |
13144 KB |
Output is correct |
24 |
Correct |
9 ms |
13400 KB |
Output is correct |
25 |
Correct |
14 ms |
13148 KB |
Output is correct |
26 |
Correct |
21 ms |
13280 KB |
Output is correct |
27 |
Correct |
17 ms |
13404 KB |
Output is correct |
28 |
Correct |
12 ms |
13148 KB |
Output is correct |
29 |
Correct |
11 ms |
13172 KB |
Output is correct |
30 |
Correct |
12 ms |
13148 KB |
Output is correct |
31 |
Execution timed out |
3016 ms |
82064 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |