/* There's someone in my head but it's not me */
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
#define SZ(x) (int) x.size()
#define F first
#define S second
const int N = 2e5 + 10, MOD = 1e9 + 7;
int ret = MOD, c, n, k, hide[N], H[N], S[N], P[N], M[N], dead[N], C[N]; vector<int> adj[N], col[N];
void preDFS(int v, int p = -1) {
S[v] = 1;
for (int u : adj[v])
if (!M[u] && u != p) preDFS(u, v), S[v] += S[u];
}
int centroid(int v, int _n, int p = -1) {
for (int u : adj[v]) {
if (!M[u] && u != p && S[u] * 2 > _n)
return centroid(u, _n, v);
}
return v;
}
void DFS(int v, int p = -1) {
H[v] = c, P[v] = (~p ? p : v);
for (int u : adj[v]) {
if (!M[u] && u != p && !dead[C[u]]) DFS(u, v);
}
}
void decompose(int v) {
preDFS(v);
v = centroid(v, S[v]);
c = C[v];
if (!dead[c]) {
DFS(v);
queue<int> Q; vector<int> hist;
Q.push(c);
int now = 0, f = 1;
while (SZ(Q)) {
int _c = Q.front(); Q.pop();
if (hide[_c]) continue;
hide[_c] = 1; hist.push_back(_c);
now++;
for (int u : col[_c]) {
if (H[u] != c || dead[_c]) f = 0;
if (C[P[u]] != C[u] && !hide[C[P[u]]]) Q.push(C[P[u]]);
}
}
while (SZ(hist)) {
int u = hist.back(); hist.pop_back();
hide[u] = 0;
}
dead[c] = 1;
if (f) ret = min(ret, now - 1);
}
M[v] = 1;
for (int u : adj[v])
if (!M[u]) decompose(u);
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i < n; i++) {
int u, v; scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
scanf("%d", &C[i]); col[C[i]].push_back(i);
}
decompose(1);
printf("%d\n", ret);
return 0;
}
Compilation message
capital_city.cpp: In function 'int main()':
capital_city.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
69 | scanf("%d%d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~
capital_city.cpp:71:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
71 | int u, v; scanf("%d%d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~
capital_city.cpp:76:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
76 | scanf("%d", &C[i]); col[C[i]].push_back(i);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9812 KB |
Output is correct |
2 |
Correct |
7 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
7 ms |
9704 KB |
Output is correct |
6 |
Correct |
7 ms |
9676 KB |
Output is correct |
7 |
Correct |
7 ms |
9676 KB |
Output is correct |
8 |
Correct |
7 ms |
9724 KB |
Output is correct |
9 |
Correct |
7 ms |
9676 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9812 KB |
Output is correct |
2 |
Correct |
7 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
7 ms |
9704 KB |
Output is correct |
6 |
Correct |
7 ms |
9676 KB |
Output is correct |
7 |
Correct |
7 ms |
9676 KB |
Output is correct |
8 |
Correct |
7 ms |
9724 KB |
Output is correct |
9 |
Correct |
7 ms |
9676 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
15 ms |
9840 KB |
Output is correct |
12 |
Correct |
15 ms |
9804 KB |
Output is correct |
13 |
Correct |
16 ms |
9804 KB |
Output is correct |
14 |
Correct |
15 ms |
9884 KB |
Output is correct |
15 |
Correct |
11 ms |
9940 KB |
Output is correct |
16 |
Correct |
13 ms |
9804 KB |
Output is correct |
17 |
Correct |
9 ms |
9932 KB |
Output is correct |
18 |
Correct |
9 ms |
9860 KB |
Output is correct |
19 |
Correct |
9 ms |
9864 KB |
Output is correct |
20 |
Correct |
9 ms |
9932 KB |
Output is correct |
21 |
Correct |
10 ms |
9932 KB |
Output is correct |
22 |
Correct |
20 ms |
9972 KB |
Output is correct |
23 |
Correct |
27 ms |
9916 KB |
Output is correct |
24 |
Correct |
20 ms |
9932 KB |
Output is correct |
25 |
Correct |
16 ms |
9944 KB |
Output is correct |
26 |
Correct |
14 ms |
9932 KB |
Output is correct |
27 |
Correct |
15 ms |
10008 KB |
Output is correct |
28 |
Correct |
12 ms |
9984 KB |
Output is correct |
29 |
Correct |
14 ms |
9912 KB |
Output is correct |
30 |
Correct |
12 ms |
9880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3078 ms |
43208 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9812 KB |
Output is correct |
2 |
Correct |
7 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
7 ms |
9704 KB |
Output is correct |
6 |
Correct |
7 ms |
9676 KB |
Output is correct |
7 |
Correct |
7 ms |
9676 KB |
Output is correct |
8 |
Correct |
7 ms |
9724 KB |
Output is correct |
9 |
Correct |
7 ms |
9676 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
15 ms |
9840 KB |
Output is correct |
12 |
Correct |
15 ms |
9804 KB |
Output is correct |
13 |
Correct |
16 ms |
9804 KB |
Output is correct |
14 |
Correct |
15 ms |
9884 KB |
Output is correct |
15 |
Correct |
11 ms |
9940 KB |
Output is correct |
16 |
Correct |
13 ms |
9804 KB |
Output is correct |
17 |
Correct |
9 ms |
9932 KB |
Output is correct |
18 |
Correct |
9 ms |
9860 KB |
Output is correct |
19 |
Correct |
9 ms |
9864 KB |
Output is correct |
20 |
Correct |
9 ms |
9932 KB |
Output is correct |
21 |
Correct |
10 ms |
9932 KB |
Output is correct |
22 |
Correct |
20 ms |
9972 KB |
Output is correct |
23 |
Correct |
27 ms |
9916 KB |
Output is correct |
24 |
Correct |
20 ms |
9932 KB |
Output is correct |
25 |
Correct |
16 ms |
9944 KB |
Output is correct |
26 |
Correct |
14 ms |
9932 KB |
Output is correct |
27 |
Correct |
15 ms |
10008 KB |
Output is correct |
28 |
Correct |
12 ms |
9984 KB |
Output is correct |
29 |
Correct |
14 ms |
9912 KB |
Output is correct |
30 |
Correct |
12 ms |
9880 KB |
Output is correct |
31 |
Execution timed out |
3078 ms |
43208 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |