#include <bits/stdc++.h>
using namespace std;
const int maxn = 500010;
int n, k;
int cset[maxn]; //store for the states
int mstate[maxn]; //store for the nodes
int croot[maxn];
#define pii pair<int, int>
vector<int> adj[maxn];
int findset(int u) {
if (cset[u] == u) return u;
return cset[u] = findset(cset[u]);
}
void unionset(int a, int b) {
a = findset(a);
b = findset(b);
if (a == b) return;
if (rand()%2) cset[a] = b;
else cset[b] = a;
}
int getcomp(int u) {
return findset(mstate[u]);
}
int par[20][maxn];
int dep[maxn];
int subsize[maxn];
int walk(int u, int k) {
for (int i = 0; i < 19; i++) {
if (k & (1 << i)) {
u = par[i][u];
}
}
return u;
}
int lca(int a, int b) {
// cout << a << " and " << b << endl;
if (dep[a] < dep[b]) swap(a, b);
a = walk(a, dep[a] - dep[b]);
if (a == b) return a;
for (int i = 19; i >= 0; i--) {
if (par[i][a] != par[i][b]) {
a = par[i][a];
b = par[i][b];
}
}
return par[0][a];
}
void predfs(int u, int p = -1) {
dep[u] = p == -1 ? 1 : dep[p]+1;
subsize[u] = 1;
par[0][u] = p == -1 ? 0 : p;
for (int v : adj[u]) {
if (v == p) continue;
predfs(v, u);
subsize[u] += subsize[v];
}
}
// set<pii> curin;
vector<int> myremos[maxn];
void dfs(int u, int p, set<pii>& curin) {
// cout << " doing " << u << endl;
int bigch = -1;
for (int v : adj[u]) {
if (v == p) continue;
if (bigch == -1 || subsize[v] > subsize[bigch]) {
bigch = v;
}
}
if (bigch != -1) {
dfs(bigch, u, curin);
}
for (int v : adj[u]) {
if (v == p || v == bigch) continue;
set<pii> tc;
dfs(v, u, tc);
for (pii vp : tc) curin.insert(vp);
}
if (curin.size()) {
int cv = (*(curin.begin())).second;
unionset(cv, mstate[u]);
}
curin.insert(pii(dep[croot[mstate[u]]], mstate[u]));
// cout << u << " : ";
// for (pii vp : curin) {
// cout << vp.first << "," << vp.second << " ";
// }
// cout << endl;
int cv = (*(curin.begin())).second;
for (int v : myremos[u]) {
unionset(cv, v);
curin.erase(curin.find(pii(dep[u], v)));
}
}
int deg[maxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
int u, v;
for (int i = 0; i < n-1; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= k; i++) {
cset[i] = i;
}
for (int i = 1; i <= n; i++) {
cin >> mstate[i];
}
predfs(1);
for (int i = 1; i < 20; i++) {
for (int j = 1; j <= n; j++) {
if (par[i-1][j] != 0) {
par[i][j] = par[i-1][par[i-1][j]];
}
else par[i][j] = 0;
}
}
//now we are ready to union components
for (int i= 1; i <= n; i++) {
if (croot[mstate[i]] == 0) {
croot[mstate[i]] = i;
}
else {
croot[mstate[i]] = lca(croot[mstate[i]], i);
}
}
for (int i = 1; i <= k; i++) {
myremos[croot[i]].push_back(i);
// cout << i << " :: " << croot[i] << endl;
}
for (int i = 1; i <= n; i++) {
reverse(myremos[i].begin(), myremos[i].end());
}
set<pii> tc;
dfs(1, -1, tc);
for (int i = 1; i <= n; i++) {
for (int j : adj[i]) {
if (getcomp(i) != getcomp(j)) {
deg[getcomp(i)]++;
deg[getcomp(j)]++;
}
}
}
int nc = 0;
for (int i = 1; i <= k; i++) {
assert(deg[i]%2 == 0);
deg[i] /= 2;
if (deg[i] == 1) nc++;
}
// cout << "sz: " << curin.size() << endl;
cout << (nc+1)/2 << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
24064 KB |
Output is correct |
2 |
Correct |
25 ms |
24064 KB |
Output is correct |
3 |
Correct |
23 ms |
24064 KB |
Output is correct |
4 |
Correct |
22 ms |
24064 KB |
Output is correct |
5 |
Correct |
27 ms |
23976 KB |
Output is correct |
6 |
Correct |
27 ms |
23992 KB |
Output is correct |
7 |
Correct |
25 ms |
24056 KB |
Output is correct |
8 |
Correct |
24 ms |
24060 KB |
Output is correct |
9 |
Correct |
26 ms |
24056 KB |
Output is correct |
10 |
Correct |
25 ms |
24064 KB |
Output is correct |
11 |
Correct |
27 ms |
24064 KB |
Output is correct |
12 |
Correct |
25 ms |
24056 KB |
Output is correct |
13 |
Correct |
25 ms |
24064 KB |
Output is correct |
14 |
Correct |
25 ms |
24064 KB |
Output is correct |
15 |
Correct |
25 ms |
24064 KB |
Output is correct |
16 |
Correct |
28 ms |
24064 KB |
Output is correct |
17 |
Correct |
27 ms |
23968 KB |
Output is correct |
18 |
Correct |
23 ms |
24028 KB |
Output is correct |
19 |
Correct |
28 ms |
24064 KB |
Output is correct |
20 |
Correct |
22 ms |
24064 KB |
Output is correct |
21 |
Correct |
21 ms |
24192 KB |
Output is correct |
22 |
Correct |
24 ms |
24064 KB |
Output is correct |
23 |
Correct |
24 ms |
24064 KB |
Output is correct |
24 |
Correct |
25 ms |
24064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
24064 KB |
Output is correct |
2 |
Correct |
25 ms |
24064 KB |
Output is correct |
3 |
Correct |
23 ms |
24064 KB |
Output is correct |
4 |
Correct |
22 ms |
24064 KB |
Output is correct |
5 |
Correct |
27 ms |
23976 KB |
Output is correct |
6 |
Correct |
27 ms |
23992 KB |
Output is correct |
7 |
Correct |
25 ms |
24056 KB |
Output is correct |
8 |
Correct |
24 ms |
24060 KB |
Output is correct |
9 |
Correct |
26 ms |
24056 KB |
Output is correct |
10 |
Correct |
25 ms |
24064 KB |
Output is correct |
11 |
Correct |
27 ms |
24064 KB |
Output is correct |
12 |
Correct |
25 ms |
24056 KB |
Output is correct |
13 |
Correct |
25 ms |
24064 KB |
Output is correct |
14 |
Correct |
25 ms |
24064 KB |
Output is correct |
15 |
Correct |
25 ms |
24064 KB |
Output is correct |
16 |
Correct |
28 ms |
24064 KB |
Output is correct |
17 |
Correct |
27 ms |
23968 KB |
Output is correct |
18 |
Correct |
23 ms |
24028 KB |
Output is correct |
19 |
Correct |
28 ms |
24064 KB |
Output is correct |
20 |
Correct |
22 ms |
24064 KB |
Output is correct |
21 |
Correct |
21 ms |
24192 KB |
Output is correct |
22 |
Correct |
24 ms |
24064 KB |
Output is correct |
23 |
Correct |
24 ms |
24064 KB |
Output is correct |
24 |
Correct |
25 ms |
24064 KB |
Output is correct |
25 |
Correct |
23 ms |
23988 KB |
Output is correct |
26 |
Correct |
26 ms |
24576 KB |
Output is correct |
27 |
Correct |
24 ms |
24440 KB |
Output is correct |
28 |
Correct |
30 ms |
24824 KB |
Output is correct |
29 |
Correct |
31 ms |
24568 KB |
Output is correct |
30 |
Correct |
29 ms |
24448 KB |
Output is correct |
31 |
Correct |
23 ms |
24064 KB |
Output is correct |
32 |
Correct |
30 ms |
24960 KB |
Output is correct |
33 |
Correct |
22 ms |
24056 KB |
Output is correct |
34 |
Correct |
27 ms |
24448 KB |
Output is correct |
35 |
Correct |
24 ms |
24576 KB |
Output is correct |
36 |
Correct |
29 ms |
24448 KB |
Output is correct |
37 |
Correct |
25 ms |
24576 KB |
Output is correct |
38 |
Correct |
21 ms |
24064 KB |
Output is correct |
39 |
Correct |
26 ms |
24576 KB |
Output is correct |
40 |
Correct |
29 ms |
24440 KB |
Output is correct |
41 |
Correct |
31 ms |
24440 KB |
Output is correct |
42 |
Correct |
26 ms |
24448 KB |
Output is correct |
43 |
Correct |
29 ms |
24952 KB |
Output is correct |
44 |
Correct |
26 ms |
24064 KB |
Output is correct |
45 |
Correct |
33 ms |
24620 KB |
Output is correct |
46 |
Correct |
29 ms |
24448 KB |
Output is correct |
47 |
Correct |
25 ms |
24064 KB |
Output is correct |
48 |
Correct |
29 ms |
24448 KB |
Output is correct |
49 |
Correct |
29 ms |
24568 KB |
Output is correct |
50 |
Correct |
31 ms |
24824 KB |
Output is correct |
51 |
Correct |
28 ms |
24376 KB |
Output is correct |
52 |
Correct |
28 ms |
24448 KB |
Output is correct |
53 |
Correct |
31 ms |
24568 KB |
Output is correct |
54 |
Correct |
30 ms |
24448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
24064 KB |
Output is correct |
2 |
Correct |
25 ms |
24064 KB |
Output is correct |
3 |
Correct |
23 ms |
24064 KB |
Output is correct |
4 |
Correct |
22 ms |
24064 KB |
Output is correct |
5 |
Correct |
27 ms |
23976 KB |
Output is correct |
6 |
Correct |
27 ms |
23992 KB |
Output is correct |
7 |
Correct |
25 ms |
24056 KB |
Output is correct |
8 |
Correct |
24 ms |
24060 KB |
Output is correct |
9 |
Correct |
26 ms |
24056 KB |
Output is correct |
10 |
Correct |
25 ms |
24064 KB |
Output is correct |
11 |
Correct |
27 ms |
24064 KB |
Output is correct |
12 |
Correct |
25 ms |
24056 KB |
Output is correct |
13 |
Correct |
25 ms |
24064 KB |
Output is correct |
14 |
Correct |
25 ms |
24064 KB |
Output is correct |
15 |
Correct |
25 ms |
24064 KB |
Output is correct |
16 |
Correct |
28 ms |
24064 KB |
Output is correct |
17 |
Correct |
27 ms |
23968 KB |
Output is correct |
18 |
Correct |
23 ms |
24028 KB |
Output is correct |
19 |
Correct |
28 ms |
24064 KB |
Output is correct |
20 |
Correct |
22 ms |
24064 KB |
Output is correct |
21 |
Correct |
21 ms |
24192 KB |
Output is correct |
22 |
Correct |
24 ms |
24064 KB |
Output is correct |
23 |
Correct |
24 ms |
24064 KB |
Output is correct |
24 |
Correct |
25 ms |
24064 KB |
Output is correct |
25 |
Correct |
22 ms |
24064 KB |
Output is correct |
26 |
Correct |
151 ms |
37976 KB |
Output is correct |
27 |
Correct |
212 ms |
37812 KB |
Output is correct |
28 |
Correct |
27 ms |
24448 KB |
Output is correct |
29 |
Correct |
25 ms |
24060 KB |
Output is correct |
30 |
Correct |
24 ms |
24068 KB |
Output is correct |
31 |
Correct |
200 ms |
37648 KB |
Output is correct |
32 |
Correct |
30 ms |
24440 KB |
Output is correct |
33 |
Correct |
242 ms |
55084 KB |
Output is correct |
34 |
Correct |
207 ms |
37628 KB |
Output is correct |
35 |
Correct |
28 ms |
24448 KB |
Output is correct |
36 |
Correct |
220 ms |
39576 KB |
Output is correct |
37 |
Correct |
23 ms |
24320 KB |
Output is correct |
38 |
Correct |
25 ms |
24448 KB |
Output is correct |
39 |
Correct |
128 ms |
38000 KB |
Output is correct |
40 |
Correct |
27 ms |
24904 KB |
Output is correct |
41 |
Correct |
189 ms |
37596 KB |
Output is correct |
42 |
Correct |
228 ms |
43384 KB |
Output is correct |
43 |
Correct |
27 ms |
24064 KB |
Output is correct |
44 |
Correct |
263 ms |
55764 KB |
Output is correct |
45 |
Correct |
186 ms |
46776 KB |
Output is correct |
46 |
Correct |
23 ms |
24448 KB |
Output is correct |
47 |
Correct |
24 ms |
24440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
132 ms |
36616 KB |
Output is correct |
2 |
Correct |
193 ms |
40880 KB |
Output is correct |
3 |
Correct |
30 ms |
24448 KB |
Output is correct |
4 |
Correct |
24 ms |
24448 KB |
Output is correct |
5 |
Correct |
24 ms |
24064 KB |
Output is correct |
6 |
Correct |
23 ms |
24056 KB |
Output is correct |
7 |
Correct |
26 ms |
24448 KB |
Output is correct |
8 |
Correct |
249 ms |
39416 KB |
Output is correct |
9 |
Correct |
31 ms |
24440 KB |
Output is correct |
10 |
Correct |
201 ms |
37816 KB |
Output is correct |
11 |
Correct |
25 ms |
24064 KB |
Output is correct |
12 |
Correct |
255 ms |
37880 KB |
Output is correct |
13 |
Correct |
284 ms |
39728 KB |
Output is correct |
14 |
Correct |
275 ms |
42360 KB |
Output is correct |
15 |
Correct |
185 ms |
37876 KB |
Output is correct |
16 |
Correct |
29 ms |
24576 KB |
Output is correct |
17 |
Correct |
22 ms |
24056 KB |
Output is correct |
18 |
Correct |
194 ms |
42384 KB |
Output is correct |
19 |
Correct |
288 ms |
51884 KB |
Output is correct |
20 |
Correct |
29 ms |
24448 KB |
Output is correct |
21 |
Correct |
29 ms |
24064 KB |
Output is correct |
22 |
Correct |
210 ms |
41128 KB |
Output is correct |
23 |
Correct |
29 ms |
24448 KB |
Output is correct |
24 |
Correct |
213 ms |
38136 KB |
Output is correct |
25 |
Correct |
257 ms |
48760 KB |
Output is correct |
26 |
Correct |
34 ms |
24568 KB |
Output is correct |
27 |
Correct |
25 ms |
24704 KB |
Output is correct |
28 |
Correct |
32 ms |
24576 KB |
Output is correct |
29 |
Correct |
32 ms |
24440 KB |
Output is correct |
30 |
Correct |
27 ms |
24576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
24064 KB |
Output is correct |
2 |
Correct |
25 ms |
24064 KB |
Output is correct |
3 |
Correct |
23 ms |
24064 KB |
Output is correct |
4 |
Correct |
22 ms |
24064 KB |
Output is correct |
5 |
Correct |
27 ms |
23976 KB |
Output is correct |
6 |
Correct |
27 ms |
23992 KB |
Output is correct |
7 |
Correct |
25 ms |
24056 KB |
Output is correct |
8 |
Correct |
24 ms |
24060 KB |
Output is correct |
9 |
Correct |
26 ms |
24056 KB |
Output is correct |
10 |
Correct |
25 ms |
24064 KB |
Output is correct |
11 |
Correct |
27 ms |
24064 KB |
Output is correct |
12 |
Correct |
25 ms |
24056 KB |
Output is correct |
13 |
Correct |
25 ms |
24064 KB |
Output is correct |
14 |
Correct |
25 ms |
24064 KB |
Output is correct |
15 |
Correct |
25 ms |
24064 KB |
Output is correct |
16 |
Correct |
28 ms |
24064 KB |
Output is correct |
17 |
Correct |
27 ms |
23968 KB |
Output is correct |
18 |
Correct |
23 ms |
24028 KB |
Output is correct |
19 |
Correct |
28 ms |
24064 KB |
Output is correct |
20 |
Correct |
22 ms |
24064 KB |
Output is correct |
21 |
Correct |
21 ms |
24192 KB |
Output is correct |
22 |
Correct |
24 ms |
24064 KB |
Output is correct |
23 |
Correct |
24 ms |
24064 KB |
Output is correct |
24 |
Correct |
25 ms |
24064 KB |
Output is correct |
25 |
Correct |
23 ms |
23988 KB |
Output is correct |
26 |
Correct |
26 ms |
24576 KB |
Output is correct |
27 |
Correct |
24 ms |
24440 KB |
Output is correct |
28 |
Correct |
30 ms |
24824 KB |
Output is correct |
29 |
Correct |
31 ms |
24568 KB |
Output is correct |
30 |
Correct |
29 ms |
24448 KB |
Output is correct |
31 |
Correct |
23 ms |
24064 KB |
Output is correct |
32 |
Correct |
30 ms |
24960 KB |
Output is correct |
33 |
Correct |
22 ms |
24056 KB |
Output is correct |
34 |
Correct |
27 ms |
24448 KB |
Output is correct |
35 |
Correct |
24 ms |
24576 KB |
Output is correct |
36 |
Correct |
29 ms |
24448 KB |
Output is correct |
37 |
Correct |
25 ms |
24576 KB |
Output is correct |
38 |
Correct |
21 ms |
24064 KB |
Output is correct |
39 |
Correct |
26 ms |
24576 KB |
Output is correct |
40 |
Correct |
29 ms |
24440 KB |
Output is correct |
41 |
Correct |
31 ms |
24440 KB |
Output is correct |
42 |
Correct |
26 ms |
24448 KB |
Output is correct |
43 |
Correct |
29 ms |
24952 KB |
Output is correct |
44 |
Correct |
26 ms |
24064 KB |
Output is correct |
45 |
Correct |
33 ms |
24620 KB |
Output is correct |
46 |
Correct |
29 ms |
24448 KB |
Output is correct |
47 |
Correct |
25 ms |
24064 KB |
Output is correct |
48 |
Correct |
29 ms |
24448 KB |
Output is correct |
49 |
Correct |
29 ms |
24568 KB |
Output is correct |
50 |
Correct |
31 ms |
24824 KB |
Output is correct |
51 |
Correct |
28 ms |
24376 KB |
Output is correct |
52 |
Correct |
28 ms |
24448 KB |
Output is correct |
53 |
Correct |
31 ms |
24568 KB |
Output is correct |
54 |
Correct |
30 ms |
24448 KB |
Output is correct |
55 |
Correct |
22 ms |
24064 KB |
Output is correct |
56 |
Correct |
151 ms |
37976 KB |
Output is correct |
57 |
Correct |
212 ms |
37812 KB |
Output is correct |
58 |
Correct |
27 ms |
24448 KB |
Output is correct |
59 |
Correct |
25 ms |
24060 KB |
Output is correct |
60 |
Correct |
24 ms |
24068 KB |
Output is correct |
61 |
Correct |
200 ms |
37648 KB |
Output is correct |
62 |
Correct |
30 ms |
24440 KB |
Output is correct |
63 |
Correct |
242 ms |
55084 KB |
Output is correct |
64 |
Correct |
207 ms |
37628 KB |
Output is correct |
65 |
Correct |
28 ms |
24448 KB |
Output is correct |
66 |
Correct |
220 ms |
39576 KB |
Output is correct |
67 |
Correct |
23 ms |
24320 KB |
Output is correct |
68 |
Correct |
25 ms |
24448 KB |
Output is correct |
69 |
Correct |
128 ms |
38000 KB |
Output is correct |
70 |
Correct |
27 ms |
24904 KB |
Output is correct |
71 |
Correct |
189 ms |
37596 KB |
Output is correct |
72 |
Correct |
228 ms |
43384 KB |
Output is correct |
73 |
Correct |
27 ms |
24064 KB |
Output is correct |
74 |
Correct |
263 ms |
55764 KB |
Output is correct |
75 |
Correct |
186 ms |
46776 KB |
Output is correct |
76 |
Correct |
23 ms |
24448 KB |
Output is correct |
77 |
Correct |
24 ms |
24440 KB |
Output is correct |
78 |
Correct |
132 ms |
36616 KB |
Output is correct |
79 |
Correct |
193 ms |
40880 KB |
Output is correct |
80 |
Correct |
30 ms |
24448 KB |
Output is correct |
81 |
Correct |
24 ms |
24448 KB |
Output is correct |
82 |
Correct |
24 ms |
24064 KB |
Output is correct |
83 |
Correct |
23 ms |
24056 KB |
Output is correct |
84 |
Correct |
26 ms |
24448 KB |
Output is correct |
85 |
Correct |
249 ms |
39416 KB |
Output is correct |
86 |
Correct |
31 ms |
24440 KB |
Output is correct |
87 |
Correct |
201 ms |
37816 KB |
Output is correct |
88 |
Correct |
25 ms |
24064 KB |
Output is correct |
89 |
Correct |
255 ms |
37880 KB |
Output is correct |
90 |
Correct |
284 ms |
39728 KB |
Output is correct |
91 |
Correct |
275 ms |
42360 KB |
Output is correct |
92 |
Correct |
185 ms |
37876 KB |
Output is correct |
93 |
Correct |
29 ms |
24576 KB |
Output is correct |
94 |
Correct |
22 ms |
24056 KB |
Output is correct |
95 |
Correct |
194 ms |
42384 KB |
Output is correct |
96 |
Correct |
288 ms |
51884 KB |
Output is correct |
97 |
Correct |
29 ms |
24448 KB |
Output is correct |
98 |
Correct |
29 ms |
24064 KB |
Output is correct |
99 |
Correct |
210 ms |
41128 KB |
Output is correct |
100 |
Correct |
29 ms |
24448 KB |
Output is correct |
101 |
Correct |
213 ms |
38136 KB |
Output is correct |
102 |
Correct |
257 ms |
48760 KB |
Output is correct |
103 |
Correct |
34 ms |
24568 KB |
Output is correct |
104 |
Correct |
25 ms |
24704 KB |
Output is correct |
105 |
Correct |
32 ms |
24576 KB |
Output is correct |
106 |
Correct |
32 ms |
24440 KB |
Output is correct |
107 |
Correct |
27 ms |
24576 KB |
Output is correct |
108 |
Correct |
1365 ms |
108800 KB |
Output is correct |
109 |
Correct |
1324 ms |
136568 KB |
Output is correct |
110 |
Correct |
1193 ms |
163768 KB |
Output is correct |
111 |
Correct |
2065 ms |
172724 KB |
Output is correct |
112 |
Correct |
1898 ms |
149260 KB |
Output is correct |
113 |
Correct |
1242 ms |
116472 KB |
Output is correct |
114 |
Correct |
1020 ms |
92668 KB |
Output is correct |
115 |
Correct |
1010 ms |
92664 KB |
Output is correct |
116 |
Correct |
1656 ms |
96648 KB |
Output is correct |
117 |
Correct |
1890 ms |
116600 KB |
Output is correct |
118 |
Correct |
1010 ms |
92536 KB |
Output is correct |
119 |
Correct |
1739 ms |
116528 KB |
Output is correct |
120 |
Correct |
1927 ms |
150684 KB |
Output is correct |
121 |
Correct |
1627 ms |
116492 KB |
Output is correct |
122 |
Correct |
1921 ms |
110776 KB |
Output is correct |
123 |
Correct |
1257 ms |
118052 KB |
Output is correct |
124 |
Correct |
1760 ms |
115344 KB |
Output is correct |