/*input
*/
#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define rd() abs((int)rng())
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
const int maxn = 2e5 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, k, color[maxn], vis[maxn], par[maxn], par2[maxn], sub[maxn], color_added[maxn], color_id[maxn], color_cnt[maxn], color_total[maxn];
vector<int>byColor[maxn];
vector<int>colors;
vector<int>adj[maxn];
void dfs(int v) {
sub[v] = 1;
for (int to : adj[v]) {
if (!vis[to] && to != par[v]) {
par[to] = v;
dfs(to);
sub[v] += sub[to];
}
}
}
int get_centroid(int v) {
par[v] = 0;
dfs(v);
int siz = sub[v];
while (true) {
pii mx = {0, 0};
for (int to : adj[v]) {
if (!vis[to] && to != par[v]) {
mx = max(mx, {sub[to], to});
}
}
if (mx.x * 2 > siz) {
v = mx.y;
} else {
return v;
}
}
}
void dfs2(int v, int p) {
par2[v] = p;
if (color_id[color[v]] == -1) {
color_id[color[v]] = sz(colors);
colors.pb(color[v]);
}
for (int to : adj[v]) {
if (!vis[to] && to != p) {
dfs2(to, v);
}
}
}
void dfs3(int v, int p) {
byColor[color_id[color[v]]].pb(v);
++color_cnt[color[v]];
for (int to : adj[v]) {
if (!vis[to] && to != p) {
dfs3(to, v);
}
}
}
int ans = mod;
void solve(int v, int level = 0) {
par[v] = 0;
v = get_centroid(v);
vis[v] = 1;
// cout << "level: " << level << ", vertex: " << v << "\n";
for (int c : colors) {
if (color_id[c] != -1) {
byColor[color_id[c]].clear();
}
color_cnt[c] = 0;
color_id[c] = -1;
color_added[c] = 0;
}
colors.clear();
dfs2(v, 0);
dfs3(v, 0);
queue<int>Q;
Q.push(color[v]);
color_added[color[v]] = 1;
int total_colors = 1;
while (sz(Q)) {
int c = Q.front();
if (color_cnt[c] != color_total[c]) {
total_colors = mod;
}
Q.pop();
for (int u : byColor[color_id[c]]) {
int p = par2[u];
if (p != 0 && !color_added[color[p]]) {
++total_colors;
color_added[color[p]] = 1;
Q.push(color[p]);
}
}
}
ans = min(ans, total_colors - 1);
for (int to : adj[v]) {
if (!vis[to]) {
solve(to, level + 1);
}
}
}
int fast() {
for (int i = 1; i <= k; i++) {
color_total[i] = 0;
color_id[i] = -1;
}
for (int i = 1; i <= n; i++) {
++color_total[color[i]];
vis[i] = 0;
}
ans = mod;
solve(1);
return ans;
}
int main()
{
ios_base::sync_with_stdio(false), cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
for (int i = 1; i <= n; i++) {
cin >> color[i];
}
cout << fast() << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9856 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
10 ms |
9856 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
11 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 ms |
9856 KB |
Output is correct |
9 |
Correct |
10 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9856 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
10 ms |
9856 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
11 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 ms |
9856 KB |
Output is correct |
9 |
Correct |
10 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
11 |
Correct |
12 ms |
9984 KB |
Output is correct |
12 |
Correct |
12 ms |
9984 KB |
Output is correct |
13 |
Correct |
12 ms |
9856 KB |
Output is correct |
14 |
Correct |
12 ms |
9856 KB |
Output is correct |
15 |
Correct |
12 ms |
9984 KB |
Output is correct |
16 |
Correct |
12 ms |
9984 KB |
Output is correct |
17 |
Correct |
11 ms |
9984 KB |
Output is correct |
18 |
Correct |
12 ms |
9984 KB |
Output is correct |
19 |
Correct |
12 ms |
9984 KB |
Output is correct |
20 |
Correct |
11 ms |
9984 KB |
Output is correct |
21 |
Correct |
14 ms |
10112 KB |
Output is correct |
22 |
Correct |
12 ms |
9984 KB |
Output is correct |
23 |
Correct |
12 ms |
9984 KB |
Output is correct |
24 |
Correct |
12 ms |
9984 KB |
Output is correct |
25 |
Correct |
13 ms |
9984 KB |
Output is correct |
26 |
Correct |
13 ms |
9984 KB |
Output is correct |
27 |
Correct |
13 ms |
9984 KB |
Output is correct |
28 |
Correct |
13 ms |
9984 KB |
Output is correct |
29 |
Correct |
13 ms |
9984 KB |
Output is correct |
30 |
Correct |
13 ms |
9984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
895 ms |
34288 KB |
Output is correct |
2 |
Correct |
319 ms |
34416 KB |
Output is correct |
3 |
Correct |
909 ms |
33904 KB |
Output is correct |
4 |
Correct |
321 ms |
34416 KB |
Output is correct |
5 |
Correct |
896 ms |
32368 KB |
Output is correct |
6 |
Correct |
333 ms |
34416 KB |
Output is correct |
7 |
Correct |
897 ms |
32368 KB |
Output is correct |
8 |
Correct |
311 ms |
34288 KB |
Output is correct |
9 |
Correct |
1042 ms |
30324 KB |
Output is correct |
10 |
Correct |
1042 ms |
28660 KB |
Output is correct |
11 |
Correct |
1029 ms |
30580 KB |
Output is correct |
12 |
Correct |
1050 ms |
31864 KB |
Output is correct |
13 |
Correct |
1060 ms |
28404 KB |
Output is correct |
14 |
Correct |
1075 ms |
32200 KB |
Output is correct |
15 |
Correct |
1086 ms |
31988 KB |
Output is correct |
16 |
Correct |
1077 ms |
29064 KB |
Output is correct |
17 |
Correct |
1093 ms |
29352 KB |
Output is correct |
18 |
Correct |
1030 ms |
29428 KB |
Output is correct |
19 |
Correct |
1045 ms |
31220 KB |
Output is correct |
20 |
Correct |
1029 ms |
32756 KB |
Output is correct |
21 |
Correct |
10 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9856 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
10 ms |
9856 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
11 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 ms |
9856 KB |
Output is correct |
9 |
Correct |
10 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
11 |
Correct |
12 ms |
9984 KB |
Output is correct |
12 |
Correct |
12 ms |
9984 KB |
Output is correct |
13 |
Correct |
12 ms |
9856 KB |
Output is correct |
14 |
Correct |
12 ms |
9856 KB |
Output is correct |
15 |
Correct |
12 ms |
9984 KB |
Output is correct |
16 |
Correct |
12 ms |
9984 KB |
Output is correct |
17 |
Correct |
11 ms |
9984 KB |
Output is correct |
18 |
Correct |
12 ms |
9984 KB |
Output is correct |
19 |
Correct |
12 ms |
9984 KB |
Output is correct |
20 |
Correct |
11 ms |
9984 KB |
Output is correct |
21 |
Correct |
14 ms |
10112 KB |
Output is correct |
22 |
Correct |
12 ms |
9984 KB |
Output is correct |
23 |
Correct |
12 ms |
9984 KB |
Output is correct |
24 |
Correct |
12 ms |
9984 KB |
Output is correct |
25 |
Correct |
13 ms |
9984 KB |
Output is correct |
26 |
Correct |
13 ms |
9984 KB |
Output is correct |
27 |
Correct |
13 ms |
9984 KB |
Output is correct |
28 |
Correct |
13 ms |
9984 KB |
Output is correct |
29 |
Correct |
13 ms |
9984 KB |
Output is correct |
30 |
Correct |
13 ms |
9984 KB |
Output is correct |
31 |
Correct |
895 ms |
34288 KB |
Output is correct |
32 |
Correct |
319 ms |
34416 KB |
Output is correct |
33 |
Correct |
909 ms |
33904 KB |
Output is correct |
34 |
Correct |
321 ms |
34416 KB |
Output is correct |
35 |
Correct |
896 ms |
32368 KB |
Output is correct |
36 |
Correct |
333 ms |
34416 KB |
Output is correct |
37 |
Correct |
897 ms |
32368 KB |
Output is correct |
38 |
Correct |
311 ms |
34288 KB |
Output is correct |
39 |
Correct |
1042 ms |
30324 KB |
Output is correct |
40 |
Correct |
1042 ms |
28660 KB |
Output is correct |
41 |
Correct |
1029 ms |
30580 KB |
Output is correct |
42 |
Correct |
1050 ms |
31864 KB |
Output is correct |
43 |
Correct |
1060 ms |
28404 KB |
Output is correct |
44 |
Correct |
1075 ms |
32200 KB |
Output is correct |
45 |
Correct |
1086 ms |
31988 KB |
Output is correct |
46 |
Correct |
1077 ms |
29064 KB |
Output is correct |
47 |
Correct |
1093 ms |
29352 KB |
Output is correct |
48 |
Correct |
1030 ms |
29428 KB |
Output is correct |
49 |
Correct |
1045 ms |
31220 KB |
Output is correct |
50 |
Correct |
1029 ms |
32756 KB |
Output is correct |
51 |
Correct |
10 ms |
9728 KB |
Output is correct |
52 |
Correct |
637 ms |
22264 KB |
Output is correct |
53 |
Correct |
723 ms |
22196 KB |
Output is correct |
54 |
Correct |
680 ms |
22136 KB |
Output is correct |
55 |
Correct |
688 ms |
22136 KB |
Output is correct |
56 |
Correct |
704 ms |
22264 KB |
Output is correct |
57 |
Correct |
707 ms |
22136 KB |
Output is correct |
58 |
Correct |
665 ms |
25584 KB |
Output is correct |
59 |
Correct |
615 ms |
25712 KB |
Output is correct |
60 |
Correct |
843 ms |
25844 KB |
Output is correct |
61 |
Correct |
902 ms |
25588 KB |
Output is correct |
62 |
Correct |
317 ms |
34520 KB |
Output is correct |
63 |
Correct |
324 ms |
34416 KB |
Output is correct |
64 |
Correct |
334 ms |
34368 KB |
Output is correct |
65 |
Correct |
319 ms |
34412 KB |
Output is correct |
66 |
Correct |
492 ms |
28400 KB |
Output is correct |
67 |
Correct |
476 ms |
28272 KB |
Output is correct |
68 |
Correct |
504 ms |
28400 KB |
Output is correct |
69 |
Correct |
502 ms |
28528 KB |
Output is correct |
70 |
Correct |
505 ms |
28400 KB |
Output is correct |
71 |
Correct |
497 ms |
28528 KB |
Output is correct |
72 |
Correct |
484 ms |
28400 KB |
Output is correct |
73 |
Correct |
497 ms |
27760 KB |
Output is correct |
74 |
Correct |
500 ms |
28528 KB |
Output is correct |
75 |
Correct |
490 ms |
28404 KB |
Output is correct |
76 |
Correct |
811 ms |
28400 KB |
Output is correct |
77 |
Correct |
756 ms |
27376 KB |
Output is correct |
78 |
Correct |
1096 ms |
29184 KB |
Output is correct |
79 |
Correct |
1130 ms |
27972 KB |
Output is correct |
80 |
Correct |
1137 ms |
32524 KB |
Output is correct |
81 |
Correct |
1116 ms |
30360 KB |
Output is correct |
82 |
Correct |
1119 ms |
30508 KB |
Output is correct |
83 |
Correct |
1102 ms |
28380 KB |
Output is correct |
84 |
Correct |
1162 ms |
31860 KB |
Output is correct |
85 |
Correct |
1143 ms |
30964 KB |
Output is correct |
86 |
Correct |
1117 ms |
27968 KB |
Output is correct |
87 |
Correct |
1147 ms |
29172 KB |
Output is correct |
88 |
Correct |
886 ms |
29812 KB |
Output is correct |
89 |
Correct |
883 ms |
27508 KB |
Output is correct |
90 |
Correct |
858 ms |
27380 KB |
Output is correct |
91 |
Correct |
868 ms |
29088 KB |
Output is correct |
92 |
Correct |
875 ms |
27892 KB |
Output is correct |
93 |
Correct |
875 ms |
27632 KB |
Output is correct |
94 |
Correct |
868 ms |
27508 KB |
Output is correct |
95 |
Correct |
872 ms |
28404 KB |
Output is correct |
96 |
Correct |
880 ms |
27548 KB |
Output is correct |
97 |
Correct |
895 ms |
28788 KB |
Output is correct |