#include <bits/stdc++.h>
using namespace std;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 2e5+10, MOD = 998244353;
struct DSU {
vector<int> par, sz;
DSU(int n): par(n) {
iota(par.begin(), par.end(), 0);
sz.assign(n, 1);
}
int find_set(int v) {
return v == par[v] ? v : par[v] = find_set(par[v]);
}
bool union_sets(int a, int b) {
if ((a = find_set(a)) == (b = find_set(b))) return false;
if (sz[a] < sz[b]) swap(a, b);
par[b] = a, sz[a] += sz[b], sz[b] = 0;
return true;
}
};
int n, k, col[N], my_out[N], tree_par[N], tree_st[N], tree_en[N], et[N];
vector<int> adj[N];
int tt = 0;
void dfs(int c, int p) {
tree_par[c] = p;
et[tree_st[c] = tt++] = c;
for (int nxt : adj[c]) if (nxt != p) {
dfs(nxt, c);
}
tree_en[c] = tt-1;
}
int par[N], min_st[N], max_st[N], cnt[N];
set<int> st[N];
multiset<int> ms[N];
void merge(int a, int b) {
a = par[a], b = par[b];
if (a == b) return;
if (sz(st[a]) < sz(st[b])) swap(a, b);
for (int x : st[b]) {
ms[a].erase(x);
st[a].insert(x);
par[x] = a;
}
st[b].clear();
cnt[a] += cnt[b];
for (int x : ms[b]) {
if (!st[a].count(x)) {
ms[a].insert(x);
}
}
ms[b].clear();
min_st[a] = min(min_st[a], min_st[b]);
max_st[a] = max(max_st[a], max_st[b]);
}
int get_out(int c) {
c = par[c];
int maybe = et[min_st[c]];
int undo = -1;
if (tree_par[maybe] != -1 && tree_en[maybe] >= max_st[c]) {
undo = col[tree_par[maybe]];
auto it = ms[c].find(undo);
if (it != ms[c].end()) {
ms[c].erase(it);
} else {
undo = -1;
}
}
int ans = -1;
if (sz(ms[c])) ans = *ms[c].begin();
if (undo != -1) {
ms[c].insert(undo);
}
return ans;
}
void solve() {
cin >> n >> k;
for (int i = 0; i < n-1; i++) {
int a, b; cin >> a >> b, --a, --b;
adj[a].push_back(b), adj[b].push_back(a);
}
dfs(0, -1);
for (int i = 0; i < n; i++) cin >> col[i], --col[i];
for (int i = 0; i < k; i++) {
par[i] = i;
cnt[i] = 1;
st[i].insert(i);
}
fill(min_st, min_st + n, MOD);
fill(max_st, max_st + n, -1);
for (int i = 0; i < n; i++) {
min_st[col[i]] = min(min_st[col[i]], tree_st[i]);
max_st[col[i]] = max(max_st[col[i]], tree_st[i]);
if (tree_par[i] != -1 && col[tree_par[i]] != col[i]) {
ms[col[i]].insert(col[tree_par[i]]);
}
}
memset(my_out, -1, sizeof(my_out));
DSU d(k);
for (int root = 0; root < k; root++) {
my_out[par[root]] = get_out(root);
while (my_out[par[root]] != -1) {
if (d.union_sets(root, my_out[par[root]])) {
break;
} else {
vector<int> use{par[my_out[par[root]]]};
while (use.back() != par[root]) {
use.push_back(par[my_out[use.back()]]);
}
use.pop_back();
for (int x : use) {
merge(x, root);
}
my_out[par[root]] = get_out(root);
}
}
}
int best = MOD;
for (int i = 0; i < k; i++) {
int c = par[i];
if (my_out[c] == -1) {
best = min(best, cnt[c] - 1);
}
}
cout << best << '\n';
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int T = 1;
// cin >> T;
while (T--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
24532 KB |
Output is correct |
2 |
Correct |
12 ms |
24532 KB |
Output is correct |
3 |
Correct |
11 ms |
24532 KB |
Output is correct |
4 |
Correct |
11 ms |
24532 KB |
Output is correct |
5 |
Correct |
11 ms |
24532 KB |
Output is correct |
6 |
Correct |
12 ms |
24592 KB |
Output is correct |
7 |
Correct |
12 ms |
24588 KB |
Output is correct |
8 |
Correct |
12 ms |
24532 KB |
Output is correct |
9 |
Correct |
12 ms |
24592 KB |
Output is correct |
10 |
Correct |
11 ms |
24532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
24532 KB |
Output is correct |
2 |
Correct |
12 ms |
24532 KB |
Output is correct |
3 |
Correct |
11 ms |
24532 KB |
Output is correct |
4 |
Correct |
11 ms |
24532 KB |
Output is correct |
5 |
Correct |
11 ms |
24532 KB |
Output is correct |
6 |
Correct |
12 ms |
24592 KB |
Output is correct |
7 |
Correct |
12 ms |
24588 KB |
Output is correct |
8 |
Correct |
12 ms |
24532 KB |
Output is correct |
9 |
Correct |
12 ms |
24592 KB |
Output is correct |
10 |
Correct |
11 ms |
24532 KB |
Output is correct |
11 |
Correct |
12 ms |
24912 KB |
Output is correct |
12 |
Correct |
12 ms |
24868 KB |
Output is correct |
13 |
Correct |
12 ms |
24864 KB |
Output is correct |
14 |
Correct |
13 ms |
24856 KB |
Output is correct |
15 |
Correct |
13 ms |
24916 KB |
Output is correct |
16 |
Correct |
13 ms |
24912 KB |
Output is correct |
17 |
Correct |
12 ms |
24788 KB |
Output is correct |
18 |
Correct |
12 ms |
24788 KB |
Output is correct |
19 |
Correct |
13 ms |
24900 KB |
Output is correct |
20 |
Correct |
12 ms |
24860 KB |
Output is correct |
21 |
Correct |
12 ms |
24868 KB |
Output is correct |
22 |
Correct |
15 ms |
25036 KB |
Output is correct |
23 |
Correct |
13 ms |
24916 KB |
Output is correct |
24 |
Correct |
14 ms |
24916 KB |
Output is correct |
25 |
Correct |
13 ms |
24900 KB |
Output is correct |
26 |
Correct |
13 ms |
24900 KB |
Output is correct |
27 |
Correct |
13 ms |
24916 KB |
Output is correct |
28 |
Correct |
13 ms |
24912 KB |
Output is correct |
29 |
Correct |
12 ms |
24916 KB |
Output is correct |
30 |
Correct |
13 ms |
24924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
150 ms |
61080 KB |
Output is correct |
2 |
Correct |
87 ms |
64968 KB |
Output is correct |
3 |
Correct |
156 ms |
64676 KB |
Output is correct |
4 |
Correct |
90 ms |
64972 KB |
Output is correct |
5 |
Correct |
127 ms |
61536 KB |
Output is correct |
6 |
Correct |
87 ms |
64800 KB |
Output is correct |
7 |
Correct |
123 ms |
60852 KB |
Output is correct |
8 |
Correct |
83 ms |
62216 KB |
Output is correct |
9 |
Correct |
201 ms |
59856 KB |
Output is correct |
10 |
Correct |
203 ms |
58576 KB |
Output is correct |
11 |
Correct |
215 ms |
60156 KB |
Output is correct |
12 |
Correct |
203 ms |
61644 KB |
Output is correct |
13 |
Correct |
192 ms |
58084 KB |
Output is correct |
14 |
Correct |
219 ms |
61880 KB |
Output is correct |
15 |
Correct |
194 ms |
61336 KB |
Output is correct |
16 |
Correct |
201 ms |
58568 KB |
Output is correct |
17 |
Correct |
208 ms |
59084 KB |
Output is correct |
18 |
Correct |
204 ms |
58956 KB |
Output is correct |
19 |
Correct |
205 ms |
60760 KB |
Output is correct |
20 |
Correct |
204 ms |
62668 KB |
Output is correct |
21 |
Correct |
12 ms |
24532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
24532 KB |
Output is correct |
2 |
Correct |
12 ms |
24532 KB |
Output is correct |
3 |
Correct |
11 ms |
24532 KB |
Output is correct |
4 |
Correct |
11 ms |
24532 KB |
Output is correct |
5 |
Correct |
11 ms |
24532 KB |
Output is correct |
6 |
Correct |
12 ms |
24592 KB |
Output is correct |
7 |
Correct |
12 ms |
24588 KB |
Output is correct |
8 |
Correct |
12 ms |
24532 KB |
Output is correct |
9 |
Correct |
12 ms |
24592 KB |
Output is correct |
10 |
Correct |
11 ms |
24532 KB |
Output is correct |
11 |
Correct |
12 ms |
24912 KB |
Output is correct |
12 |
Correct |
12 ms |
24868 KB |
Output is correct |
13 |
Correct |
12 ms |
24864 KB |
Output is correct |
14 |
Correct |
13 ms |
24856 KB |
Output is correct |
15 |
Correct |
13 ms |
24916 KB |
Output is correct |
16 |
Correct |
13 ms |
24912 KB |
Output is correct |
17 |
Correct |
12 ms |
24788 KB |
Output is correct |
18 |
Correct |
12 ms |
24788 KB |
Output is correct |
19 |
Correct |
13 ms |
24900 KB |
Output is correct |
20 |
Correct |
12 ms |
24860 KB |
Output is correct |
21 |
Correct |
12 ms |
24868 KB |
Output is correct |
22 |
Correct |
15 ms |
25036 KB |
Output is correct |
23 |
Correct |
13 ms |
24916 KB |
Output is correct |
24 |
Correct |
14 ms |
24916 KB |
Output is correct |
25 |
Correct |
13 ms |
24900 KB |
Output is correct |
26 |
Correct |
13 ms |
24900 KB |
Output is correct |
27 |
Correct |
13 ms |
24916 KB |
Output is correct |
28 |
Correct |
13 ms |
24912 KB |
Output is correct |
29 |
Correct |
12 ms |
24916 KB |
Output is correct |
30 |
Correct |
13 ms |
24924 KB |
Output is correct |
31 |
Correct |
150 ms |
61080 KB |
Output is correct |
32 |
Correct |
87 ms |
64968 KB |
Output is correct |
33 |
Correct |
156 ms |
64676 KB |
Output is correct |
34 |
Correct |
90 ms |
64972 KB |
Output is correct |
35 |
Correct |
127 ms |
61536 KB |
Output is correct |
36 |
Correct |
87 ms |
64800 KB |
Output is correct |
37 |
Correct |
123 ms |
60852 KB |
Output is correct |
38 |
Correct |
83 ms |
62216 KB |
Output is correct |
39 |
Correct |
201 ms |
59856 KB |
Output is correct |
40 |
Correct |
203 ms |
58576 KB |
Output is correct |
41 |
Correct |
215 ms |
60156 KB |
Output is correct |
42 |
Correct |
203 ms |
61644 KB |
Output is correct |
43 |
Correct |
192 ms |
58084 KB |
Output is correct |
44 |
Correct |
219 ms |
61880 KB |
Output is correct |
45 |
Correct |
194 ms |
61336 KB |
Output is correct |
46 |
Correct |
201 ms |
58568 KB |
Output is correct |
47 |
Correct |
208 ms |
59084 KB |
Output is correct |
48 |
Correct |
204 ms |
58956 KB |
Output is correct |
49 |
Correct |
205 ms |
60760 KB |
Output is correct |
50 |
Correct |
204 ms |
62668 KB |
Output is correct |
51 |
Correct |
12 ms |
24532 KB |
Output is correct |
52 |
Correct |
187 ms |
50428 KB |
Output is correct |
53 |
Correct |
193 ms |
50536 KB |
Output is correct |
54 |
Correct |
144 ms |
50464 KB |
Output is correct |
55 |
Correct |
176 ms |
50488 KB |
Output is correct |
56 |
Correct |
192 ms |
50548 KB |
Output is correct |
57 |
Correct |
179 ms |
50552 KB |
Output is correct |
58 |
Correct |
137 ms |
55552 KB |
Output is correct |
59 |
Correct |
136 ms |
55956 KB |
Output is correct |
60 |
Correct |
151 ms |
56140 KB |
Output is correct |
61 |
Correct |
142 ms |
56036 KB |
Output is correct |
62 |
Correct |
85 ms |
64972 KB |
Output is correct |
63 |
Correct |
86 ms |
65036 KB |
Output is correct |
64 |
Correct |
85 ms |
62928 KB |
Output is correct |
65 |
Correct |
87 ms |
64776 KB |
Output is correct |
66 |
Correct |
138 ms |
53064 KB |
Output is correct |
67 |
Correct |
108 ms |
52804 KB |
Output is correct |
68 |
Correct |
104 ms |
52852 KB |
Output is correct |
69 |
Correct |
102 ms |
52968 KB |
Output is correct |
70 |
Correct |
105 ms |
52820 KB |
Output is correct |
71 |
Correct |
116 ms |
52872 KB |
Output is correct |
72 |
Correct |
104 ms |
52824 KB |
Output is correct |
73 |
Correct |
104 ms |
52312 KB |
Output is correct |
74 |
Correct |
100 ms |
52972 KB |
Output is correct |
75 |
Correct |
108 ms |
52880 KB |
Output is correct |
76 |
Correct |
135 ms |
59028 KB |
Output is correct |
77 |
Correct |
126 ms |
57880 KB |
Output is correct |
78 |
Correct |
213 ms |
58700 KB |
Output is correct |
79 |
Correct |
210 ms |
57848 KB |
Output is correct |
80 |
Correct |
197 ms |
62412 KB |
Output is correct |
81 |
Correct |
201 ms |
60088 KB |
Output is correct |
82 |
Correct |
216 ms |
59824 KB |
Output is correct |
83 |
Correct |
210 ms |
57996 KB |
Output is correct |
84 |
Correct |
202 ms |
61324 KB |
Output is correct |
85 |
Correct |
216 ms |
60584 KB |
Output is correct |
86 |
Correct |
200 ms |
57676 KB |
Output is correct |
87 |
Correct |
199 ms |
58956 KB |
Output is correct |
88 |
Correct |
184 ms |
59340 KB |
Output is correct |
89 |
Correct |
174 ms |
57348 KB |
Output is correct |
90 |
Correct |
157 ms |
57984 KB |
Output is correct |
91 |
Correct |
162 ms |
58760 KB |
Output is correct |
92 |
Correct |
167 ms |
57292 KB |
Output is correct |
93 |
Correct |
164 ms |
57672 KB |
Output is correct |
94 |
Correct |
167 ms |
57164 KB |
Output is correct |
95 |
Correct |
187 ms |
58288 KB |
Output is correct |
96 |
Correct |
179 ms |
56888 KB |
Output is correct |
97 |
Correct |
169 ms |
58384 KB |
Output is correct |