#include<bits/stdc++.h>
using namespace std;
int n, k, x, y;
int main() {
scanf("%d %d", &n, &k);
vector<int> c(n + 5), st(k + 5), ca(k + 5);
vector<vector<int>> v(n + 5, vector<int>()), v2(k + 5, vector<int>()), rev(k + 5, vector<int>());
for (int i = 1; i < n; i++) scanf("%d %d", &x, &y), v[x].emplace_back(y), v[y].emplace_back(x);
for (int i = 1; i <= n; i++) scanf("%d", &c[i]), ca[c[i]]++;
int t = 0;
vector<int> sz(n + 5), hv(n + 5), pos(n + 5), tin(n + 5), tout(n + 5);
function<void(int, int)> dfs = [&] (int cur, int par) {
tin[cur] = ++t; pos[t] = cur; sz[cur] = 1;
for (auto &e: v[cur]) {
if (par == e) continue;
dfs(e, cur); sz[cur] += sz[e];
if (sz[e] > sz[hv[cur]]) hv[cur] = e;
}
tout[cur] = t;
};
vector<int> col(n + 5), cnt(k + 5);
auto add = [&] (int idx) {
cnt[c[idx]]++;
};
auto del = [&] (int idx) {
cnt[c[idx]]--;
};
function<void(int, int, bool)> dfs2 = [&] (int cur, int par, bool ck) {
for (auto &e: v[cur]) if (par != e && hv[cur] != e) dfs2(e, cur, 1);
if (hv[cur]) dfs2(hv[cur], cur, 0); add(cur);
for (auto &e: v[cur]) if (par != e && hv[cur] != e) for (int i = tin[e]; i <= tout[e]; i++) add(pos[i]);
col[cur] = cnt[c[cur]];
if (ck) for (int i = tin[cur]; i <= tout[cur]; i++) del(pos[i]);
};
dfs(1, 0);
dfs2(1, 0, 0);
int ans = 1e9;
vector<int> indeg(k + 5), outdeg(k + 5);
function<void(int, int)> dfs3 = [&] (int cur, int par) {
if (par && c[cur] != c[par]) {
// cerr << cur << ' ' << ca[c[cur]] << ' ' << col[cur] << '\n';
if (ca[c[cur]] - col[cur]) v2[c[cur]].emplace_back(c[par]), indeg[c[par]]++, outdeg[c[cur]]++, rev[c[par]].emplace_back(c[cur]);
}
for (auto &e: v[cur]) {
if (par == e) continue;
dfs3(e, cur);
}
};
dfs3(1, 0);
queue<int> q;
for (int i = 1; i <= k; i++) if (!indeg[i]) q.emplace(i);
while (!q.empty()) {
int cur = q.front(); q.pop();
for (auto &e: v2[cur]) if (!(--indeg[e])) q.emplace(e);
}
stack<int> cst;
int csz = 0;
vector<bool> f(k + 5), f2(k + 5);
function<void(int, int)> dfs4 = [&] (int cur, int par) {
if (f[cur]) return; f[cur] = 1;
for (auto &e: v2[cur]) {
if (par == e) continue;
dfs4(e, cur);
}
cst.emplace(cur);
};
function<void(int, int)> dfs5 = [&] (int cur, int par) {
if (f2[cur]) return; f2[cur] = 1; csz++;
for (auto &e: rev[cur]) {
if (par == e) continue;
dfs5(e, cur);
}
};
for (int i = 1; i <= k; i++) if (!outdeg[i]) ans = 0;
for (int i = 1; i <= k; i++) if (!indeg[i]) f[i] = f2[i] = 1;
for (int i = 1; i <= k; i++) if (!f[i]) dfs4(i, 0);
while (!cst.empty()) {
int cur = cst.top(); cst.pop();
if (f2[cur]) continue; csz = 0;
dfs5(cur, 0);
ans = min(ans, csz - 1);
}
printf("%d", ans);
}
Compilation message
capital_city.cpp: In lambda function:
capital_city.cpp:33:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
33 | if (hv[cur]) dfs2(hv[cur], cur, 0); add(cur);
| ^~
capital_city.cpp:33:45: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
33 | if (hv[cur]) dfs2(hv[cur], cur, 0); add(cur);
| ^~~
capital_city.cpp: In lambda function:
capital_city.cpp:63:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
63 | if (f[cur]) return; f[cur] = 1;
| ^~
capital_city.cpp:63:29: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
63 | if (f[cur]) return; f[cur] = 1;
| ^
capital_city.cpp: In lambda function:
capital_city.cpp:71:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
71 | if (f2[cur]) return; f2[cur] = 1; csz++;
| ^~
capital_city.cpp:71:30: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
71 | if (f2[cur]) return; f2[cur] = 1; csz++;
| ^~
capital_city.cpp: In function 'int main()':
capital_city.cpp:82:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
82 | if (f2[cur]) continue; csz = 0;
| ^~
capital_city.cpp:82:32: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
82 | if (f2[cur]) continue; csz = 0;
| ^~~
capital_city.cpp:8:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
8 | scanf("%d %d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:11:38: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
11 | for (int i = 1; i < n; i++) scanf("%d %d", &x, &y), v[x].emplace_back(y), v[y].emplace_back(x);
| ~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:12:39: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | for (int i = 1; i <= n; i++) scanf("%d", &c[i]), ca[c[i]]++;
| ~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
856 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
704 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
856 KB |
Output is correct |
24 |
Correct |
1 ms |
856 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
860 KB |
Output is correct |
27 |
Correct |
2 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
184 ms |
57656 KB |
Output is correct |
2 |
Correct |
103 ms |
58448 KB |
Output is correct |
3 |
Correct |
188 ms |
57188 KB |
Output is correct |
4 |
Correct |
116 ms |
58328 KB |
Output is correct |
5 |
Correct |
163 ms |
51520 KB |
Output is correct |
6 |
Correct |
104 ms |
58276 KB |
Output is correct |
7 |
Correct |
179 ms |
51640 KB |
Output is correct |
8 |
Correct |
102 ms |
56148 KB |
Output is correct |
9 |
Correct |
165 ms |
46176 KB |
Output is correct |
10 |
Correct |
165 ms |
42064 KB |
Output is correct |
11 |
Correct |
185 ms |
46928 KB |
Output is correct |
12 |
Correct |
226 ms |
51072 KB |
Output is correct |
13 |
Correct |
164 ms |
41556 KB |
Output is correct |
14 |
Correct |
179 ms |
51504 KB |
Output is correct |
15 |
Correct |
172 ms |
51104 KB |
Output is correct |
16 |
Correct |
226 ms |
43056 KB |
Output is correct |
17 |
Correct |
195 ms |
44036 KB |
Output is correct |
18 |
Correct |
171 ms |
44204 KB |
Output is correct |
19 |
Correct |
176 ms |
49200 KB |
Output is correct |
20 |
Correct |
165 ms |
52812 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
856 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
704 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
856 KB |
Output is correct |
24 |
Correct |
1 ms |
856 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
860 KB |
Output is correct |
27 |
Correct |
2 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
31 |
Correct |
184 ms |
57656 KB |
Output is correct |
32 |
Correct |
103 ms |
58448 KB |
Output is correct |
33 |
Correct |
188 ms |
57188 KB |
Output is correct |
34 |
Correct |
116 ms |
58328 KB |
Output is correct |
35 |
Correct |
163 ms |
51520 KB |
Output is correct |
36 |
Correct |
104 ms |
58276 KB |
Output is correct |
37 |
Correct |
179 ms |
51640 KB |
Output is correct |
38 |
Correct |
102 ms |
56148 KB |
Output is correct |
39 |
Correct |
165 ms |
46176 KB |
Output is correct |
40 |
Correct |
165 ms |
42064 KB |
Output is correct |
41 |
Correct |
185 ms |
46928 KB |
Output is correct |
42 |
Correct |
226 ms |
51072 KB |
Output is correct |
43 |
Correct |
164 ms |
41556 KB |
Output is correct |
44 |
Correct |
179 ms |
51504 KB |
Output is correct |
45 |
Correct |
172 ms |
51104 KB |
Output is correct |
46 |
Correct |
226 ms |
43056 KB |
Output is correct |
47 |
Correct |
195 ms |
44036 KB |
Output is correct |
48 |
Correct |
171 ms |
44204 KB |
Output is correct |
49 |
Correct |
176 ms |
49200 KB |
Output is correct |
50 |
Correct |
165 ms |
52812 KB |
Output is correct |
51 |
Correct |
0 ms |
344 KB |
Output is correct |
52 |
Correct |
175 ms |
26028 KB |
Output is correct |
53 |
Correct |
163 ms |
26096 KB |
Output is correct |
54 |
Correct |
171 ms |
25936 KB |
Output is correct |
55 |
Correct |
239 ms |
25984 KB |
Output is correct |
56 |
Correct |
162 ms |
26100 KB |
Output is correct |
57 |
Correct |
199 ms |
25940 KB |
Output is correct |
58 |
Correct |
209 ms |
34364 KB |
Output is correct |
59 |
Correct |
157 ms |
35416 KB |
Output is correct |
60 |
Correct |
181 ms |
34384 KB |
Output is correct |
61 |
Correct |
193 ms |
33620 KB |
Output is correct |
62 |
Correct |
105 ms |
58336 KB |
Output is correct |
63 |
Correct |
112 ms |
58320 KB |
Output is correct |
64 |
Correct |
104 ms |
56660 KB |
Output is correct |
65 |
Correct |
111 ms |
58288 KB |
Output is correct |
66 |
Correct |
149 ms |
38856 KB |
Output is correct |
67 |
Correct |
150 ms |
38628 KB |
Output is correct |
68 |
Correct |
157 ms |
38876 KB |
Output is correct |
69 |
Correct |
129 ms |
38860 KB |
Output is correct |
70 |
Correct |
154 ms |
39060 KB |
Output is correct |
71 |
Correct |
137 ms |
38860 KB |
Output is correct |
72 |
Correct |
131 ms |
38860 KB |
Output is correct |
73 |
Correct |
134 ms |
37484 KB |
Output is correct |
74 |
Correct |
151 ms |
39012 KB |
Output is correct |
75 |
Correct |
137 ms |
38728 KB |
Output is correct |
76 |
Correct |
162 ms |
42576 KB |
Output is correct |
77 |
Correct |
175 ms |
39508 KB |
Output is correct |
78 |
Correct |
169 ms |
43856 KB |
Output is correct |
79 |
Correct |
170 ms |
40784 KB |
Output is correct |
80 |
Correct |
205 ms |
52052 KB |
Output is correct |
81 |
Correct |
164 ms |
46420 KB |
Output is correct |
82 |
Correct |
165 ms |
46676 KB |
Output is correct |
83 |
Correct |
176 ms |
41540 KB |
Output is correct |
84 |
Correct |
168 ms |
50972 KB |
Output is correct |
85 |
Correct |
162 ms |
48184 KB |
Output is correct |
86 |
Correct |
198 ms |
40696 KB |
Output is correct |
87 |
Correct |
165 ms |
43344 KB |
Output is correct |
88 |
Correct |
165 ms |
44628 KB |
Output is correct |
89 |
Correct |
167 ms |
38480 KB |
Output is correct |
90 |
Correct |
150 ms |
38056 KB |
Output is correct |
91 |
Correct |
162 ms |
41812 KB |
Output is correct |
92 |
Correct |
158 ms |
39508 KB |
Output is correct |
93 |
Correct |
178 ms |
39252 KB |
Output is correct |
94 |
Correct |
163 ms |
38264 KB |
Output is correct |
95 |
Correct |
179 ms |
40532 KB |
Output is correct |
96 |
Correct |
158 ms |
38600 KB |
Output is correct |
97 |
Correct |
152 ms |
41708 KB |
Output is correct |