#include "bits/stdc++.h"
using namespace std;
#define int long long
#define double long double
const int MAXN = 5e5 + 10;
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
if(p==0) return 1 % MOD;
int r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
int inv(int b) {
return bm(b, MOD-2);
}
int fastlog(int x) {
return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
static void run_with_stack_size(void (*func)(void), size_t stsize) {
char *stack, *send;
stack = (char *)malloc(stsize);
send = stack + stsize - 16;
send = (char *)((uintptr_t)send / 16 * 16);
asm volatile(
"mov %%rsp, (%0)\n"
"mov %0, %%rsp\n"
:
: "r"(send));
func();
asm volatile("mov (%0), %%rsp\n" : : "r"(send));
free(stack);
}
int n, k;
int c[MAXN], sz[MAXN];
vector<int> tr[MAXN];
vector<int> adj[MAXN], rev[MAXN];
stack<int> ts;
bool vis[MAXN];
void dfs1(int node) {
vis[node] = 1;
for(int x: adj[node]) {
if(!vis[x]) dfs1(x);
}
ts.push(node);
}
vector<int> comp;
vector<int> cond[MAXN];
int dsu[MAXN];
int set_of(int u) {
if(dsu[u] == u) return u;
return dsu[u] = set_of(dsu[u]);
}
void union_(int u, int v) {
dsu[set_of(u)] = set_of(v);
}
void dfs2(int node) {
vis[node] = 1;
for(int x: rev[node]) {
if(!vis[x]) {
dfs2(x);
}
}
comp.push_back(node);
}
int tin[MAXN], cur;
void dfs0(int node, int prv) {
tin[node] = ++cur;
sz[node] = 1;
for(int x: tr[node]) {
if(x != prv) {
dfs0(x, node);
sz[node] += sz[x];
}
}
}
set<int> city[MAXN];
bool have(int node, int prv, int tar) {
if(tin[node] < tin[prv]) {
if(*city[tar].rbegin() >= tin[prv] + sz[prv]) return 1;
if(*city[tar].begin() < tin[prv]) return 1;
return 0;
}
else {
// [tin[node], tin[node] + sz[node])
auto it = city[tar].lower_bound(tin[node]);
if(it != city[tar].end() && (*it) < tin[node] + sz[node]) return 1;
return 0;
}
return 0;
}
void solve(int tc) {
cin >> n >> k;
vector<pair<int,int> > edges;
for(int i=1; i<n; i++) {
int u, v;
cin >> u >> v;
edges.push_back({u, v});
tr[u].push_back(v);
tr[v].push_back(u);
}
for(int i=1; i<=n; i++) {
cin >> c[i];
}
dfs0(1, -1);
for(int i=1; i<=n; i++) {
city[c[i]].insert(tin[i]);
}
for(pair<int, int> k : edges) {
if(have(k.first, k.second, c[k.second])) {
adj[c[k.second]].push_back(c[k.first]);
}
if(have(k.second, k.first, c[k.first])) {
adj[c[k.first]].push_back(c[k.second]);
}
}
for(int i=1; i<=k; i++) {
for(int x: adj[i]) {
rev[x].push_back(i);
}
}
for(int i=1; i<=k; i++) {
if(!vis[i]) {
dfs1(i);
}
}
for(int i=1; i<=k; i++) vis[i] = 0, dsu[i] = i;
while(ts.size()) {
int tp = ts.top();
ts.pop();
if(!vis[tp]) {
comp.clear();
dfs2(tp);
for(int i=1; i<comp.size(); i++) {
union_(comp[i-1], comp[i]);
}
}
}
set<pair<int,int>> newedges;
for(int i=1; i<=k; i++) {
for(int x: adj[i]) {
if(set_of(i) != set_of(x) && !newedges.count({set_of(i), set_of(x)})) {
cond[set_of(i)].push_back(set_of(x));
newedges.insert({set_of(i), set_of(x)});
}
}
}
int ans = 1e9;
int cnt[k+1];
for(int i=1; i<=k; i++) {
cnt[i] = 0;
}
for(int i=1; i<=k; i++) {
cnt[set_of(i)]++;
}
for(int i=1; i<=k; i++) {
if(set_of(i) == i && cond[i].empty()) {
ans = min(ans, cnt[i]);
}
}
cout << ans - 1 << "\n";
}
void uwu() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
int32_t main() {
#ifdef ONLINE_JUDGE
uwu();
#endif
#ifndef ONLINE_JUDGE
run_with_stack_size(uwu, 1024 * 1024 * 1024); // run with a 1 GiB stack
#endif
}
/*
g++ A.cpp -std=c++17 -O2 -o A
./A < input.txt
*/
Compilation message
capital_city.cpp: In function 'void solve(long long int)':
capital_city.cpp:138:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
138 | for(int i=1; i<comp.size(); i++) {
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
78428 KB |
Output is correct |
2 |
Correct |
17 ms |
78424 KB |
Output is correct |
3 |
Correct |
17 ms |
78424 KB |
Output is correct |
4 |
Correct |
16 ms |
78552 KB |
Output is correct |
5 |
Correct |
16 ms |
78428 KB |
Output is correct |
6 |
Correct |
16 ms |
78428 KB |
Output is correct |
7 |
Correct |
15 ms |
78428 KB |
Output is correct |
8 |
Correct |
16 ms |
78428 KB |
Output is correct |
9 |
Correct |
16 ms |
78424 KB |
Output is correct |
10 |
Correct |
17 ms |
78428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
78428 KB |
Output is correct |
2 |
Correct |
17 ms |
78424 KB |
Output is correct |
3 |
Correct |
17 ms |
78424 KB |
Output is correct |
4 |
Correct |
16 ms |
78552 KB |
Output is correct |
5 |
Correct |
16 ms |
78428 KB |
Output is correct |
6 |
Correct |
16 ms |
78428 KB |
Output is correct |
7 |
Correct |
15 ms |
78428 KB |
Output is correct |
8 |
Correct |
16 ms |
78428 KB |
Output is correct |
9 |
Correct |
16 ms |
78424 KB |
Output is correct |
10 |
Correct |
17 ms |
78428 KB |
Output is correct |
11 |
Correct |
17 ms |
78684 KB |
Output is correct |
12 |
Correct |
17 ms |
78808 KB |
Output is correct |
13 |
Correct |
17 ms |
78936 KB |
Output is correct |
14 |
Correct |
17 ms |
78684 KB |
Output is correct |
15 |
Correct |
17 ms |
78940 KB |
Output is correct |
16 |
Correct |
18 ms |
78936 KB |
Output is correct |
17 |
Correct |
18 ms |
78940 KB |
Output is correct |
18 |
Correct |
17 ms |
80984 KB |
Output is correct |
19 |
Correct |
17 ms |
78940 KB |
Output is correct |
20 |
Correct |
18 ms |
78860 KB |
Output is correct |
21 |
Correct |
18 ms |
80984 KB |
Output is correct |
22 |
Correct |
18 ms |
78936 KB |
Output is correct |
23 |
Correct |
18 ms |
78936 KB |
Output is correct |
24 |
Correct |
17 ms |
78940 KB |
Output is correct |
25 |
Correct |
17 ms |
78940 KB |
Output is correct |
26 |
Correct |
17 ms |
78940 KB |
Output is correct |
27 |
Correct |
17 ms |
78940 KB |
Output is correct |
28 |
Correct |
17 ms |
78936 KB |
Output is correct |
29 |
Correct |
17 ms |
78784 KB |
Output is correct |
30 |
Correct |
17 ms |
78788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
256 ms |
130748 KB |
Output is correct |
2 |
Correct |
128 ms |
134588 KB |
Output is correct |
3 |
Correct |
235 ms |
134096 KB |
Output is correct |
4 |
Correct |
134 ms |
133888 KB |
Output is correct |
5 |
Correct |
282 ms |
131260 KB |
Output is correct |
6 |
Correct |
139 ms |
134592 KB |
Output is correct |
7 |
Correct |
232 ms |
130680 KB |
Output is correct |
8 |
Correct |
151 ms |
132856 KB |
Output is correct |
9 |
Correct |
195 ms |
124104 KB |
Output is correct |
10 |
Correct |
211 ms |
122000 KB |
Output is correct |
11 |
Correct |
187 ms |
124204 KB |
Output is correct |
12 |
Correct |
189 ms |
124600 KB |
Output is correct |
13 |
Correct |
238 ms |
121928 KB |
Output is correct |
14 |
Correct |
190 ms |
126540 KB |
Output is correct |
15 |
Correct |
193 ms |
126136 KB |
Output is correct |
16 |
Correct |
231 ms |
121964 KB |
Output is correct |
17 |
Correct |
198 ms |
122428 KB |
Output is correct |
18 |
Correct |
199 ms |
122220 KB |
Output is correct |
19 |
Correct |
213 ms |
125036 KB |
Output is correct |
20 |
Correct |
216 ms |
127160 KB |
Output is correct |
21 |
Correct |
16 ms |
78428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
78428 KB |
Output is correct |
2 |
Correct |
17 ms |
78424 KB |
Output is correct |
3 |
Correct |
17 ms |
78424 KB |
Output is correct |
4 |
Correct |
16 ms |
78552 KB |
Output is correct |
5 |
Correct |
16 ms |
78428 KB |
Output is correct |
6 |
Correct |
16 ms |
78428 KB |
Output is correct |
7 |
Correct |
15 ms |
78428 KB |
Output is correct |
8 |
Correct |
16 ms |
78428 KB |
Output is correct |
9 |
Correct |
16 ms |
78424 KB |
Output is correct |
10 |
Correct |
17 ms |
78428 KB |
Output is correct |
11 |
Correct |
17 ms |
78684 KB |
Output is correct |
12 |
Correct |
17 ms |
78808 KB |
Output is correct |
13 |
Correct |
17 ms |
78936 KB |
Output is correct |
14 |
Correct |
17 ms |
78684 KB |
Output is correct |
15 |
Correct |
17 ms |
78940 KB |
Output is correct |
16 |
Correct |
18 ms |
78936 KB |
Output is correct |
17 |
Correct |
18 ms |
78940 KB |
Output is correct |
18 |
Correct |
17 ms |
80984 KB |
Output is correct |
19 |
Correct |
17 ms |
78940 KB |
Output is correct |
20 |
Correct |
18 ms |
78860 KB |
Output is correct |
21 |
Correct |
18 ms |
80984 KB |
Output is correct |
22 |
Correct |
18 ms |
78936 KB |
Output is correct |
23 |
Correct |
18 ms |
78936 KB |
Output is correct |
24 |
Correct |
17 ms |
78940 KB |
Output is correct |
25 |
Correct |
17 ms |
78940 KB |
Output is correct |
26 |
Correct |
17 ms |
78940 KB |
Output is correct |
27 |
Correct |
17 ms |
78940 KB |
Output is correct |
28 |
Correct |
17 ms |
78936 KB |
Output is correct |
29 |
Correct |
17 ms |
78784 KB |
Output is correct |
30 |
Correct |
17 ms |
78788 KB |
Output is correct |
31 |
Correct |
256 ms |
130748 KB |
Output is correct |
32 |
Correct |
128 ms |
134588 KB |
Output is correct |
33 |
Correct |
235 ms |
134096 KB |
Output is correct |
34 |
Correct |
134 ms |
133888 KB |
Output is correct |
35 |
Correct |
282 ms |
131260 KB |
Output is correct |
36 |
Correct |
139 ms |
134592 KB |
Output is correct |
37 |
Correct |
232 ms |
130680 KB |
Output is correct |
38 |
Correct |
151 ms |
132856 KB |
Output is correct |
39 |
Correct |
195 ms |
124104 KB |
Output is correct |
40 |
Correct |
211 ms |
122000 KB |
Output is correct |
41 |
Correct |
187 ms |
124204 KB |
Output is correct |
42 |
Correct |
189 ms |
124600 KB |
Output is correct |
43 |
Correct |
238 ms |
121928 KB |
Output is correct |
44 |
Correct |
190 ms |
126540 KB |
Output is correct |
45 |
Correct |
193 ms |
126136 KB |
Output is correct |
46 |
Correct |
231 ms |
121964 KB |
Output is correct |
47 |
Correct |
198 ms |
122428 KB |
Output is correct |
48 |
Correct |
199 ms |
122220 KB |
Output is correct |
49 |
Correct |
213 ms |
125036 KB |
Output is correct |
50 |
Correct |
216 ms |
127160 KB |
Output is correct |
51 |
Correct |
16 ms |
78428 KB |
Output is correct |
52 |
Correct |
191 ms |
112672 KB |
Output is correct |
53 |
Correct |
209 ms |
115104 KB |
Output is correct |
54 |
Correct |
197 ms |
114872 KB |
Output is correct |
55 |
Correct |
224 ms |
114880 KB |
Output is correct |
56 |
Correct |
381 ms |
108472 KB |
Output is correct |
57 |
Correct |
217 ms |
110860 KB |
Output is correct |
58 |
Correct |
303 ms |
117052 KB |
Output is correct |
59 |
Correct |
239 ms |
123116 KB |
Output is correct |
60 |
Correct |
267 ms |
125384 KB |
Output is correct |
61 |
Correct |
261 ms |
123760 KB |
Output is correct |
62 |
Correct |
143 ms |
134668 KB |
Output is correct |
63 |
Correct |
140 ms |
133840 KB |
Output is correct |
64 |
Correct |
142 ms |
133376 KB |
Output is correct |
65 |
Correct |
166 ms |
134244 KB |
Output is correct |
66 |
Correct |
200 ms |
123936 KB |
Output is correct |
67 |
Correct |
238 ms |
123832 KB |
Output is correct |
68 |
Correct |
201 ms |
126652 KB |
Output is correct |
69 |
Correct |
209 ms |
124640 KB |
Output is correct |
70 |
Correct |
194 ms |
123820 KB |
Output is correct |
71 |
Correct |
195 ms |
124088 KB |
Output is correct |
72 |
Correct |
204 ms |
123760 KB |
Output is correct |
73 |
Correct |
235 ms |
122084 KB |
Output is correct |
74 |
Correct |
251 ms |
124224 KB |
Output is correct |
75 |
Correct |
215 ms |
124448 KB |
Output is correct |
76 |
Correct |
231 ms |
127656 KB |
Output is correct |
77 |
Correct |
217 ms |
126332 KB |
Output is correct |
78 |
Correct |
222 ms |
122240 KB |
Output is correct |
79 |
Correct |
223 ms |
120352 KB |
Output is correct |
80 |
Correct |
223 ms |
126480 KB |
Output is correct |
81 |
Correct |
205 ms |
123780 KB |
Output is correct |
82 |
Correct |
211 ms |
122600 KB |
Output is correct |
83 |
Correct |
212 ms |
120764 KB |
Output is correct |
84 |
Correct |
263 ms |
126424 KB |
Output is correct |
85 |
Correct |
211 ms |
124576 KB |
Output is correct |
86 |
Correct |
220 ms |
121732 KB |
Output is correct |
87 |
Correct |
236 ms |
120604 KB |
Output is correct |
88 |
Correct |
203 ms |
124224 KB |
Output is correct |
89 |
Correct |
222 ms |
121312 KB |
Output is correct |
90 |
Correct |
220 ms |
121580 KB |
Output is correct |
91 |
Correct |
212 ms |
123844 KB |
Output is correct |
92 |
Correct |
247 ms |
122040 KB |
Output is correct |
93 |
Correct |
254 ms |
121248 KB |
Output is correct |
94 |
Correct |
203 ms |
121272 KB |
Output is correct |
95 |
Correct |
226 ms |
121588 KB |
Output is correct |
96 |
Correct |
222 ms |
122108 KB |
Output is correct |
97 |
Correct |
202 ms |
122044 KB |
Output is correct |