//Then
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
mt19937_64 Rand(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 5e5 + 1;
//const int Mod = 1e9 + 7;
//const int inf =
int n, k;
int id[maxN];
int lab[maxN];
vector <int> S[maxN];
int sum[maxN];
pii e[maxN];
vector <int> adj[maxN], adj1[maxN];
int depth[maxN], lift[maxN][20];
int find_set(int u){
return lab[u] < 0 ? u : lab[u] = find_set(lab[u]);
}
bool unite(int u, int v){
u = find_set(u);
v = find_set(v);
if (u == v) return 0;
lab[u] += lab[v];
lab[v] = u;
return 1;
}
inline int lca(int u, int v){
if (depth[u] < depth[v]) swap(u, v);
int k = depth[u] - depth[v];
while (k){
int t = __lg(k);
u = lift[u][t];
k ^= 1 << t;
}
if (u == v) return u;
for (int i = 19; i >= 0; --i){
if (lift[u][i] == lift[v][i]) continue;
u = lift[u][i];
v = lift[v][i];
}
return lift[u][0];
}
void dfs(int u, int p){
depth[u] = depth[p] + 1;
lift[u][0] = p;
for (int i = 1; i < 20; ++i) lift[u][i] = lift[lift[u][i - 1]][i - 1];
for (int i: adj[u]){
if (i == p) continue;
dfs(i, u);
}
}
void dfs1(int u, int p){
for (int i: adj[u]){
if (i == p) continue;
dfs1(i, u);
sum[u] += sum[i];
}
if (sum[u] < 0){
unite(id[u], id[p]);
}
}
int leaf;
void dfs2(int u, int p){
if (adj1[u].size() == 1) ++leaf;
for (int i: adj1[u]){
if (i == p) continue;
dfs2(i, u);
}
}
void Init(){
cin >> n >> k;
for (int i = 1; i < n; ++i){
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
e[i] = {u, v};
}
for (int i = 1; i <= n; ++i){
cin >> id[i];
S[id[i]].pb(i);
}
fill(lab + 1, lab + n + 1, -1);
dfs(1, 0);
for (int i = 1; i <= k; ++i){
int root = S[i][0];
for (int j = 1; j < signed(S[i].size()); ++j){
root = lca(root, S[i][j]);
}
for (int j: S[i]){
--sum[j];
}
sum[root] += S[i].size();
}
dfs1(1, 0);
for (int i = 1; i < n; ++i){
int u = find_set(id[e[i].fi]);
int v = find_set(id[e[i].se]);
if (u == v) continue;
adj1[u].pb(v);
adj1[v].pb(u);
}
for (int i = 1; i <= k; ++i){
if (adj1[i].size()){
dfs2(i, 0);
break;
}
}
cout << (leaf + 1) / 2;
}
#define debug
#define taskname "test"
signed main(){
faster
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
int tt = 1;
//cin >> tt;
while (tt--){
Init();
}
if (fopen("timeout.txt", "r")){
ofstream timeout("timeout.txt");
timeout << signed(double(clock()) / CLOCKS_PER_SEC * 1000);
timeout.close();
#ifndef debug
cerr << "Time elapsed: " << signed(double(clock()) / CLOCKS_PER_SEC * 1000) << "ms\n";
#endif // debug
}
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:131:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
131 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:132:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
132 | freopen(taskname".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
35540 KB |
Output is correct |
2 |
Correct |
19 ms |
35540 KB |
Output is correct |
3 |
Correct |
21 ms |
35540 KB |
Output is correct |
4 |
Correct |
19 ms |
35540 KB |
Output is correct |
5 |
Correct |
19 ms |
35504 KB |
Output is correct |
6 |
Correct |
18 ms |
35572 KB |
Output is correct |
7 |
Correct |
29 ms |
35540 KB |
Output is correct |
8 |
Correct |
18 ms |
35520 KB |
Output is correct |
9 |
Correct |
19 ms |
35588 KB |
Output is correct |
10 |
Correct |
18 ms |
35484 KB |
Output is correct |
11 |
Correct |
17 ms |
35540 KB |
Output is correct |
12 |
Correct |
17 ms |
35540 KB |
Output is correct |
13 |
Correct |
17 ms |
35540 KB |
Output is correct |
14 |
Correct |
18 ms |
35572 KB |
Output is correct |
15 |
Correct |
19 ms |
35540 KB |
Output is correct |
16 |
Correct |
20 ms |
35540 KB |
Output is correct |
17 |
Correct |
19 ms |
35540 KB |
Output is correct |
18 |
Correct |
17 ms |
35540 KB |
Output is correct |
19 |
Correct |
17 ms |
35540 KB |
Output is correct |
20 |
Correct |
18 ms |
35572 KB |
Output is correct |
21 |
Correct |
17 ms |
35540 KB |
Output is correct |
22 |
Correct |
18 ms |
35572 KB |
Output is correct |
23 |
Correct |
18 ms |
35532 KB |
Output is correct |
24 |
Correct |
26 ms |
35572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
35540 KB |
Output is correct |
2 |
Correct |
19 ms |
35540 KB |
Output is correct |
3 |
Correct |
21 ms |
35540 KB |
Output is correct |
4 |
Correct |
19 ms |
35540 KB |
Output is correct |
5 |
Correct |
19 ms |
35504 KB |
Output is correct |
6 |
Correct |
18 ms |
35572 KB |
Output is correct |
7 |
Correct |
29 ms |
35540 KB |
Output is correct |
8 |
Correct |
18 ms |
35520 KB |
Output is correct |
9 |
Correct |
19 ms |
35588 KB |
Output is correct |
10 |
Correct |
18 ms |
35484 KB |
Output is correct |
11 |
Correct |
17 ms |
35540 KB |
Output is correct |
12 |
Correct |
17 ms |
35540 KB |
Output is correct |
13 |
Correct |
17 ms |
35540 KB |
Output is correct |
14 |
Correct |
18 ms |
35572 KB |
Output is correct |
15 |
Correct |
19 ms |
35540 KB |
Output is correct |
16 |
Correct |
20 ms |
35540 KB |
Output is correct |
17 |
Correct |
19 ms |
35540 KB |
Output is correct |
18 |
Correct |
17 ms |
35540 KB |
Output is correct |
19 |
Correct |
17 ms |
35540 KB |
Output is correct |
20 |
Correct |
18 ms |
35572 KB |
Output is correct |
21 |
Correct |
17 ms |
35540 KB |
Output is correct |
22 |
Correct |
18 ms |
35572 KB |
Output is correct |
23 |
Correct |
18 ms |
35532 KB |
Output is correct |
24 |
Correct |
26 ms |
35572 KB |
Output is correct |
25 |
Correct |
18 ms |
35604 KB |
Output is correct |
26 |
Correct |
21 ms |
36052 KB |
Output is correct |
27 |
Correct |
23 ms |
35968 KB |
Output is correct |
28 |
Correct |
74 ms |
36244 KB |
Output is correct |
29 |
Correct |
20 ms |
36144 KB |
Output is correct |
30 |
Correct |
19 ms |
35924 KB |
Output is correct |
31 |
Correct |
17 ms |
35492 KB |
Output is correct |
32 |
Correct |
26 ms |
36236 KB |
Output is correct |
33 |
Correct |
23 ms |
35516 KB |
Output is correct |
34 |
Correct |
21 ms |
35960 KB |
Output is correct |
35 |
Correct |
22 ms |
36180 KB |
Output is correct |
36 |
Correct |
22 ms |
35924 KB |
Output is correct |
37 |
Correct |
20 ms |
36052 KB |
Output is correct |
38 |
Correct |
41 ms |
35532 KB |
Output is correct |
39 |
Correct |
18 ms |
36052 KB |
Output is correct |
40 |
Correct |
18 ms |
35932 KB |
Output is correct |
41 |
Correct |
18 ms |
35924 KB |
Output is correct |
42 |
Correct |
19 ms |
36052 KB |
Output is correct |
43 |
Correct |
18 ms |
36180 KB |
Output is correct |
44 |
Correct |
17 ms |
35512 KB |
Output is correct |
45 |
Correct |
24 ms |
36052 KB |
Output is correct |
46 |
Correct |
20 ms |
36060 KB |
Output is correct |
47 |
Correct |
18 ms |
35552 KB |
Output is correct |
48 |
Correct |
20 ms |
35924 KB |
Output is correct |
49 |
Correct |
19 ms |
36108 KB |
Output is correct |
50 |
Correct |
18 ms |
36196 KB |
Output is correct |
51 |
Correct |
19 ms |
35904 KB |
Output is correct |
52 |
Correct |
22 ms |
35924 KB |
Output is correct |
53 |
Correct |
22 ms |
36024 KB |
Output is correct |
54 |
Correct |
23 ms |
35992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
35540 KB |
Output is correct |
2 |
Correct |
19 ms |
35540 KB |
Output is correct |
3 |
Correct |
21 ms |
35540 KB |
Output is correct |
4 |
Correct |
19 ms |
35540 KB |
Output is correct |
5 |
Correct |
19 ms |
35504 KB |
Output is correct |
6 |
Correct |
18 ms |
35572 KB |
Output is correct |
7 |
Correct |
29 ms |
35540 KB |
Output is correct |
8 |
Correct |
18 ms |
35520 KB |
Output is correct |
9 |
Correct |
19 ms |
35588 KB |
Output is correct |
10 |
Correct |
18 ms |
35484 KB |
Output is correct |
11 |
Correct |
17 ms |
35540 KB |
Output is correct |
12 |
Correct |
17 ms |
35540 KB |
Output is correct |
13 |
Correct |
17 ms |
35540 KB |
Output is correct |
14 |
Correct |
18 ms |
35572 KB |
Output is correct |
15 |
Correct |
19 ms |
35540 KB |
Output is correct |
16 |
Correct |
20 ms |
35540 KB |
Output is correct |
17 |
Correct |
19 ms |
35540 KB |
Output is correct |
18 |
Correct |
17 ms |
35540 KB |
Output is correct |
19 |
Correct |
17 ms |
35540 KB |
Output is correct |
20 |
Correct |
18 ms |
35572 KB |
Output is correct |
21 |
Correct |
17 ms |
35540 KB |
Output is correct |
22 |
Correct |
18 ms |
35572 KB |
Output is correct |
23 |
Correct |
18 ms |
35532 KB |
Output is correct |
24 |
Correct |
26 ms |
35572 KB |
Output is correct |
25 |
Correct |
17 ms |
35540 KB |
Output is correct |
26 |
Correct |
92 ms |
49764 KB |
Output is correct |
27 |
Correct |
116 ms |
49456 KB |
Output is correct |
28 |
Correct |
26 ms |
35924 KB |
Output is correct |
29 |
Correct |
17 ms |
35540 KB |
Output is correct |
30 |
Correct |
17 ms |
35548 KB |
Output is correct |
31 |
Correct |
109 ms |
49460 KB |
Output is correct |
32 |
Correct |
21 ms |
35924 KB |
Output is correct |
33 |
Correct |
131 ms |
56636 KB |
Output is correct |
34 |
Correct |
122 ms |
49688 KB |
Output is correct |
35 |
Correct |
20 ms |
35924 KB |
Output is correct |
36 |
Correct |
152 ms |
50216 KB |
Output is correct |
37 |
Correct |
39 ms |
35908 KB |
Output is correct |
38 |
Correct |
20 ms |
36012 KB |
Output is correct |
39 |
Correct |
79 ms |
49832 KB |
Output is correct |
40 |
Correct |
20 ms |
36144 KB |
Output is correct |
41 |
Correct |
93 ms |
49512 KB |
Output is correct |
42 |
Correct |
154 ms |
51876 KB |
Output is correct |
43 |
Correct |
19 ms |
35540 KB |
Output is correct |
44 |
Correct |
124 ms |
56980 KB |
Output is correct |
45 |
Correct |
125 ms |
53176 KB |
Output is correct |
46 |
Correct |
19 ms |
35924 KB |
Output is correct |
47 |
Correct |
20 ms |
35944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
49856 KB |
Output is correct |
2 |
Correct |
109 ms |
55860 KB |
Output is correct |
3 |
Correct |
22 ms |
36052 KB |
Output is correct |
4 |
Correct |
20 ms |
35964 KB |
Output is correct |
5 |
Correct |
18 ms |
35564 KB |
Output is correct |
6 |
Correct |
18 ms |
35540 KB |
Output is correct |
7 |
Correct |
19 ms |
35952 KB |
Output is correct |
8 |
Correct |
126 ms |
50952 KB |
Output is correct |
9 |
Correct |
20 ms |
36016 KB |
Output is correct |
10 |
Correct |
95 ms |
49552 KB |
Output is correct |
11 |
Correct |
20 ms |
35520 KB |
Output is correct |
12 |
Correct |
130 ms |
49612 KB |
Output is correct |
13 |
Correct |
112 ms |
51124 KB |
Output is correct |
14 |
Correct |
139 ms |
55244 KB |
Output is correct |
15 |
Correct |
80 ms |
49740 KB |
Output is correct |
16 |
Correct |
20 ms |
36052 KB |
Output is correct |
17 |
Correct |
18 ms |
35608 KB |
Output is correct |
18 |
Correct |
96 ms |
54908 KB |
Output is correct |
19 |
Correct |
158 ms |
59420 KB |
Output is correct |
20 |
Correct |
18 ms |
36024 KB |
Output is correct |
21 |
Correct |
30 ms |
35540 KB |
Output is correct |
22 |
Correct |
101 ms |
51536 KB |
Output is correct |
23 |
Correct |
21 ms |
35928 KB |
Output is correct |
24 |
Correct |
138 ms |
49940 KB |
Output is correct |
25 |
Correct |
135 ms |
57832 KB |
Output is correct |
26 |
Correct |
19 ms |
36212 KB |
Output is correct |
27 |
Correct |
20 ms |
36184 KB |
Output is correct |
28 |
Correct |
19 ms |
35924 KB |
Output is correct |
29 |
Correct |
19 ms |
35924 KB |
Output is correct |
30 |
Correct |
19 ms |
36052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
35540 KB |
Output is correct |
2 |
Correct |
19 ms |
35540 KB |
Output is correct |
3 |
Correct |
21 ms |
35540 KB |
Output is correct |
4 |
Correct |
19 ms |
35540 KB |
Output is correct |
5 |
Correct |
19 ms |
35504 KB |
Output is correct |
6 |
Correct |
18 ms |
35572 KB |
Output is correct |
7 |
Correct |
29 ms |
35540 KB |
Output is correct |
8 |
Correct |
18 ms |
35520 KB |
Output is correct |
9 |
Correct |
19 ms |
35588 KB |
Output is correct |
10 |
Correct |
18 ms |
35484 KB |
Output is correct |
11 |
Correct |
17 ms |
35540 KB |
Output is correct |
12 |
Correct |
17 ms |
35540 KB |
Output is correct |
13 |
Correct |
17 ms |
35540 KB |
Output is correct |
14 |
Correct |
18 ms |
35572 KB |
Output is correct |
15 |
Correct |
19 ms |
35540 KB |
Output is correct |
16 |
Correct |
20 ms |
35540 KB |
Output is correct |
17 |
Correct |
19 ms |
35540 KB |
Output is correct |
18 |
Correct |
17 ms |
35540 KB |
Output is correct |
19 |
Correct |
17 ms |
35540 KB |
Output is correct |
20 |
Correct |
18 ms |
35572 KB |
Output is correct |
21 |
Correct |
17 ms |
35540 KB |
Output is correct |
22 |
Correct |
18 ms |
35572 KB |
Output is correct |
23 |
Correct |
18 ms |
35532 KB |
Output is correct |
24 |
Correct |
26 ms |
35572 KB |
Output is correct |
25 |
Correct |
18 ms |
35604 KB |
Output is correct |
26 |
Correct |
21 ms |
36052 KB |
Output is correct |
27 |
Correct |
23 ms |
35968 KB |
Output is correct |
28 |
Correct |
74 ms |
36244 KB |
Output is correct |
29 |
Correct |
20 ms |
36144 KB |
Output is correct |
30 |
Correct |
19 ms |
35924 KB |
Output is correct |
31 |
Correct |
17 ms |
35492 KB |
Output is correct |
32 |
Correct |
26 ms |
36236 KB |
Output is correct |
33 |
Correct |
23 ms |
35516 KB |
Output is correct |
34 |
Correct |
21 ms |
35960 KB |
Output is correct |
35 |
Correct |
22 ms |
36180 KB |
Output is correct |
36 |
Correct |
22 ms |
35924 KB |
Output is correct |
37 |
Correct |
20 ms |
36052 KB |
Output is correct |
38 |
Correct |
41 ms |
35532 KB |
Output is correct |
39 |
Correct |
18 ms |
36052 KB |
Output is correct |
40 |
Correct |
18 ms |
35932 KB |
Output is correct |
41 |
Correct |
18 ms |
35924 KB |
Output is correct |
42 |
Correct |
19 ms |
36052 KB |
Output is correct |
43 |
Correct |
18 ms |
36180 KB |
Output is correct |
44 |
Correct |
17 ms |
35512 KB |
Output is correct |
45 |
Correct |
24 ms |
36052 KB |
Output is correct |
46 |
Correct |
20 ms |
36060 KB |
Output is correct |
47 |
Correct |
18 ms |
35552 KB |
Output is correct |
48 |
Correct |
20 ms |
35924 KB |
Output is correct |
49 |
Correct |
19 ms |
36108 KB |
Output is correct |
50 |
Correct |
18 ms |
36196 KB |
Output is correct |
51 |
Correct |
19 ms |
35904 KB |
Output is correct |
52 |
Correct |
22 ms |
35924 KB |
Output is correct |
53 |
Correct |
22 ms |
36024 KB |
Output is correct |
54 |
Correct |
23 ms |
35992 KB |
Output is correct |
55 |
Correct |
17 ms |
35540 KB |
Output is correct |
56 |
Correct |
92 ms |
49764 KB |
Output is correct |
57 |
Correct |
116 ms |
49456 KB |
Output is correct |
58 |
Correct |
26 ms |
35924 KB |
Output is correct |
59 |
Correct |
17 ms |
35540 KB |
Output is correct |
60 |
Correct |
17 ms |
35548 KB |
Output is correct |
61 |
Correct |
109 ms |
49460 KB |
Output is correct |
62 |
Correct |
21 ms |
35924 KB |
Output is correct |
63 |
Correct |
131 ms |
56636 KB |
Output is correct |
64 |
Correct |
122 ms |
49688 KB |
Output is correct |
65 |
Correct |
20 ms |
35924 KB |
Output is correct |
66 |
Correct |
152 ms |
50216 KB |
Output is correct |
67 |
Correct |
39 ms |
35908 KB |
Output is correct |
68 |
Correct |
20 ms |
36012 KB |
Output is correct |
69 |
Correct |
79 ms |
49832 KB |
Output is correct |
70 |
Correct |
20 ms |
36144 KB |
Output is correct |
71 |
Correct |
93 ms |
49512 KB |
Output is correct |
72 |
Correct |
154 ms |
51876 KB |
Output is correct |
73 |
Correct |
19 ms |
35540 KB |
Output is correct |
74 |
Correct |
124 ms |
56980 KB |
Output is correct |
75 |
Correct |
125 ms |
53176 KB |
Output is correct |
76 |
Correct |
19 ms |
35924 KB |
Output is correct |
77 |
Correct |
20 ms |
35944 KB |
Output is correct |
78 |
Correct |
74 ms |
49856 KB |
Output is correct |
79 |
Correct |
109 ms |
55860 KB |
Output is correct |
80 |
Correct |
22 ms |
36052 KB |
Output is correct |
81 |
Correct |
20 ms |
35964 KB |
Output is correct |
82 |
Correct |
18 ms |
35564 KB |
Output is correct |
83 |
Correct |
18 ms |
35540 KB |
Output is correct |
84 |
Correct |
19 ms |
35952 KB |
Output is correct |
85 |
Correct |
126 ms |
50952 KB |
Output is correct |
86 |
Correct |
20 ms |
36016 KB |
Output is correct |
87 |
Correct |
95 ms |
49552 KB |
Output is correct |
88 |
Correct |
20 ms |
35520 KB |
Output is correct |
89 |
Correct |
130 ms |
49612 KB |
Output is correct |
90 |
Correct |
112 ms |
51124 KB |
Output is correct |
91 |
Correct |
139 ms |
55244 KB |
Output is correct |
92 |
Correct |
80 ms |
49740 KB |
Output is correct |
93 |
Correct |
20 ms |
36052 KB |
Output is correct |
94 |
Correct |
18 ms |
35608 KB |
Output is correct |
95 |
Correct |
96 ms |
54908 KB |
Output is correct |
96 |
Correct |
158 ms |
59420 KB |
Output is correct |
97 |
Correct |
18 ms |
36024 KB |
Output is correct |
98 |
Correct |
30 ms |
35540 KB |
Output is correct |
99 |
Correct |
101 ms |
51536 KB |
Output is correct |
100 |
Correct |
21 ms |
35928 KB |
Output is correct |
101 |
Correct |
138 ms |
49940 KB |
Output is correct |
102 |
Correct |
135 ms |
57832 KB |
Output is correct |
103 |
Correct |
19 ms |
36212 KB |
Output is correct |
104 |
Correct |
20 ms |
36184 KB |
Output is correct |
105 |
Correct |
19 ms |
35924 KB |
Output is correct |
106 |
Correct |
19 ms |
35924 KB |
Output is correct |
107 |
Correct |
19 ms |
36052 KB |
Output is correct |
108 |
Correct |
554 ms |
112044 KB |
Output is correct |
109 |
Correct |
1082 ms |
122708 KB |
Output is correct |
110 |
Correct |
806 ms |
133816 KB |
Output is correct |
111 |
Correct |
1302 ms |
155652 KB |
Output is correct |
112 |
Correct |
1042 ms |
141096 KB |
Output is correct |
113 |
Correct |
703 ms |
132632 KB |
Output is correct |
114 |
Correct |
599 ms |
105108 KB |
Output is correct |
115 |
Correct |
572 ms |
105184 KB |
Output is correct |
116 |
Correct |
736 ms |
107836 KB |
Output is correct |
117 |
Correct |
981 ms |
134936 KB |
Output is correct |
118 |
Correct |
636 ms |
104768 KB |
Output is correct |
119 |
Correct |
1008 ms |
134688 KB |
Output is correct |
120 |
Correct |
1159 ms |
147996 KB |
Output is correct |
121 |
Correct |
960 ms |
134492 KB |
Output is correct |
122 |
Correct |
831 ms |
115156 KB |
Output is correct |
123 |
Correct |
665 ms |
137300 KB |
Output is correct |
124 |
Correct |
1057 ms |
126508 KB |
Output is correct |