#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
using namespace std;
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
int const nmax = 500000;
int const lgmax = 20;
vector<int> g[1 + nmax];
int far[1 + lgmax][1 + nmax];
int level[1 + nmax];
void dfs(int node, int parent){
level[node] = level[parent] + 1;
far[0][node] = parent;
for(int h = 0; h < g[node].size(); h++){
int to = g[node][h];
if(to != parent)
dfs(to, node);
}
}
void computefar(int n){
for(int h = 1; h <= lgmax; h++)
for(int i = 1;i <= n; i++)
far[h][i] = far[h - 1][far[h - 1][i]];
}
int getlca(int x, int y){
if(level[x] < level[y])
swap(x, y);
for(int h = lgmax; 0 <= h; h--)
if(level[y] + (1 << h) <= level[x])
x = far[h][x];
if(x == y)
return x;
for(int h = lgmax; 0 <= h; h--)
if(far[h][x] != far[h][y]){
x = far[h][x];
y = far[h][y];
}
return far[0][x];
}
class Dsu{
private:
vector<int> mult;
public:
vector<int> highest;
Dsu(int n){
mult.resize(1 + n);
highest.resize(1 + n);
for(int i = 1;i <= n; i++) {
mult[i] = i;
highest[i] = i;
}
}
int jump(int x){
if(mult[x] != x)
mult[x] = jump(mult[x]);
return mult[x];
}
bool unite(int gala, int galb){
gala = jump(gala);
galb = jump(galb);
if(gala == galb)
return 0;
mult[gala] = galb;
if(level[highest[galb]] < level[highest[gala]])
gala = galb;
return 1;
}
};
int frec[1 + nmax];
void markpath(int x, int y, Dsu &dsu){
int lca = getlca(x, y);
x = dsu.highest[dsu.jump(x)];
while(level[lca] < level[x]){
dsu.unite(x, far[0][x]);
x = dsu.highest[dsu.jump(x)];
}
y = dsu.highest[dsu.jump(y)];
while(level[lca] < level[y]){
dsu.unite(y, far[0][y]);
y = dsu.highest[dsu.jump(y)];
}
}
set<int> tree[1 + nmax];
int edge[1 + nmax][2];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
for(int i = 1;i < n; i++){
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
edge[i][0] = x;
edge[i][1] = y;
}
dfs(1, 0);
computefar(n);
Dsu dsu(n);
for(int i = 1;i <= n; i++){
int val;
cin >> val;
if(0 < frec[val]) {
markpath(frec[val], i, dsu);
}
frec[val] = i;
}
for(int i = 1;i < n; i++){
int x = dsu.jump(edge[i][0]);
int y = dsu.jump(edge[i][1]);
if(x != y){
tree[x].insert(y);
tree[y].insert(x);
}
}
int leafs = 0;
for(int i = 1; i <= n; i++)
leafs += (tree[i].size() == 1);
if(leafs == 0)
cout << 0;
else
cout << (leafs + 1) / 2;
return 0;
}
Compilation message
mergers.cpp: In function 'void dfs(int, int)':
mergers.cpp:24:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int h = 0; h < g[node].size(); h++){
~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
35712 KB |
Output is correct |
2 |
Correct |
24 ms |
35712 KB |
Output is correct |
3 |
Correct |
26 ms |
35840 KB |
Output is correct |
4 |
Correct |
23 ms |
35712 KB |
Output is correct |
5 |
Correct |
24 ms |
35752 KB |
Output is correct |
6 |
Correct |
23 ms |
35712 KB |
Output is correct |
7 |
Correct |
24 ms |
35712 KB |
Output is correct |
8 |
Correct |
23 ms |
35712 KB |
Output is correct |
9 |
Correct |
25 ms |
35712 KB |
Output is correct |
10 |
Correct |
23 ms |
35712 KB |
Output is correct |
11 |
Correct |
23 ms |
35840 KB |
Output is correct |
12 |
Correct |
23 ms |
35712 KB |
Output is correct |
13 |
Correct |
24 ms |
35712 KB |
Output is correct |
14 |
Correct |
23 ms |
35712 KB |
Output is correct |
15 |
Correct |
30 ms |
35712 KB |
Output is correct |
16 |
Correct |
23 ms |
35712 KB |
Output is correct |
17 |
Correct |
26 ms |
35712 KB |
Output is correct |
18 |
Correct |
24 ms |
35712 KB |
Output is correct |
19 |
Correct |
23 ms |
35712 KB |
Output is correct |
20 |
Correct |
23 ms |
35712 KB |
Output is correct |
21 |
Correct |
23 ms |
35712 KB |
Output is correct |
22 |
Correct |
24 ms |
35712 KB |
Output is correct |
23 |
Correct |
23 ms |
35712 KB |
Output is correct |
24 |
Correct |
23 ms |
35712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
35712 KB |
Output is correct |
2 |
Correct |
24 ms |
35712 KB |
Output is correct |
3 |
Correct |
26 ms |
35840 KB |
Output is correct |
4 |
Correct |
23 ms |
35712 KB |
Output is correct |
5 |
Correct |
24 ms |
35752 KB |
Output is correct |
6 |
Correct |
23 ms |
35712 KB |
Output is correct |
7 |
Correct |
24 ms |
35712 KB |
Output is correct |
8 |
Correct |
23 ms |
35712 KB |
Output is correct |
9 |
Correct |
25 ms |
35712 KB |
Output is correct |
10 |
Correct |
23 ms |
35712 KB |
Output is correct |
11 |
Correct |
23 ms |
35840 KB |
Output is correct |
12 |
Correct |
23 ms |
35712 KB |
Output is correct |
13 |
Correct |
24 ms |
35712 KB |
Output is correct |
14 |
Correct |
23 ms |
35712 KB |
Output is correct |
15 |
Correct |
30 ms |
35712 KB |
Output is correct |
16 |
Correct |
23 ms |
35712 KB |
Output is correct |
17 |
Correct |
26 ms |
35712 KB |
Output is correct |
18 |
Correct |
24 ms |
35712 KB |
Output is correct |
19 |
Correct |
23 ms |
35712 KB |
Output is correct |
20 |
Correct |
23 ms |
35712 KB |
Output is correct |
21 |
Correct |
23 ms |
35712 KB |
Output is correct |
22 |
Correct |
24 ms |
35712 KB |
Output is correct |
23 |
Correct |
23 ms |
35712 KB |
Output is correct |
24 |
Correct |
23 ms |
35712 KB |
Output is correct |
25 |
Correct |
24 ms |
35840 KB |
Output is correct |
26 |
Correct |
25 ms |
36224 KB |
Output is correct |
27 |
Correct |
27 ms |
36224 KB |
Output is correct |
28 |
Correct |
25 ms |
36352 KB |
Output is correct |
29 |
Correct |
25 ms |
36480 KB |
Output is correct |
30 |
Correct |
25 ms |
36096 KB |
Output is correct |
31 |
Correct |
24 ms |
35712 KB |
Output is correct |
32 |
Correct |
26 ms |
36352 KB |
Output is correct |
33 |
Correct |
23 ms |
35712 KB |
Output is correct |
34 |
Correct |
24 ms |
36096 KB |
Output is correct |
35 |
Correct |
26 ms |
36480 KB |
Output is correct |
36 |
Correct |
25 ms |
36096 KB |
Output is correct |
37 |
Correct |
26 ms |
36224 KB |
Output is correct |
38 |
Correct |
24 ms |
35840 KB |
Output is correct |
39 |
Correct |
26 ms |
36224 KB |
Output is correct |
40 |
Correct |
26 ms |
36224 KB |
Output is correct |
41 |
Correct |
25 ms |
36224 KB |
Output is correct |
42 |
Correct |
25 ms |
36224 KB |
Output is correct |
43 |
Correct |
26 ms |
36224 KB |
Output is correct |
44 |
Correct |
23 ms |
35712 KB |
Output is correct |
45 |
Correct |
25 ms |
36224 KB |
Output is correct |
46 |
Correct |
25 ms |
36224 KB |
Output is correct |
47 |
Correct |
23 ms |
35704 KB |
Output is correct |
48 |
Correct |
26 ms |
36224 KB |
Output is correct |
49 |
Correct |
26 ms |
36480 KB |
Output is correct |
50 |
Correct |
26 ms |
36600 KB |
Output is correct |
51 |
Correct |
25 ms |
36224 KB |
Output is correct |
52 |
Correct |
25 ms |
36224 KB |
Output is correct |
53 |
Correct |
25 ms |
36224 KB |
Output is correct |
54 |
Correct |
26 ms |
36216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
35712 KB |
Output is correct |
2 |
Correct |
24 ms |
35712 KB |
Output is correct |
3 |
Correct |
26 ms |
35840 KB |
Output is correct |
4 |
Correct |
23 ms |
35712 KB |
Output is correct |
5 |
Correct |
24 ms |
35752 KB |
Output is correct |
6 |
Correct |
23 ms |
35712 KB |
Output is correct |
7 |
Correct |
24 ms |
35712 KB |
Output is correct |
8 |
Correct |
23 ms |
35712 KB |
Output is correct |
9 |
Correct |
25 ms |
35712 KB |
Output is correct |
10 |
Correct |
23 ms |
35712 KB |
Output is correct |
11 |
Correct |
23 ms |
35840 KB |
Output is correct |
12 |
Correct |
23 ms |
35712 KB |
Output is correct |
13 |
Correct |
24 ms |
35712 KB |
Output is correct |
14 |
Correct |
23 ms |
35712 KB |
Output is correct |
15 |
Correct |
30 ms |
35712 KB |
Output is correct |
16 |
Correct |
23 ms |
35712 KB |
Output is correct |
17 |
Correct |
26 ms |
35712 KB |
Output is correct |
18 |
Correct |
24 ms |
35712 KB |
Output is correct |
19 |
Correct |
23 ms |
35712 KB |
Output is correct |
20 |
Correct |
23 ms |
35712 KB |
Output is correct |
21 |
Correct |
23 ms |
35712 KB |
Output is correct |
22 |
Correct |
24 ms |
35712 KB |
Output is correct |
23 |
Correct |
23 ms |
35712 KB |
Output is correct |
24 |
Correct |
23 ms |
35712 KB |
Output is correct |
25 |
Correct |
23 ms |
35712 KB |
Output is correct |
26 |
Correct |
91 ms |
49520 KB |
Output is correct |
27 |
Correct |
137 ms |
49144 KB |
Output is correct |
28 |
Correct |
25 ms |
36096 KB |
Output is correct |
29 |
Correct |
24 ms |
35712 KB |
Output is correct |
30 |
Correct |
24 ms |
35840 KB |
Output is correct |
31 |
Correct |
146 ms |
49144 KB |
Output is correct |
32 |
Correct |
25 ms |
36352 KB |
Output is correct |
33 |
Correct |
151 ms |
53368 KB |
Output is correct |
34 |
Correct |
126 ms |
49144 KB |
Output is correct |
35 |
Correct |
25 ms |
36224 KB |
Output is correct |
36 |
Correct |
147 ms |
49528 KB |
Output is correct |
37 |
Correct |
25 ms |
36096 KB |
Output is correct |
38 |
Correct |
25 ms |
36096 KB |
Output is correct |
39 |
Correct |
90 ms |
49392 KB |
Output is correct |
40 |
Correct |
26 ms |
36224 KB |
Output is correct |
41 |
Correct |
134 ms |
49272 KB |
Output is correct |
42 |
Correct |
189 ms |
50552 KB |
Output is correct |
43 |
Correct |
23 ms |
35712 KB |
Output is correct |
44 |
Correct |
149 ms |
53624 KB |
Output is correct |
45 |
Correct |
161 ms |
51412 KB |
Output is correct |
46 |
Correct |
25 ms |
36096 KB |
Output is correct |
47 |
Correct |
25 ms |
36096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
49392 KB |
Output is correct |
2 |
Correct |
157 ms |
59248 KB |
Output is correct |
3 |
Correct |
28 ms |
36352 KB |
Output is correct |
4 |
Correct |
28 ms |
36224 KB |
Output is correct |
5 |
Correct |
23 ms |
35712 KB |
Output is correct |
6 |
Correct |
23 ms |
35712 KB |
Output is correct |
7 |
Correct |
25 ms |
36224 KB |
Output is correct |
8 |
Correct |
149 ms |
50296 KB |
Output is correct |
9 |
Correct |
25 ms |
36224 KB |
Output is correct |
10 |
Correct |
126 ms |
50808 KB |
Output is correct |
11 |
Correct |
23 ms |
35704 KB |
Output is correct |
12 |
Correct |
168 ms |
50808 KB |
Output is correct |
13 |
Correct |
131 ms |
52860 KB |
Output is correct |
14 |
Correct |
169 ms |
60600 KB |
Output is correct |
15 |
Correct |
91 ms |
50896 KB |
Output is correct |
16 |
Correct |
26 ms |
36224 KB |
Output is correct |
17 |
Correct |
26 ms |
35840 KB |
Output is correct |
18 |
Correct |
148 ms |
59120 KB |
Output is correct |
19 |
Correct |
176 ms |
63096 KB |
Output is correct |
20 |
Correct |
28 ms |
36216 KB |
Output is correct |
21 |
Correct |
31 ms |
35712 KB |
Output is correct |
22 |
Correct |
107 ms |
52976 KB |
Output is correct |
23 |
Correct |
26 ms |
36224 KB |
Output is correct |
24 |
Correct |
133 ms |
51832 KB |
Output is correct |
25 |
Correct |
165 ms |
62228 KB |
Output is correct |
26 |
Correct |
27 ms |
36480 KB |
Output is correct |
27 |
Correct |
26 ms |
36480 KB |
Output is correct |
28 |
Correct |
30 ms |
36224 KB |
Output is correct |
29 |
Correct |
26 ms |
36224 KB |
Output is correct |
30 |
Correct |
26 ms |
36352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
35712 KB |
Output is correct |
2 |
Correct |
24 ms |
35712 KB |
Output is correct |
3 |
Correct |
26 ms |
35840 KB |
Output is correct |
4 |
Correct |
23 ms |
35712 KB |
Output is correct |
5 |
Correct |
24 ms |
35752 KB |
Output is correct |
6 |
Correct |
23 ms |
35712 KB |
Output is correct |
7 |
Correct |
24 ms |
35712 KB |
Output is correct |
8 |
Correct |
23 ms |
35712 KB |
Output is correct |
9 |
Correct |
25 ms |
35712 KB |
Output is correct |
10 |
Correct |
23 ms |
35712 KB |
Output is correct |
11 |
Correct |
23 ms |
35840 KB |
Output is correct |
12 |
Correct |
23 ms |
35712 KB |
Output is correct |
13 |
Correct |
24 ms |
35712 KB |
Output is correct |
14 |
Correct |
23 ms |
35712 KB |
Output is correct |
15 |
Correct |
30 ms |
35712 KB |
Output is correct |
16 |
Correct |
23 ms |
35712 KB |
Output is correct |
17 |
Correct |
26 ms |
35712 KB |
Output is correct |
18 |
Correct |
24 ms |
35712 KB |
Output is correct |
19 |
Correct |
23 ms |
35712 KB |
Output is correct |
20 |
Correct |
23 ms |
35712 KB |
Output is correct |
21 |
Correct |
23 ms |
35712 KB |
Output is correct |
22 |
Correct |
24 ms |
35712 KB |
Output is correct |
23 |
Correct |
23 ms |
35712 KB |
Output is correct |
24 |
Correct |
23 ms |
35712 KB |
Output is correct |
25 |
Correct |
24 ms |
35840 KB |
Output is correct |
26 |
Correct |
25 ms |
36224 KB |
Output is correct |
27 |
Correct |
27 ms |
36224 KB |
Output is correct |
28 |
Correct |
25 ms |
36352 KB |
Output is correct |
29 |
Correct |
25 ms |
36480 KB |
Output is correct |
30 |
Correct |
25 ms |
36096 KB |
Output is correct |
31 |
Correct |
24 ms |
35712 KB |
Output is correct |
32 |
Correct |
26 ms |
36352 KB |
Output is correct |
33 |
Correct |
23 ms |
35712 KB |
Output is correct |
34 |
Correct |
24 ms |
36096 KB |
Output is correct |
35 |
Correct |
26 ms |
36480 KB |
Output is correct |
36 |
Correct |
25 ms |
36096 KB |
Output is correct |
37 |
Correct |
26 ms |
36224 KB |
Output is correct |
38 |
Correct |
24 ms |
35840 KB |
Output is correct |
39 |
Correct |
26 ms |
36224 KB |
Output is correct |
40 |
Correct |
26 ms |
36224 KB |
Output is correct |
41 |
Correct |
25 ms |
36224 KB |
Output is correct |
42 |
Correct |
25 ms |
36224 KB |
Output is correct |
43 |
Correct |
26 ms |
36224 KB |
Output is correct |
44 |
Correct |
23 ms |
35712 KB |
Output is correct |
45 |
Correct |
25 ms |
36224 KB |
Output is correct |
46 |
Correct |
25 ms |
36224 KB |
Output is correct |
47 |
Correct |
23 ms |
35704 KB |
Output is correct |
48 |
Correct |
26 ms |
36224 KB |
Output is correct |
49 |
Correct |
26 ms |
36480 KB |
Output is correct |
50 |
Correct |
26 ms |
36600 KB |
Output is correct |
51 |
Correct |
25 ms |
36224 KB |
Output is correct |
52 |
Correct |
25 ms |
36224 KB |
Output is correct |
53 |
Correct |
25 ms |
36224 KB |
Output is correct |
54 |
Correct |
26 ms |
36216 KB |
Output is correct |
55 |
Correct |
23 ms |
35712 KB |
Output is correct |
56 |
Correct |
91 ms |
49520 KB |
Output is correct |
57 |
Correct |
137 ms |
49144 KB |
Output is correct |
58 |
Correct |
25 ms |
36096 KB |
Output is correct |
59 |
Correct |
24 ms |
35712 KB |
Output is correct |
60 |
Correct |
24 ms |
35840 KB |
Output is correct |
61 |
Correct |
146 ms |
49144 KB |
Output is correct |
62 |
Correct |
25 ms |
36352 KB |
Output is correct |
63 |
Correct |
151 ms |
53368 KB |
Output is correct |
64 |
Correct |
126 ms |
49144 KB |
Output is correct |
65 |
Correct |
25 ms |
36224 KB |
Output is correct |
66 |
Correct |
147 ms |
49528 KB |
Output is correct |
67 |
Correct |
25 ms |
36096 KB |
Output is correct |
68 |
Correct |
25 ms |
36096 KB |
Output is correct |
69 |
Correct |
90 ms |
49392 KB |
Output is correct |
70 |
Correct |
26 ms |
36224 KB |
Output is correct |
71 |
Correct |
134 ms |
49272 KB |
Output is correct |
72 |
Correct |
189 ms |
50552 KB |
Output is correct |
73 |
Correct |
23 ms |
35712 KB |
Output is correct |
74 |
Correct |
149 ms |
53624 KB |
Output is correct |
75 |
Correct |
161 ms |
51412 KB |
Output is correct |
76 |
Correct |
25 ms |
36096 KB |
Output is correct |
77 |
Correct |
25 ms |
36096 KB |
Output is correct |
78 |
Correct |
87 ms |
49392 KB |
Output is correct |
79 |
Correct |
157 ms |
59248 KB |
Output is correct |
80 |
Correct |
28 ms |
36352 KB |
Output is correct |
81 |
Correct |
28 ms |
36224 KB |
Output is correct |
82 |
Correct |
23 ms |
35712 KB |
Output is correct |
83 |
Correct |
23 ms |
35712 KB |
Output is correct |
84 |
Correct |
25 ms |
36224 KB |
Output is correct |
85 |
Correct |
149 ms |
50296 KB |
Output is correct |
86 |
Correct |
25 ms |
36224 KB |
Output is correct |
87 |
Correct |
126 ms |
50808 KB |
Output is correct |
88 |
Correct |
23 ms |
35704 KB |
Output is correct |
89 |
Correct |
168 ms |
50808 KB |
Output is correct |
90 |
Correct |
131 ms |
52860 KB |
Output is correct |
91 |
Correct |
169 ms |
60600 KB |
Output is correct |
92 |
Correct |
91 ms |
50896 KB |
Output is correct |
93 |
Correct |
26 ms |
36224 KB |
Output is correct |
94 |
Correct |
26 ms |
35840 KB |
Output is correct |
95 |
Correct |
148 ms |
59120 KB |
Output is correct |
96 |
Correct |
176 ms |
63096 KB |
Output is correct |
97 |
Correct |
28 ms |
36216 KB |
Output is correct |
98 |
Correct |
31 ms |
35712 KB |
Output is correct |
99 |
Correct |
107 ms |
52976 KB |
Output is correct |
100 |
Correct |
26 ms |
36224 KB |
Output is correct |
101 |
Correct |
133 ms |
51832 KB |
Output is correct |
102 |
Correct |
165 ms |
62228 KB |
Output is correct |
103 |
Correct |
27 ms |
36480 KB |
Output is correct |
104 |
Correct |
26 ms |
36480 KB |
Output is correct |
105 |
Correct |
30 ms |
36224 KB |
Output is correct |
106 |
Correct |
26 ms |
36224 KB |
Output is correct |
107 |
Correct |
26 ms |
36352 KB |
Output is correct |
108 |
Correct |
621 ms |
119012 KB |
Output is correct |
109 |
Correct |
1465 ms |
121044 KB |
Output is correct |
110 |
Correct |
1511 ms |
127608 KB |
Output is correct |
111 |
Correct |
993 ms |
171640 KB |
Output is correct |
112 |
Correct |
976 ms |
147404 KB |
Output is correct |
113 |
Correct |
976 ms |
153692 KB |
Output is correct |
114 |
Correct |
880 ms |
110344 KB |
Output is correct |
115 |
Correct |
878 ms |
110328 KB |
Output is correct |
116 |
Correct |
988 ms |
112736 KB |
Output is correct |
117 |
Correct |
993 ms |
161624 KB |
Output is correct |
118 |
Correct |
938 ms |
109816 KB |
Output is correct |
119 |
Correct |
1041 ms |
161656 KB |
Output is correct |
120 |
Correct |
1050 ms |
169664 KB |
Output is correct |
121 |
Correct |
1027 ms |
161400 KB |
Output is correct |
122 |
Correct |
971 ms |
121848 KB |
Output is correct |
123 |
Correct |
1110 ms |
163172 KB |
Output is correct |
124 |
Correct |
921 ms |
142840 KB |
Output is correct |