//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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
35540 KB |
Output is correct |
2 |
Correct |
17 ms |
35540 KB |
Output is correct |
3 |
Correct |
17 ms |
35540 KB |
Output is correct |
4 |
Correct |
17 ms |
35540 KB |
Output is correct |
5 |
Correct |
18 ms |
35540 KB |
Output is correct |
6 |
Correct |
17 ms |
35516 KB |
Output is correct |
7 |
Correct |
18 ms |
35540 KB |
Output is correct |
8 |
Correct |
20 ms |
35552 KB |
Output is correct |
9 |
Correct |
17 ms |
35540 KB |
Output is correct |
10 |
Correct |
18 ms |
35512 KB |
Output is correct |
11 |
Correct |
17 ms |
35516 KB |
Output is correct |
12 |
Correct |
16 ms |
35544 KB |
Output is correct |
13 |
Correct |
17 ms |
35540 KB |
Output is correct |
14 |
Correct |
17 ms |
35500 KB |
Output is correct |
15 |
Correct |
17 ms |
35540 KB |
Output is correct |
16 |
Correct |
17 ms |
35520 KB |
Output is correct |
17 |
Correct |
20 ms |
35540 KB |
Output is correct |
18 |
Correct |
20 ms |
35604 KB |
Output is correct |
19 |
Correct |
18 ms |
35576 KB |
Output is correct |
20 |
Correct |
17 ms |
35540 KB |
Output is correct |
21 |
Correct |
17 ms |
35540 KB |
Output is correct |
22 |
Correct |
17 ms |
35532 KB |
Output is correct |
23 |
Correct |
17 ms |
35540 KB |
Output is correct |
24 |
Correct |
16 ms |
35504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
35540 KB |
Output is correct |
2 |
Correct |
17 ms |
35540 KB |
Output is correct |
3 |
Correct |
17 ms |
35540 KB |
Output is correct |
4 |
Correct |
17 ms |
35540 KB |
Output is correct |
5 |
Correct |
18 ms |
35540 KB |
Output is correct |
6 |
Correct |
17 ms |
35516 KB |
Output is correct |
7 |
Correct |
18 ms |
35540 KB |
Output is correct |
8 |
Correct |
20 ms |
35552 KB |
Output is correct |
9 |
Correct |
17 ms |
35540 KB |
Output is correct |
10 |
Correct |
18 ms |
35512 KB |
Output is correct |
11 |
Correct |
17 ms |
35516 KB |
Output is correct |
12 |
Correct |
16 ms |
35544 KB |
Output is correct |
13 |
Correct |
17 ms |
35540 KB |
Output is correct |
14 |
Correct |
17 ms |
35500 KB |
Output is correct |
15 |
Correct |
17 ms |
35540 KB |
Output is correct |
16 |
Correct |
17 ms |
35520 KB |
Output is correct |
17 |
Correct |
20 ms |
35540 KB |
Output is correct |
18 |
Correct |
20 ms |
35604 KB |
Output is correct |
19 |
Correct |
18 ms |
35576 KB |
Output is correct |
20 |
Correct |
17 ms |
35540 KB |
Output is correct |
21 |
Correct |
17 ms |
35540 KB |
Output is correct |
22 |
Correct |
17 ms |
35532 KB |
Output is correct |
23 |
Correct |
17 ms |
35540 KB |
Output is correct |
24 |
Correct |
16 ms |
35504 KB |
Output is correct |
25 |
Correct |
17 ms |
35540 KB |
Output is correct |
26 |
Correct |
20 ms |
36064 KB |
Output is correct |
27 |
Correct |
18 ms |
36048 KB |
Output is correct |
28 |
Correct |
19 ms |
36188 KB |
Output is correct |
29 |
Correct |
22 ms |
36100 KB |
Output is correct |
30 |
Correct |
18 ms |
35956 KB |
Output is correct |
31 |
Correct |
16 ms |
35540 KB |
Output is correct |
32 |
Correct |
21 ms |
36160 KB |
Output is correct |
33 |
Correct |
16 ms |
35540 KB |
Output is correct |
34 |
Correct |
18 ms |
35984 KB |
Output is correct |
35 |
Correct |
18 ms |
36176 KB |
Output is correct |
36 |
Correct |
22 ms |
35972 KB |
Output is correct |
37 |
Correct |
23 ms |
36056 KB |
Output is correct |
38 |
Correct |
20 ms |
35580 KB |
Output is correct |
39 |
Correct |
18 ms |
36060 KB |
Output is correct |
40 |
Correct |
18 ms |
35980 KB |
Output is correct |
41 |
Correct |
18 ms |
36040 KB |
Output is correct |
42 |
Correct |
18 ms |
36092 KB |
Output is correct |
43 |
Correct |
18 ms |
36264 KB |
Output is correct |
44 |
Correct |
17 ms |
35532 KB |
Output is correct |
45 |
Correct |
18 ms |
36088 KB |
Output is correct |
46 |
Correct |
18 ms |
36052 KB |
Output is correct |
47 |
Correct |
16 ms |
35540 KB |
Output is correct |
48 |
Correct |
20 ms |
35996 KB |
Output is correct |
49 |
Correct |
19 ms |
36164 KB |
Output is correct |
50 |
Correct |
19 ms |
36244 KB |
Output is correct |
51 |
Correct |
19 ms |
35924 KB |
Output is correct |
52 |
Correct |
23 ms |
35924 KB |
Output is correct |
53 |
Correct |
18 ms |
36068 KB |
Output is correct |
54 |
Correct |
19 ms |
35936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
35540 KB |
Output is correct |
2 |
Correct |
17 ms |
35540 KB |
Output is correct |
3 |
Correct |
17 ms |
35540 KB |
Output is correct |
4 |
Correct |
17 ms |
35540 KB |
Output is correct |
5 |
Correct |
18 ms |
35540 KB |
Output is correct |
6 |
Correct |
17 ms |
35516 KB |
Output is correct |
7 |
Correct |
18 ms |
35540 KB |
Output is correct |
8 |
Correct |
20 ms |
35552 KB |
Output is correct |
9 |
Correct |
17 ms |
35540 KB |
Output is correct |
10 |
Correct |
18 ms |
35512 KB |
Output is correct |
11 |
Correct |
17 ms |
35516 KB |
Output is correct |
12 |
Correct |
16 ms |
35544 KB |
Output is correct |
13 |
Correct |
17 ms |
35540 KB |
Output is correct |
14 |
Correct |
17 ms |
35500 KB |
Output is correct |
15 |
Correct |
17 ms |
35540 KB |
Output is correct |
16 |
Correct |
17 ms |
35520 KB |
Output is correct |
17 |
Correct |
20 ms |
35540 KB |
Output is correct |
18 |
Correct |
20 ms |
35604 KB |
Output is correct |
19 |
Correct |
18 ms |
35576 KB |
Output is correct |
20 |
Correct |
17 ms |
35540 KB |
Output is correct |
21 |
Correct |
17 ms |
35540 KB |
Output is correct |
22 |
Correct |
17 ms |
35532 KB |
Output is correct |
23 |
Correct |
17 ms |
35540 KB |
Output is correct |
24 |
Correct |
16 ms |
35504 KB |
Output is correct |
25 |
Correct |
17 ms |
35516 KB |
Output is correct |
26 |
Correct |
72 ms |
49868 KB |
Output is correct |
27 |
Correct |
94 ms |
49484 KB |
Output is correct |
28 |
Correct |
19 ms |
35924 KB |
Output is correct |
29 |
Correct |
17 ms |
35540 KB |
Output is correct |
30 |
Correct |
21 ms |
35540 KB |
Output is correct |
31 |
Correct |
102 ms |
49584 KB |
Output is correct |
32 |
Correct |
19 ms |
36044 KB |
Output is correct |
33 |
Correct |
122 ms |
56604 KB |
Output is correct |
34 |
Correct |
98 ms |
49520 KB |
Output is correct |
35 |
Correct |
18 ms |
35924 KB |
Output is correct |
36 |
Correct |
120 ms |
50220 KB |
Output is correct |
37 |
Correct |
20 ms |
35976 KB |
Output is correct |
38 |
Correct |
20 ms |
36004 KB |
Output is correct |
39 |
Correct |
71 ms |
49828 KB |
Output is correct |
40 |
Correct |
18 ms |
36308 KB |
Output is correct |
41 |
Correct |
83 ms |
49508 KB |
Output is correct |
42 |
Correct |
119 ms |
51848 KB |
Output is correct |
43 |
Correct |
17 ms |
35540 KB |
Output is correct |
44 |
Correct |
128 ms |
56900 KB |
Output is correct |
45 |
Correct |
117 ms |
53160 KB |
Output is correct |
46 |
Correct |
18 ms |
36052 KB |
Output is correct |
47 |
Correct |
19 ms |
36004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
49808 KB |
Output is correct |
2 |
Correct |
89 ms |
55876 KB |
Output is correct |
3 |
Correct |
19 ms |
36052 KB |
Output is correct |
4 |
Correct |
19 ms |
36016 KB |
Output is correct |
5 |
Correct |
20 ms |
35600 KB |
Output is correct |
6 |
Correct |
22 ms |
35552 KB |
Output is correct |
7 |
Correct |
18 ms |
35920 KB |
Output is correct |
8 |
Correct |
120 ms |
50940 KB |
Output is correct |
9 |
Correct |
19 ms |
35924 KB |
Output is correct |
10 |
Correct |
104 ms |
49552 KB |
Output is correct |
11 |
Correct |
17 ms |
35572 KB |
Output is correct |
12 |
Correct |
138 ms |
49436 KB |
Output is correct |
13 |
Correct |
116 ms |
51140 KB |
Output is correct |
14 |
Correct |
119 ms |
55236 KB |
Output is correct |
15 |
Correct |
81 ms |
49772 KB |
Output is correct |
16 |
Correct |
19 ms |
36052 KB |
Output is correct |
17 |
Correct |
19 ms |
35568 KB |
Output is correct |
18 |
Correct |
93 ms |
54876 KB |
Output is correct |
19 |
Correct |
204 ms |
59328 KB |
Output is correct |
20 |
Correct |
20 ms |
36048 KB |
Output is correct |
21 |
Correct |
21 ms |
35608 KB |
Output is correct |
22 |
Correct |
126 ms |
51512 KB |
Output is correct |
23 |
Correct |
21 ms |
36020 KB |
Output is correct |
24 |
Correct |
133 ms |
50012 KB |
Output is correct |
25 |
Correct |
152 ms |
57828 KB |
Output is correct |
26 |
Correct |
21 ms |
36128 KB |
Output is correct |
27 |
Correct |
19 ms |
36224 KB |
Output is correct |
28 |
Correct |
19 ms |
35948 KB |
Output is correct |
29 |
Correct |
19 ms |
36044 KB |
Output is correct |
30 |
Correct |
20 ms |
36052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
35540 KB |
Output is correct |
2 |
Correct |
17 ms |
35540 KB |
Output is correct |
3 |
Correct |
17 ms |
35540 KB |
Output is correct |
4 |
Correct |
17 ms |
35540 KB |
Output is correct |
5 |
Correct |
18 ms |
35540 KB |
Output is correct |
6 |
Correct |
17 ms |
35516 KB |
Output is correct |
7 |
Correct |
18 ms |
35540 KB |
Output is correct |
8 |
Correct |
20 ms |
35552 KB |
Output is correct |
9 |
Correct |
17 ms |
35540 KB |
Output is correct |
10 |
Correct |
18 ms |
35512 KB |
Output is correct |
11 |
Correct |
17 ms |
35516 KB |
Output is correct |
12 |
Correct |
16 ms |
35544 KB |
Output is correct |
13 |
Correct |
17 ms |
35540 KB |
Output is correct |
14 |
Correct |
17 ms |
35500 KB |
Output is correct |
15 |
Correct |
17 ms |
35540 KB |
Output is correct |
16 |
Correct |
17 ms |
35520 KB |
Output is correct |
17 |
Correct |
20 ms |
35540 KB |
Output is correct |
18 |
Correct |
20 ms |
35604 KB |
Output is correct |
19 |
Correct |
18 ms |
35576 KB |
Output is correct |
20 |
Correct |
17 ms |
35540 KB |
Output is correct |
21 |
Correct |
17 ms |
35540 KB |
Output is correct |
22 |
Correct |
17 ms |
35532 KB |
Output is correct |
23 |
Correct |
17 ms |
35540 KB |
Output is correct |
24 |
Correct |
16 ms |
35504 KB |
Output is correct |
25 |
Correct |
17 ms |
35540 KB |
Output is correct |
26 |
Correct |
20 ms |
36064 KB |
Output is correct |
27 |
Correct |
18 ms |
36048 KB |
Output is correct |
28 |
Correct |
19 ms |
36188 KB |
Output is correct |
29 |
Correct |
22 ms |
36100 KB |
Output is correct |
30 |
Correct |
18 ms |
35956 KB |
Output is correct |
31 |
Correct |
16 ms |
35540 KB |
Output is correct |
32 |
Correct |
21 ms |
36160 KB |
Output is correct |
33 |
Correct |
16 ms |
35540 KB |
Output is correct |
34 |
Correct |
18 ms |
35984 KB |
Output is correct |
35 |
Correct |
18 ms |
36176 KB |
Output is correct |
36 |
Correct |
22 ms |
35972 KB |
Output is correct |
37 |
Correct |
23 ms |
36056 KB |
Output is correct |
38 |
Correct |
20 ms |
35580 KB |
Output is correct |
39 |
Correct |
18 ms |
36060 KB |
Output is correct |
40 |
Correct |
18 ms |
35980 KB |
Output is correct |
41 |
Correct |
18 ms |
36040 KB |
Output is correct |
42 |
Correct |
18 ms |
36092 KB |
Output is correct |
43 |
Correct |
18 ms |
36264 KB |
Output is correct |
44 |
Correct |
17 ms |
35532 KB |
Output is correct |
45 |
Correct |
18 ms |
36088 KB |
Output is correct |
46 |
Correct |
18 ms |
36052 KB |
Output is correct |
47 |
Correct |
16 ms |
35540 KB |
Output is correct |
48 |
Correct |
20 ms |
35996 KB |
Output is correct |
49 |
Correct |
19 ms |
36164 KB |
Output is correct |
50 |
Correct |
19 ms |
36244 KB |
Output is correct |
51 |
Correct |
19 ms |
35924 KB |
Output is correct |
52 |
Correct |
23 ms |
35924 KB |
Output is correct |
53 |
Correct |
18 ms |
36068 KB |
Output is correct |
54 |
Correct |
19 ms |
35936 KB |
Output is correct |
55 |
Correct |
17 ms |
35516 KB |
Output is correct |
56 |
Correct |
72 ms |
49868 KB |
Output is correct |
57 |
Correct |
94 ms |
49484 KB |
Output is correct |
58 |
Correct |
19 ms |
35924 KB |
Output is correct |
59 |
Correct |
17 ms |
35540 KB |
Output is correct |
60 |
Correct |
21 ms |
35540 KB |
Output is correct |
61 |
Correct |
102 ms |
49584 KB |
Output is correct |
62 |
Correct |
19 ms |
36044 KB |
Output is correct |
63 |
Correct |
122 ms |
56604 KB |
Output is correct |
64 |
Correct |
98 ms |
49520 KB |
Output is correct |
65 |
Correct |
18 ms |
35924 KB |
Output is correct |
66 |
Correct |
120 ms |
50220 KB |
Output is correct |
67 |
Correct |
20 ms |
35976 KB |
Output is correct |
68 |
Correct |
20 ms |
36004 KB |
Output is correct |
69 |
Correct |
71 ms |
49828 KB |
Output is correct |
70 |
Correct |
18 ms |
36308 KB |
Output is correct |
71 |
Correct |
83 ms |
49508 KB |
Output is correct |
72 |
Correct |
119 ms |
51848 KB |
Output is correct |
73 |
Correct |
17 ms |
35540 KB |
Output is correct |
74 |
Correct |
128 ms |
56900 KB |
Output is correct |
75 |
Correct |
117 ms |
53160 KB |
Output is correct |
76 |
Correct |
18 ms |
36052 KB |
Output is correct |
77 |
Correct |
19 ms |
36004 KB |
Output is correct |
78 |
Correct |
76 ms |
49808 KB |
Output is correct |
79 |
Correct |
89 ms |
55876 KB |
Output is correct |
80 |
Correct |
19 ms |
36052 KB |
Output is correct |
81 |
Correct |
19 ms |
36016 KB |
Output is correct |
82 |
Correct |
20 ms |
35600 KB |
Output is correct |
83 |
Correct |
22 ms |
35552 KB |
Output is correct |
84 |
Correct |
18 ms |
35920 KB |
Output is correct |
85 |
Correct |
120 ms |
50940 KB |
Output is correct |
86 |
Correct |
19 ms |
35924 KB |
Output is correct |
87 |
Correct |
104 ms |
49552 KB |
Output is correct |
88 |
Correct |
17 ms |
35572 KB |
Output is correct |
89 |
Correct |
138 ms |
49436 KB |
Output is correct |
90 |
Correct |
116 ms |
51140 KB |
Output is correct |
91 |
Correct |
119 ms |
55236 KB |
Output is correct |
92 |
Correct |
81 ms |
49772 KB |
Output is correct |
93 |
Correct |
19 ms |
36052 KB |
Output is correct |
94 |
Correct |
19 ms |
35568 KB |
Output is correct |
95 |
Correct |
93 ms |
54876 KB |
Output is correct |
96 |
Correct |
204 ms |
59328 KB |
Output is correct |
97 |
Correct |
20 ms |
36048 KB |
Output is correct |
98 |
Correct |
21 ms |
35608 KB |
Output is correct |
99 |
Correct |
126 ms |
51512 KB |
Output is correct |
100 |
Correct |
21 ms |
36020 KB |
Output is correct |
101 |
Correct |
133 ms |
50012 KB |
Output is correct |
102 |
Correct |
152 ms |
57828 KB |
Output is correct |
103 |
Correct |
21 ms |
36128 KB |
Output is correct |
104 |
Correct |
19 ms |
36224 KB |
Output is correct |
105 |
Correct |
19 ms |
35948 KB |
Output is correct |
106 |
Correct |
19 ms |
36044 KB |
Output is correct |
107 |
Correct |
20 ms |
36052 KB |
Output is correct |
108 |
Correct |
611 ms |
115072 KB |
Output is correct |
109 |
Correct |
1040 ms |
125776 KB |
Output is correct |
110 |
Correct |
989 ms |
137000 KB |
Output is correct |
111 |
Correct |
1372 ms |
165744 KB |
Output is correct |
112 |
Correct |
1120 ms |
145056 KB |
Output is correct |
113 |
Correct |
717 ms |
135872 KB |
Output is correct |
114 |
Correct |
676 ms |
108172 KB |
Output is correct |
115 |
Correct |
632 ms |
108128 KB |
Output is correct |
116 |
Correct |
796 ms |
113264 KB |
Output is correct |
117 |
Correct |
1028 ms |
138280 KB |
Output is correct |
118 |
Correct |
656 ms |
107292 KB |
Output is correct |
119 |
Correct |
1076 ms |
136408 KB |
Output is correct |
120 |
Correct |
1343 ms |
157712 KB |
Output is correct |
121 |
Correct |
1347 ms |
144320 KB |
Output is correct |
122 |
Correct |
886 ms |
124892 KB |
Output is correct |
123 |
Correct |
693 ms |
147428 KB |
Output is correct |
124 |
Correct |
1089 ms |
136228 KB |
Output is correct |