// subtask 1 && subtask 2
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#define INF 1e9
using namespace std;
typedef long long ll;
int v[200005], p[200005];
vector<int>adj[200005], c[200005];
void dfs(int s, int e) {
for (auto u : adj[s]) {
if (u == e) continue;
p[u] = s;
dfs(u, s);
}
}
void solve()
{
int n, k; cin >> n >> k;
for (int i = 1; i <= n - 1; i++) {
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
cin >> v[i];
c[v[i]].push_back(i);
}
int ans = INF;
for (int i = 1; i <= k; i++) {
dfs(c[i][0], 0);
queue<int>q;
for (auto u : c[i]) {q.push(u); }
vector<bool>visited(k + 1, false);
visited[i] = true;
int anss = 0;
while (q.size()) {
int u = q.front(); q.pop();
if (p[u] != 0 && v[p[u]] != v[u] && !visited[v[p[u]]]) {
visited[v[p[u]]] = true;
++anss;
for (auto x : c[v[p[u]]])q.push(x);
}
}
ans = min(ans, anss);
}
cout << ans << "\n";
}
int main()
{
ios_base::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 |
3 ms |
10840 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
1 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
10684 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10840 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
1 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
10684 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10904 KB |
Output is correct |
11 |
Correct |
10 ms |
10840 KB |
Output is correct |
12 |
Correct |
9 ms |
10844 KB |
Output is correct |
13 |
Correct |
15 ms |
11016 KB |
Output is correct |
14 |
Correct |
11 ms |
11100 KB |
Output is correct |
15 |
Correct |
13 ms |
10844 KB |
Output is correct |
16 |
Correct |
13 ms |
10844 KB |
Output is correct |
17 |
Correct |
8 ms |
10844 KB |
Output is correct |
18 |
Correct |
9 ms |
11032 KB |
Output is correct |
19 |
Correct |
9 ms |
10844 KB |
Output is correct |
20 |
Correct |
9 ms |
10844 KB |
Output is correct |
21 |
Correct |
9 ms |
11040 KB |
Output is correct |
22 |
Correct |
20 ms |
10844 KB |
Output is correct |
23 |
Correct |
32 ms |
10844 KB |
Output is correct |
24 |
Correct |
17 ms |
10848 KB |
Output is correct |
25 |
Correct |
17 ms |
10844 KB |
Output is correct |
26 |
Correct |
16 ms |
10840 KB |
Output is correct |
27 |
Correct |
19 ms |
10844 KB |
Output is correct |
28 |
Correct |
19 ms |
11048 KB |
Output is correct |
29 |
Correct |
14 ms |
10844 KB |
Output is correct |
30 |
Correct |
18 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3048 ms |
25428 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10840 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
1 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
10684 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10904 KB |
Output is correct |
11 |
Correct |
10 ms |
10840 KB |
Output is correct |
12 |
Correct |
9 ms |
10844 KB |
Output is correct |
13 |
Correct |
15 ms |
11016 KB |
Output is correct |
14 |
Correct |
11 ms |
11100 KB |
Output is correct |
15 |
Correct |
13 ms |
10844 KB |
Output is correct |
16 |
Correct |
13 ms |
10844 KB |
Output is correct |
17 |
Correct |
8 ms |
10844 KB |
Output is correct |
18 |
Correct |
9 ms |
11032 KB |
Output is correct |
19 |
Correct |
9 ms |
10844 KB |
Output is correct |
20 |
Correct |
9 ms |
10844 KB |
Output is correct |
21 |
Correct |
9 ms |
11040 KB |
Output is correct |
22 |
Correct |
20 ms |
10844 KB |
Output is correct |
23 |
Correct |
32 ms |
10844 KB |
Output is correct |
24 |
Correct |
17 ms |
10848 KB |
Output is correct |
25 |
Correct |
17 ms |
10844 KB |
Output is correct |
26 |
Correct |
16 ms |
10840 KB |
Output is correct |
27 |
Correct |
19 ms |
10844 KB |
Output is correct |
28 |
Correct |
19 ms |
11048 KB |
Output is correct |
29 |
Correct |
14 ms |
10844 KB |
Output is correct |
30 |
Correct |
18 ms |
10844 KB |
Output is correct |
31 |
Execution timed out |
3048 ms |
25428 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |