#include <bits/stdc++.h>
#define mp make_pair
#define F first
#define S second
#define eb emplace_back
#define iter(a) a.begin(), a.end()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(iter(a), greater<>())
using namespace std;
using pii = pair<int, int>;
int n, m;
vector<int> C;
vector<vector<int>> g;
vector<vector<int>> cv;
vector<int> pr;
void dfs(int now, int p){
pr[now] = p;
for(int i : g[now]){
if(i == p) continue;
dfs(i, now);
}
}
int solve(int s){
dfs(s, s);
vector<bool> ok(n + 1), use(m + 1);
queue<int> q;
int clr = C[s]; use[clr] = true;
for(int i : cv[clr]) q.push(i);
ok[s] = true;
while(!q.empty()){
int v = q.front();
q.pop();
while(!ok[v]){
ok[v] = true;
if(!use[C[v]]){
use[C[v]] = true;
for(int i : cv[C[v]]) q.push(i);
}
v = pr[v];
}
}
int ans = 0;
for(int i = 1; i <= m; i++) ans += use[i];
return ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
C.resize(n + 1);
g.resize(n + 1);
pr.resize(n + 1);
cv.resize(m + 1);
for(int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
g[u].eb(v);
g[v].eb(u);
}
for(int i = 1; i <= n; i++){
cin >> C[i];
cv[C[i]].eb(i);
}
int ans = n;
for(int i = 1; i <= n; i++){
ans = min(ans, solve(i) - 1);
}
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
136 ms |
480 KB |
Output is correct |
12 |
Correct |
137 ms |
480 KB |
Output is correct |
13 |
Correct |
134 ms |
484 KB |
Output is correct |
14 |
Correct |
138 ms |
588 KB |
Output is correct |
15 |
Correct |
44 ms |
468 KB |
Output is correct |
16 |
Correct |
63 ms |
456 KB |
Output is correct |
17 |
Correct |
55 ms |
468 KB |
Output is correct |
18 |
Correct |
57 ms |
536 KB |
Output is correct |
19 |
Correct |
56 ms |
540 KB |
Output is correct |
20 |
Correct |
56 ms |
536 KB |
Output is correct |
21 |
Correct |
59 ms |
536 KB |
Output is correct |
22 |
Correct |
61 ms |
608 KB |
Output is correct |
23 |
Correct |
75 ms |
600 KB |
Output is correct |
24 |
Correct |
52 ms |
536 KB |
Output is correct |
25 |
Correct |
93 ms |
596 KB |
Output is correct |
26 |
Correct |
94 ms |
596 KB |
Output is correct |
27 |
Correct |
97 ms |
596 KB |
Output is correct |
28 |
Correct |
76 ms |
468 KB |
Output is correct |
29 |
Correct |
81 ms |
564 KB |
Output is correct |
30 |
Correct |
79 ms |
560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3055 ms |
32256 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
136 ms |
480 KB |
Output is correct |
12 |
Correct |
137 ms |
480 KB |
Output is correct |
13 |
Correct |
134 ms |
484 KB |
Output is correct |
14 |
Correct |
138 ms |
588 KB |
Output is correct |
15 |
Correct |
44 ms |
468 KB |
Output is correct |
16 |
Correct |
63 ms |
456 KB |
Output is correct |
17 |
Correct |
55 ms |
468 KB |
Output is correct |
18 |
Correct |
57 ms |
536 KB |
Output is correct |
19 |
Correct |
56 ms |
540 KB |
Output is correct |
20 |
Correct |
56 ms |
536 KB |
Output is correct |
21 |
Correct |
59 ms |
536 KB |
Output is correct |
22 |
Correct |
61 ms |
608 KB |
Output is correct |
23 |
Correct |
75 ms |
600 KB |
Output is correct |
24 |
Correct |
52 ms |
536 KB |
Output is correct |
25 |
Correct |
93 ms |
596 KB |
Output is correct |
26 |
Correct |
94 ms |
596 KB |
Output is correct |
27 |
Correct |
97 ms |
596 KB |
Output is correct |
28 |
Correct |
76 ms |
468 KB |
Output is correct |
29 |
Correct |
81 ms |
564 KB |
Output is correct |
30 |
Correct |
79 ms |
560 KB |
Output is correct |
31 |
Execution timed out |
3055 ms |
32256 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |