//Challenge: Accepted
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 300005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
vector<int> adj[maxn];
int c[maxn], dep[maxn], f[maxn];
pii ran[maxn];
int pa[maxn];
void dfs(int n, int par, int d) {
pa[n] = par;
dep[n] = d;
for (int v:adj[n]) {
if (v != par) {
dfs(v, n, d+1);
}
}
}
vector<int> pos[maxn];
bool vis[maxn], mark[maxn];
int prv[maxn];
int main() {
io
int n, K;
cin >> n >> K;
for (int i = 0;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 >> c[i];
pos[c[i]].push_back(i);
}
int ans = n - 1;
for (int i = 1;i <= K;i++) {
for (int j = 1;j <= n;j++) vis[j] = mark[j] = 0;
queue<int> que;
dfs(pos[i][0], 0, 0);
vis[pos[i][0]] = 1;
for (int j:pos[i]) que.push(j);
mark[i] = 1;
int cnt = 0;
while (que.size()) {
int cur = que.front();que.pop();
if (vis[cur]) continue;
while (!vis[cur]) {
vis[cur] = 1;
if (!mark[c[cur]]) {
cnt++;
mark[c[cur]] = 1;
for (int v:pos[c[cur]]) que.push(v);
}
cur = pa[cur];
}
}
ans = min(ans, cnt);
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
10 ms |
14420 KB |
Output is correct |
3 |
Correct |
9 ms |
14396 KB |
Output is correct |
4 |
Correct |
11 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
9 ms |
14404 KB |
Output is correct |
7 |
Correct |
8 ms |
14428 KB |
Output is correct |
8 |
Correct |
9 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
10 ms |
14420 KB |
Output is correct |
3 |
Correct |
9 ms |
14396 KB |
Output is correct |
4 |
Correct |
11 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
9 ms |
14404 KB |
Output is correct |
7 |
Correct |
8 ms |
14428 KB |
Output is correct |
8 |
Correct |
9 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
27 ms |
14548 KB |
Output is correct |
12 |
Correct |
29 ms |
14572 KB |
Output is correct |
13 |
Correct |
29 ms |
14548 KB |
Output is correct |
14 |
Correct |
31 ms |
14576 KB |
Output is correct |
15 |
Correct |
25 ms |
14576 KB |
Output is correct |
16 |
Correct |
37 ms |
14528 KB |
Output is correct |
17 |
Correct |
23 ms |
14548 KB |
Output is correct |
18 |
Correct |
22 ms |
14576 KB |
Output is correct |
19 |
Correct |
25 ms |
14548 KB |
Output is correct |
20 |
Correct |
23 ms |
14568 KB |
Output is correct |
21 |
Correct |
24 ms |
14548 KB |
Output is correct |
22 |
Correct |
44 ms |
14728 KB |
Output is correct |
23 |
Correct |
45 ms |
14600 KB |
Output is correct |
24 |
Correct |
37 ms |
14568 KB |
Output is correct |
25 |
Correct |
35 ms |
14632 KB |
Output is correct |
26 |
Correct |
33 ms |
14548 KB |
Output is correct |
27 |
Correct |
34 ms |
14564 KB |
Output is correct |
28 |
Correct |
32 ms |
14620 KB |
Output is correct |
29 |
Correct |
35 ms |
14548 KB |
Output is correct |
30 |
Correct |
33 ms |
14548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3080 ms |
32880 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
10 ms |
14420 KB |
Output is correct |
3 |
Correct |
9 ms |
14396 KB |
Output is correct |
4 |
Correct |
11 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
9 ms |
14404 KB |
Output is correct |
7 |
Correct |
8 ms |
14428 KB |
Output is correct |
8 |
Correct |
9 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
27 ms |
14548 KB |
Output is correct |
12 |
Correct |
29 ms |
14572 KB |
Output is correct |
13 |
Correct |
29 ms |
14548 KB |
Output is correct |
14 |
Correct |
31 ms |
14576 KB |
Output is correct |
15 |
Correct |
25 ms |
14576 KB |
Output is correct |
16 |
Correct |
37 ms |
14528 KB |
Output is correct |
17 |
Correct |
23 ms |
14548 KB |
Output is correct |
18 |
Correct |
22 ms |
14576 KB |
Output is correct |
19 |
Correct |
25 ms |
14548 KB |
Output is correct |
20 |
Correct |
23 ms |
14568 KB |
Output is correct |
21 |
Correct |
24 ms |
14548 KB |
Output is correct |
22 |
Correct |
44 ms |
14728 KB |
Output is correct |
23 |
Correct |
45 ms |
14600 KB |
Output is correct |
24 |
Correct |
37 ms |
14568 KB |
Output is correct |
25 |
Correct |
35 ms |
14632 KB |
Output is correct |
26 |
Correct |
33 ms |
14548 KB |
Output is correct |
27 |
Correct |
34 ms |
14564 KB |
Output is correct |
28 |
Correct |
32 ms |
14620 KB |
Output is correct |
29 |
Correct |
35 ms |
14548 KB |
Output is correct |
30 |
Correct |
33 ms |
14548 KB |
Output is correct |
31 |
Execution timed out |
3080 ms |
32880 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |