//Challenge: Accepted
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 200005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
vector<int> adj[maxn], pos[maxn];
int c[maxn], siz[maxn];
int pa[maxn];
bool vis[maxn], col[maxn], mark[maxn], done[maxn], ins[maxn];
void dfs(int n, int par, vector<int> &vec) {
vec.push_back(n);
pa[n] = par;
siz[n] = 1;
for (int v:adj[n]) {
if (v != par && !mark[v]) {
dfs(v, n, vec);
siz[n] += siz[v];
}
}
}
int getcent(int n, int par, int tot) {
for (int v:adj[n]) {
if (v != par && !mark[v] && siz[v] * 2 >= tot) return getcent(v, n, tot);
}
return n;
}
int ans = 1<<30;
void solve(vector<int> a) {
if (a.size() == 0) return;
pary(a.begin(), a.end());
vector<int> tmp;
dfs(a[0], 0, tmp);
int root = getcent(a[0], 0, siz[a[0]]);
dfs(root, 0, tmp);
if (!done[c[root]]) {
for (int i:a) col[c[i]] = 0, vis[i] = 0, ins[i] = 1;
bool check = 1;
queue<int> que;
vis[root] = 1;
col[c[root]] = 1;
int cnt = 0;
for (int v:pos[c[root]]) {
if (!ins[v]) {
check = 0;
break;
}
que.push(v);
}
while (que.size()) {
if (!check) break;
int cur = que.front();que.pop();
while (!vis[cur]) {
vis[cur] = 1;
if (!col[c[cur]]) {
cnt++;
col[c[cur]] = 1;
for (int v:pos[c[cur]]) {
if (!ins[v]) {
check = 0;
break;
}
que.push(v);
}
}
cur = pa[cur];
}
}
if (check) ans = min(ans, cnt);
done[c[root]] = 1;
for (int i:a) ins[i] = 0;
}
mark[root] = 1;
for (int v:adj[root]) {
if (mark[v]) continue;
vector<int> rec;
dfs(v, root, rec);
solve(rec);
}
}
int main() {
io
int n, K;
cin >> n >> K;
for (int i = 0;i < n - 1;i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<int> ini;
for (int i = 1;i <= n;i++) {
cin >> c[i];
pos[c[i]].push_back(i);
ini.push_back(i);
}
solve(ini);
cout << ans << endl;
}
Compilation message
capital_city.cpp: In function 'void solve(std::vector<int>)':
capital_city.cpp:13:19: warning: statement has no effect [-Wunused-value]
13 | #define pary(...) 0
| ^
capital_city.cpp:45:2: note: in expansion of macro 'pary'
45 | pary(a.begin(), a.end());
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9720 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
8 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9716 KB |
Output is correct |
10 |
Correct |
7 ms |
9772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9720 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
8 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9716 KB |
Output is correct |
10 |
Correct |
7 ms |
9772 KB |
Output is correct |
11 |
Correct |
8 ms |
9860 KB |
Output is correct |
12 |
Correct |
9 ms |
9864 KB |
Output is correct |
13 |
Correct |
10 ms |
9868 KB |
Output is correct |
14 |
Correct |
8 ms |
9916 KB |
Output is correct |
15 |
Correct |
12 ms |
9996 KB |
Output is correct |
16 |
Correct |
11 ms |
9904 KB |
Output is correct |
17 |
Correct |
8 ms |
9940 KB |
Output is correct |
18 |
Correct |
9 ms |
9988 KB |
Output is correct |
19 |
Correct |
9 ms |
10028 KB |
Output is correct |
20 |
Correct |
9 ms |
9952 KB |
Output is correct |
21 |
Correct |
8 ms |
9940 KB |
Output is correct |
22 |
Correct |
11 ms |
10116 KB |
Output is correct |
23 |
Correct |
9 ms |
9940 KB |
Output is correct |
24 |
Correct |
11 ms |
9992 KB |
Output is correct |
25 |
Correct |
11 ms |
10048 KB |
Output is correct |
26 |
Correct |
9 ms |
10068 KB |
Output is correct |
27 |
Correct |
9 ms |
10032 KB |
Output is correct |
28 |
Correct |
9 ms |
9928 KB |
Output is correct |
29 |
Correct |
8 ms |
9940 KB |
Output is correct |
30 |
Correct |
9 ms |
9928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
888 ms |
45332 KB |
Output is correct |
2 |
Correct |
373 ms |
45504 KB |
Output is correct |
3 |
Correct |
935 ms |
44828 KB |
Output is correct |
4 |
Correct |
381 ms |
45652 KB |
Output is correct |
5 |
Correct |
930 ms |
42040 KB |
Output is correct |
6 |
Correct |
366 ms |
45704 KB |
Output is correct |
7 |
Correct |
835 ms |
42292 KB |
Output is correct |
8 |
Correct |
349 ms |
45408 KB |
Output is correct |
9 |
Correct |
932 ms |
39964 KB |
Output is correct |
10 |
Correct |
922 ms |
37760 KB |
Output is correct |
11 |
Correct |
841 ms |
40740 KB |
Output is correct |
12 |
Correct |
915 ms |
43648 KB |
Output is correct |
13 |
Correct |
918 ms |
37648 KB |
Output is correct |
14 |
Correct |
902 ms |
43612 KB |
Output is correct |
15 |
Correct |
925 ms |
43464 KB |
Output is correct |
16 |
Correct |
903 ms |
38088 KB |
Output is correct |
17 |
Correct |
884 ms |
38660 KB |
Output is correct |
18 |
Correct |
872 ms |
38876 KB |
Output is correct |
19 |
Correct |
907 ms |
42132 KB |
Output is correct |
20 |
Correct |
908 ms |
44256 KB |
Output is correct |
21 |
Correct |
7 ms |
9812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9720 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
8 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9716 KB |
Output is correct |
10 |
Correct |
7 ms |
9772 KB |
Output is correct |
11 |
Correct |
8 ms |
9860 KB |
Output is correct |
12 |
Correct |
9 ms |
9864 KB |
Output is correct |
13 |
Correct |
10 ms |
9868 KB |
Output is correct |
14 |
Correct |
8 ms |
9916 KB |
Output is correct |
15 |
Correct |
12 ms |
9996 KB |
Output is correct |
16 |
Correct |
11 ms |
9904 KB |
Output is correct |
17 |
Correct |
8 ms |
9940 KB |
Output is correct |
18 |
Correct |
9 ms |
9988 KB |
Output is correct |
19 |
Correct |
9 ms |
10028 KB |
Output is correct |
20 |
Correct |
9 ms |
9952 KB |
Output is correct |
21 |
Correct |
8 ms |
9940 KB |
Output is correct |
22 |
Correct |
11 ms |
10116 KB |
Output is correct |
23 |
Correct |
9 ms |
9940 KB |
Output is correct |
24 |
Correct |
11 ms |
9992 KB |
Output is correct |
25 |
Correct |
11 ms |
10048 KB |
Output is correct |
26 |
Correct |
9 ms |
10068 KB |
Output is correct |
27 |
Correct |
9 ms |
10032 KB |
Output is correct |
28 |
Correct |
9 ms |
9928 KB |
Output is correct |
29 |
Correct |
8 ms |
9940 KB |
Output is correct |
30 |
Correct |
9 ms |
9928 KB |
Output is correct |
31 |
Correct |
888 ms |
45332 KB |
Output is correct |
32 |
Correct |
373 ms |
45504 KB |
Output is correct |
33 |
Correct |
935 ms |
44828 KB |
Output is correct |
34 |
Correct |
381 ms |
45652 KB |
Output is correct |
35 |
Correct |
930 ms |
42040 KB |
Output is correct |
36 |
Correct |
366 ms |
45704 KB |
Output is correct |
37 |
Correct |
835 ms |
42292 KB |
Output is correct |
38 |
Correct |
349 ms |
45408 KB |
Output is correct |
39 |
Correct |
932 ms |
39964 KB |
Output is correct |
40 |
Correct |
922 ms |
37760 KB |
Output is correct |
41 |
Correct |
841 ms |
40740 KB |
Output is correct |
42 |
Correct |
915 ms |
43648 KB |
Output is correct |
43 |
Correct |
918 ms |
37648 KB |
Output is correct |
44 |
Correct |
902 ms |
43612 KB |
Output is correct |
45 |
Correct |
925 ms |
43464 KB |
Output is correct |
46 |
Correct |
903 ms |
38088 KB |
Output is correct |
47 |
Correct |
884 ms |
38660 KB |
Output is correct |
48 |
Correct |
872 ms |
38876 KB |
Output is correct |
49 |
Correct |
907 ms |
42132 KB |
Output is correct |
50 |
Correct |
908 ms |
44256 KB |
Output is correct |
51 |
Correct |
7 ms |
9812 KB |
Output is correct |
52 |
Correct |
469 ms |
27248 KB |
Output is correct |
53 |
Correct |
645 ms |
27448 KB |
Output is correct |
54 |
Correct |
576 ms |
27036 KB |
Output is correct |
55 |
Correct |
578 ms |
26960 KB |
Output is correct |
56 |
Correct |
639 ms |
27492 KB |
Output is correct |
57 |
Correct |
611 ms |
27648 KB |
Output is correct |
58 |
Correct |
639 ms |
31808 KB |
Output is correct |
59 |
Correct |
534 ms |
32200 KB |
Output is correct |
60 |
Correct |
761 ms |
31544 KB |
Output is correct |
61 |
Correct |
753 ms |
31120 KB |
Output is correct |
62 |
Correct |
294 ms |
45112 KB |
Output is correct |
63 |
Correct |
296 ms |
45156 KB |
Output is correct |
64 |
Correct |
315 ms |
45152 KB |
Output is correct |
65 |
Correct |
296 ms |
45444 KB |
Output is correct |
66 |
Correct |
501 ms |
36352 KB |
Output is correct |
67 |
Correct |
473 ms |
36448 KB |
Output is correct |
68 |
Correct |
457 ms |
36392 KB |
Output is correct |
69 |
Correct |
469 ms |
36604 KB |
Output is correct |
70 |
Correct |
474 ms |
36416 KB |
Output is correct |
71 |
Correct |
446 ms |
36596 KB |
Output is correct |
72 |
Correct |
449 ms |
36452 KB |
Output is correct |
73 |
Correct |
457 ms |
35448 KB |
Output is correct |
74 |
Correct |
433 ms |
36304 KB |
Output is correct |
75 |
Correct |
506 ms |
36524 KB |
Output is correct |
76 |
Correct |
671 ms |
35596 KB |
Output is correct |
77 |
Correct |
715 ms |
33648 KB |
Output is correct |
78 |
Correct |
884 ms |
38772 KB |
Output is correct |
79 |
Correct |
896 ms |
36596 KB |
Output is correct |
80 |
Correct |
929 ms |
43584 KB |
Output is correct |
81 |
Correct |
820 ms |
40292 KB |
Output is correct |
82 |
Correct |
912 ms |
40344 KB |
Output is correct |
83 |
Correct |
891 ms |
36584 KB |
Output is correct |
84 |
Correct |
972 ms |
42564 KB |
Output is correct |
85 |
Correct |
914 ms |
40932 KB |
Output is correct |
86 |
Correct |
898 ms |
36748 KB |
Output is correct |
87 |
Correct |
954 ms |
38412 KB |
Output is correct |
88 |
Correct |
777 ms |
39224 KB |
Output is correct |
89 |
Correct |
692 ms |
35300 KB |
Output is correct |
90 |
Correct |
715 ms |
35388 KB |
Output is correct |
91 |
Correct |
793 ms |
37404 KB |
Output is correct |
92 |
Correct |
740 ms |
35916 KB |
Output is correct |
93 |
Correct |
765 ms |
36040 KB |
Output is correct |
94 |
Correct |
753 ms |
35344 KB |
Output is correct |
95 |
Correct |
749 ms |
36748 KB |
Output is correct |
96 |
Correct |
733 ms |
35312 KB |
Output is correct |
97 |
Correct |
752 ms |
37800 KB |
Output is correct |