#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 5;
vector<int> adj[maxn];
int N, cnt[maxn], sum[maxn], K;
vector<int> grp[maxn];
int depth[maxn], tin[maxn], tout[maxn], par[maxn][20];
class DSU
{
public:
vector<int> lab;
void init(int n)
{
lab.assign(n + 5, -1);
}
int finds(int u)
{
if (lab[u] < 0) return u;
return lab[u] = finds(lab[u]);
}
void merges(int u, int v)
{
u = finds(u); v = finds(v);
if (u == v) return;
if (lab[v] < lab[u]) swap(u, v);
lab[u] += lab[v];
lab[v] = u;
}
}dsu;
void prepare(int u = 1, int p = -1)
{
static int nTime = 0;
tin[u] = ++nTime;
for (int i = 1; (1 << i) <= N; ++i){
par[u][i] = par[par[u][i - 1]][i - 1];
}
for (int v : adj[u]) if (v != p){
depth[v] = depth[u] + 1;
par[v][0] = u;
prepare(v, u);
}
tout[u] = nTime;
}
int LCA(int x, int y)
{
if (depth[y] > depth[x]) swap(x, y);
for (int k = 19; k >= 0; --k){
if (depth[par[x][k]] >= depth[y]){
x = par[x][k];
}
}
if (x == y) return x;
for (int k = 19; k >= 0; --k){
if (par[x][k] != par[y][k]){
x = par[x][k];
y = par[y][k];
}
}
return par[x][0];
}
void DFS(int u, int p = -1)
{
for (int v : adj[u]) if (v != p){
DFS(v, u);
sum[u] += sum[v];
}
}
signed main(void)
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen("A.INP", "r")){
freopen("A.INP", "r", stdin);
freopen("A.OUT", "w", stdout);
}
cin >> N >> K; dsu.init(N);
for (int i = 1; i < N; ++i){
int u, v; cin >> u >> v;
adj[u].pb(v); adj[v].pb(u);
}
for (int i = 1; i <= N; ++i){
int v; cin >> v;
grp[v].pb(i);
}
depth[1] = 1;
prepare();
for (int i = 1; i <= K; ++i){
sort(grp[i].begin(), grp[i].end(),
[&](const int & x, const int & y){
return tin[x] < tin[y];
}
);
for (int j = 0; j < (int)grp[i].size() - 1; ++j){
sum[grp[i][j]]++; sum[grp[i][j + 1]]++;
sum[LCA(grp[i][j], grp[i][j + 1])] -= 2;
}
}
DFS(1);
for (int i = 2; i <= N; ++i){
if (sum[i] >= 1){
dsu.merges(i, par[i][0]);
}
}
for (int i = 2; i <= N; ++i){
int l = dsu.finds(i);
int r = dsu.finds(par[i][0]);
if (l != r) ++cnt[l], ++cnt[r];
}
int res = 0;
for (int i = 1; i <= N; ++i){
res += (cnt[i] == 1);
}
cout << (res + 1) / 2;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:86:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("A.INP", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~
mergers.cpp:87:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("A.OUT", "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
21 ms |
23928 KB |
Output is correct |
3 |
Correct |
20 ms |
23800 KB |
Output is correct |
4 |
Correct |
21 ms |
23804 KB |
Output is correct |
5 |
Correct |
21 ms |
23800 KB |
Output is correct |
6 |
Correct |
21 ms |
23800 KB |
Output is correct |
7 |
Correct |
21 ms |
24056 KB |
Output is correct |
8 |
Correct |
20 ms |
23928 KB |
Output is correct |
9 |
Correct |
21 ms |
23800 KB |
Output is correct |
10 |
Correct |
20 ms |
23800 KB |
Output is correct |
11 |
Correct |
21 ms |
23932 KB |
Output is correct |
12 |
Correct |
21 ms |
23928 KB |
Output is correct |
13 |
Correct |
20 ms |
23800 KB |
Output is correct |
14 |
Correct |
20 ms |
23800 KB |
Output is correct |
15 |
Correct |
21 ms |
24056 KB |
Output is correct |
16 |
Correct |
20 ms |
23928 KB |
Output is correct |
17 |
Correct |
20 ms |
23800 KB |
Output is correct |
18 |
Correct |
20 ms |
23800 KB |
Output is correct |
19 |
Correct |
20 ms |
23804 KB |
Output is correct |
20 |
Correct |
20 ms |
23928 KB |
Output is correct |
21 |
Correct |
20 ms |
23928 KB |
Output is correct |
22 |
Correct |
20 ms |
23804 KB |
Output is correct |
23 |
Correct |
21 ms |
23800 KB |
Output is correct |
24 |
Correct |
20 ms |
23800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
21 ms |
23928 KB |
Output is correct |
3 |
Correct |
20 ms |
23800 KB |
Output is correct |
4 |
Correct |
21 ms |
23804 KB |
Output is correct |
5 |
Correct |
21 ms |
23800 KB |
Output is correct |
6 |
Correct |
21 ms |
23800 KB |
Output is correct |
7 |
Correct |
21 ms |
24056 KB |
Output is correct |
8 |
Correct |
20 ms |
23928 KB |
Output is correct |
9 |
Correct |
21 ms |
23800 KB |
Output is correct |
10 |
Correct |
20 ms |
23800 KB |
Output is correct |
11 |
Correct |
21 ms |
23932 KB |
Output is correct |
12 |
Correct |
21 ms |
23928 KB |
Output is correct |
13 |
Correct |
20 ms |
23800 KB |
Output is correct |
14 |
Correct |
20 ms |
23800 KB |
Output is correct |
15 |
Correct |
21 ms |
24056 KB |
Output is correct |
16 |
Correct |
20 ms |
23928 KB |
Output is correct |
17 |
Correct |
20 ms |
23800 KB |
Output is correct |
18 |
Correct |
20 ms |
23800 KB |
Output is correct |
19 |
Correct |
20 ms |
23804 KB |
Output is correct |
20 |
Correct |
20 ms |
23928 KB |
Output is correct |
21 |
Correct |
20 ms |
23928 KB |
Output is correct |
22 |
Correct |
20 ms |
23804 KB |
Output is correct |
23 |
Correct |
21 ms |
23800 KB |
Output is correct |
24 |
Correct |
20 ms |
23800 KB |
Output is correct |
25 |
Correct |
20 ms |
23932 KB |
Output is correct |
26 |
Correct |
22 ms |
24312 KB |
Output is correct |
27 |
Correct |
24 ms |
24312 KB |
Output is correct |
28 |
Correct |
22 ms |
24440 KB |
Output is correct |
29 |
Correct |
22 ms |
24312 KB |
Output is correct |
30 |
Correct |
25 ms |
24440 KB |
Output is correct |
31 |
Correct |
20 ms |
23928 KB |
Output is correct |
32 |
Correct |
23 ms |
24572 KB |
Output is correct |
33 |
Correct |
21 ms |
23800 KB |
Output is correct |
34 |
Correct |
22 ms |
24312 KB |
Output is correct |
35 |
Correct |
22 ms |
24312 KB |
Output is correct |
36 |
Correct |
22 ms |
24312 KB |
Output is correct |
37 |
Correct |
23 ms |
24312 KB |
Output is correct |
38 |
Correct |
22 ms |
23800 KB |
Output is correct |
39 |
Correct |
22 ms |
24312 KB |
Output is correct |
40 |
Correct |
23 ms |
24312 KB |
Output is correct |
41 |
Correct |
23 ms |
24312 KB |
Output is correct |
42 |
Correct |
24 ms |
24312 KB |
Output is correct |
43 |
Correct |
22 ms |
24440 KB |
Output is correct |
44 |
Correct |
20 ms |
23928 KB |
Output is correct |
45 |
Correct |
24 ms |
24440 KB |
Output is correct |
46 |
Correct |
24 ms |
24312 KB |
Output is correct |
47 |
Correct |
21 ms |
23800 KB |
Output is correct |
48 |
Correct |
23 ms |
24312 KB |
Output is correct |
49 |
Correct |
22 ms |
24440 KB |
Output is correct |
50 |
Correct |
25 ms |
24440 KB |
Output is correct |
51 |
Correct |
23 ms |
24312 KB |
Output is correct |
52 |
Correct |
22 ms |
24312 KB |
Output is correct |
53 |
Correct |
24 ms |
24312 KB |
Output is correct |
54 |
Correct |
23 ms |
24312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
21 ms |
23928 KB |
Output is correct |
3 |
Correct |
20 ms |
23800 KB |
Output is correct |
4 |
Correct |
21 ms |
23804 KB |
Output is correct |
5 |
Correct |
21 ms |
23800 KB |
Output is correct |
6 |
Correct |
21 ms |
23800 KB |
Output is correct |
7 |
Correct |
21 ms |
24056 KB |
Output is correct |
8 |
Correct |
20 ms |
23928 KB |
Output is correct |
9 |
Correct |
21 ms |
23800 KB |
Output is correct |
10 |
Correct |
20 ms |
23800 KB |
Output is correct |
11 |
Correct |
21 ms |
23932 KB |
Output is correct |
12 |
Correct |
21 ms |
23928 KB |
Output is correct |
13 |
Correct |
20 ms |
23800 KB |
Output is correct |
14 |
Correct |
20 ms |
23800 KB |
Output is correct |
15 |
Correct |
21 ms |
24056 KB |
Output is correct |
16 |
Correct |
20 ms |
23928 KB |
Output is correct |
17 |
Correct |
20 ms |
23800 KB |
Output is correct |
18 |
Correct |
20 ms |
23800 KB |
Output is correct |
19 |
Correct |
20 ms |
23804 KB |
Output is correct |
20 |
Correct |
20 ms |
23928 KB |
Output is correct |
21 |
Correct |
20 ms |
23928 KB |
Output is correct |
22 |
Correct |
20 ms |
23804 KB |
Output is correct |
23 |
Correct |
21 ms |
23800 KB |
Output is correct |
24 |
Correct |
20 ms |
23800 KB |
Output is correct |
25 |
Correct |
21 ms |
23928 KB |
Output is correct |
26 |
Correct |
137 ms |
37744 KB |
Output is correct |
27 |
Correct |
177 ms |
37368 KB |
Output is correct |
28 |
Correct |
23 ms |
24312 KB |
Output is correct |
29 |
Correct |
20 ms |
23928 KB |
Output is correct |
30 |
Correct |
20 ms |
23800 KB |
Output is correct |
31 |
Correct |
161 ms |
37368 KB |
Output is correct |
32 |
Correct |
24 ms |
24312 KB |
Output is correct |
33 |
Correct |
258 ms |
43128 KB |
Output is correct |
34 |
Correct |
203 ms |
37496 KB |
Output is correct |
35 |
Correct |
23 ms |
24316 KB |
Output is correct |
36 |
Correct |
193 ms |
38008 KB |
Output is correct |
37 |
Correct |
22 ms |
24312 KB |
Output is correct |
38 |
Correct |
22 ms |
24312 KB |
Output is correct |
39 |
Correct |
133 ms |
37744 KB |
Output is correct |
40 |
Correct |
24 ms |
24440 KB |
Output is correct |
41 |
Correct |
171 ms |
37360 KB |
Output is correct |
42 |
Correct |
225 ms |
39292 KB |
Output is correct |
43 |
Correct |
20 ms |
23932 KB |
Output is correct |
44 |
Correct |
219 ms |
43376 KB |
Output is correct |
45 |
Correct |
240 ms |
40312 KB |
Output is correct |
46 |
Correct |
24 ms |
24316 KB |
Output is correct |
47 |
Correct |
24 ms |
24312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
137 ms |
37744 KB |
Output is correct |
2 |
Correct |
115 ms |
40436 KB |
Output is correct |
3 |
Correct |
23 ms |
24312 KB |
Output is correct |
4 |
Correct |
24 ms |
24312 KB |
Output is correct |
5 |
Correct |
21 ms |
23800 KB |
Output is correct |
6 |
Correct |
21 ms |
23928 KB |
Output is correct |
7 |
Correct |
22 ms |
24316 KB |
Output is correct |
8 |
Correct |
181 ms |
38904 KB |
Output is correct |
9 |
Correct |
22 ms |
24312 KB |
Output is correct |
10 |
Correct |
164 ms |
37624 KB |
Output is correct |
11 |
Correct |
22 ms |
23928 KB |
Output is correct |
12 |
Correct |
188 ms |
37624 KB |
Output is correct |
13 |
Correct |
170 ms |
38780 KB |
Output is correct |
14 |
Correct |
141 ms |
40440 KB |
Output is correct |
15 |
Correct |
127 ms |
37744 KB |
Output is correct |
16 |
Correct |
23 ms |
24316 KB |
Output is correct |
17 |
Correct |
20 ms |
23800 KB |
Output is correct |
18 |
Correct |
118 ms |
40432 KB |
Output is correct |
19 |
Correct |
173 ms |
43512 KB |
Output is correct |
20 |
Correct |
24 ms |
24312 KB |
Output is correct |
21 |
Correct |
20 ms |
23804 KB |
Output is correct |
22 |
Correct |
124 ms |
39152 KB |
Output is correct |
23 |
Correct |
23 ms |
24312 KB |
Output is correct |
24 |
Correct |
175 ms |
38008 KB |
Output is correct |
25 |
Correct |
167 ms |
42600 KB |
Output is correct |
26 |
Correct |
22 ms |
24316 KB |
Output is correct |
27 |
Correct |
22 ms |
24440 KB |
Output is correct |
28 |
Correct |
23 ms |
24312 KB |
Output is correct |
29 |
Correct |
22 ms |
24312 KB |
Output is correct |
30 |
Correct |
23 ms |
24312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
23800 KB |
Output is correct |
2 |
Correct |
21 ms |
23928 KB |
Output is correct |
3 |
Correct |
20 ms |
23800 KB |
Output is correct |
4 |
Correct |
21 ms |
23804 KB |
Output is correct |
5 |
Correct |
21 ms |
23800 KB |
Output is correct |
6 |
Correct |
21 ms |
23800 KB |
Output is correct |
7 |
Correct |
21 ms |
24056 KB |
Output is correct |
8 |
Correct |
20 ms |
23928 KB |
Output is correct |
9 |
Correct |
21 ms |
23800 KB |
Output is correct |
10 |
Correct |
20 ms |
23800 KB |
Output is correct |
11 |
Correct |
21 ms |
23932 KB |
Output is correct |
12 |
Correct |
21 ms |
23928 KB |
Output is correct |
13 |
Correct |
20 ms |
23800 KB |
Output is correct |
14 |
Correct |
20 ms |
23800 KB |
Output is correct |
15 |
Correct |
21 ms |
24056 KB |
Output is correct |
16 |
Correct |
20 ms |
23928 KB |
Output is correct |
17 |
Correct |
20 ms |
23800 KB |
Output is correct |
18 |
Correct |
20 ms |
23800 KB |
Output is correct |
19 |
Correct |
20 ms |
23804 KB |
Output is correct |
20 |
Correct |
20 ms |
23928 KB |
Output is correct |
21 |
Correct |
20 ms |
23928 KB |
Output is correct |
22 |
Correct |
20 ms |
23804 KB |
Output is correct |
23 |
Correct |
21 ms |
23800 KB |
Output is correct |
24 |
Correct |
20 ms |
23800 KB |
Output is correct |
25 |
Correct |
20 ms |
23932 KB |
Output is correct |
26 |
Correct |
22 ms |
24312 KB |
Output is correct |
27 |
Correct |
24 ms |
24312 KB |
Output is correct |
28 |
Correct |
22 ms |
24440 KB |
Output is correct |
29 |
Correct |
22 ms |
24312 KB |
Output is correct |
30 |
Correct |
25 ms |
24440 KB |
Output is correct |
31 |
Correct |
20 ms |
23928 KB |
Output is correct |
32 |
Correct |
23 ms |
24572 KB |
Output is correct |
33 |
Correct |
21 ms |
23800 KB |
Output is correct |
34 |
Correct |
22 ms |
24312 KB |
Output is correct |
35 |
Correct |
22 ms |
24312 KB |
Output is correct |
36 |
Correct |
22 ms |
24312 KB |
Output is correct |
37 |
Correct |
23 ms |
24312 KB |
Output is correct |
38 |
Correct |
22 ms |
23800 KB |
Output is correct |
39 |
Correct |
22 ms |
24312 KB |
Output is correct |
40 |
Correct |
23 ms |
24312 KB |
Output is correct |
41 |
Correct |
23 ms |
24312 KB |
Output is correct |
42 |
Correct |
24 ms |
24312 KB |
Output is correct |
43 |
Correct |
22 ms |
24440 KB |
Output is correct |
44 |
Correct |
20 ms |
23928 KB |
Output is correct |
45 |
Correct |
24 ms |
24440 KB |
Output is correct |
46 |
Correct |
24 ms |
24312 KB |
Output is correct |
47 |
Correct |
21 ms |
23800 KB |
Output is correct |
48 |
Correct |
23 ms |
24312 KB |
Output is correct |
49 |
Correct |
22 ms |
24440 KB |
Output is correct |
50 |
Correct |
25 ms |
24440 KB |
Output is correct |
51 |
Correct |
23 ms |
24312 KB |
Output is correct |
52 |
Correct |
22 ms |
24312 KB |
Output is correct |
53 |
Correct |
24 ms |
24312 KB |
Output is correct |
54 |
Correct |
23 ms |
24312 KB |
Output is correct |
55 |
Correct |
21 ms |
23928 KB |
Output is correct |
56 |
Correct |
137 ms |
37744 KB |
Output is correct |
57 |
Correct |
177 ms |
37368 KB |
Output is correct |
58 |
Correct |
23 ms |
24312 KB |
Output is correct |
59 |
Correct |
20 ms |
23928 KB |
Output is correct |
60 |
Correct |
20 ms |
23800 KB |
Output is correct |
61 |
Correct |
161 ms |
37368 KB |
Output is correct |
62 |
Correct |
24 ms |
24312 KB |
Output is correct |
63 |
Correct |
258 ms |
43128 KB |
Output is correct |
64 |
Correct |
203 ms |
37496 KB |
Output is correct |
65 |
Correct |
23 ms |
24316 KB |
Output is correct |
66 |
Correct |
193 ms |
38008 KB |
Output is correct |
67 |
Correct |
22 ms |
24312 KB |
Output is correct |
68 |
Correct |
22 ms |
24312 KB |
Output is correct |
69 |
Correct |
133 ms |
37744 KB |
Output is correct |
70 |
Correct |
24 ms |
24440 KB |
Output is correct |
71 |
Correct |
171 ms |
37360 KB |
Output is correct |
72 |
Correct |
225 ms |
39292 KB |
Output is correct |
73 |
Correct |
20 ms |
23932 KB |
Output is correct |
74 |
Correct |
219 ms |
43376 KB |
Output is correct |
75 |
Correct |
240 ms |
40312 KB |
Output is correct |
76 |
Correct |
24 ms |
24316 KB |
Output is correct |
77 |
Correct |
24 ms |
24312 KB |
Output is correct |
78 |
Correct |
137 ms |
37744 KB |
Output is correct |
79 |
Correct |
115 ms |
40436 KB |
Output is correct |
80 |
Correct |
23 ms |
24312 KB |
Output is correct |
81 |
Correct |
24 ms |
24312 KB |
Output is correct |
82 |
Correct |
21 ms |
23800 KB |
Output is correct |
83 |
Correct |
21 ms |
23928 KB |
Output is correct |
84 |
Correct |
22 ms |
24316 KB |
Output is correct |
85 |
Correct |
181 ms |
38904 KB |
Output is correct |
86 |
Correct |
22 ms |
24312 KB |
Output is correct |
87 |
Correct |
164 ms |
37624 KB |
Output is correct |
88 |
Correct |
22 ms |
23928 KB |
Output is correct |
89 |
Correct |
188 ms |
37624 KB |
Output is correct |
90 |
Correct |
170 ms |
38780 KB |
Output is correct |
91 |
Correct |
141 ms |
40440 KB |
Output is correct |
92 |
Correct |
127 ms |
37744 KB |
Output is correct |
93 |
Correct |
23 ms |
24316 KB |
Output is correct |
94 |
Correct |
20 ms |
23800 KB |
Output is correct |
95 |
Correct |
118 ms |
40432 KB |
Output is correct |
96 |
Correct |
173 ms |
43512 KB |
Output is correct |
97 |
Correct |
24 ms |
24312 KB |
Output is correct |
98 |
Correct |
20 ms |
23804 KB |
Output is correct |
99 |
Correct |
124 ms |
39152 KB |
Output is correct |
100 |
Correct |
23 ms |
24312 KB |
Output is correct |
101 |
Correct |
175 ms |
38008 KB |
Output is correct |
102 |
Correct |
167 ms |
42600 KB |
Output is correct |
103 |
Correct |
22 ms |
24316 KB |
Output is correct |
104 |
Correct |
22 ms |
24440 KB |
Output is correct |
105 |
Correct |
23 ms |
24312 KB |
Output is correct |
106 |
Correct |
22 ms |
24312 KB |
Output is correct |
107 |
Correct |
23 ms |
24312 KB |
Output is correct |
108 |
Correct |
794 ms |
108660 KB |
Output is correct |
109 |
Correct |
1413 ms |
113076 KB |
Output is correct |
110 |
Correct |
1398 ms |
122144 KB |
Output is correct |
111 |
Correct |
1220 ms |
134904 KB |
Output is correct |
112 |
Correct |
1191 ms |
127100 KB |
Output is correct |
113 |
Correct |
698 ms |
116460 KB |
Output is correct |
114 |
Correct |
1040 ms |
98708 KB |
Output is correct |
115 |
Correct |
1035 ms |
98668 KB |
Output is correct |
116 |
Correct |
1274 ms |
105080 KB |
Output is correct |
117 |
Correct |
945 ms |
116660 KB |
Output is correct |
118 |
Correct |
1012 ms |
99576 KB |
Output is correct |
119 |
Correct |
946 ms |
116600 KB |
Output is correct |
120 |
Correct |
1182 ms |
127480 KB |
Output is correct |
121 |
Correct |
941 ms |
116472 KB |
Output is correct |
122 |
Correct |
1151 ms |
110200 KB |
Output is correct |
123 |
Correct |
665 ms |
116072 KB |
Output is correct |
124 |
Correct |
1128 ms |
114552 KB |
Output is correct |