# include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
//#define int long long
#define pii pair <int, int>
#define pb push_back
const int N = 5e5 + 5;
int t,n,a[N],sz[N],tin,in[N],out[N],p[N],par[N][20],k,x[N],lv[N],deg[N];
vector <int> v[N];
vector <pii> vec[N];
void dfs(int a, int p) {
tin++;
in[a] = tin;
par[a][0] = p;
for (int i = 1; i <= 18; i++) {
if (par[a][i - 1]) par[a][i] = par[par[a][i - 1]][i - 1];
}
lv[a] = lv[p] + 1;
for (int to : v[a]) {
if (to == p) continue;
dfs(to, a);
}
out[a] = tin;
}
void init() {
for (int i = 1; i <= n; i++) {
p[i] = i; sz[i] = 1;
}
}
int get_col(int a) {
if (a == p[a]) return a;
else return p[a] = get_col(p[a]);
}
void col(int a, int b) {
a = get_col(a); b = get_col(b);
if (a == b) return ;
if (lv[a] > lv[b]) swap(a, b);
p[b] = a;
}
void merge(int a, int lc) {
while (get_col(a) != get_col(lc)) {
col(a, par[get_col(a)][0]);
}
}
int upper(int a, int b) {
return (in[a] <= in[b] && out[a] >= out[b]);
}
int lca(int a, int b) {
if (upper(a, b)) return a;
for (int i = 18; i >= 0; i--) {
if (par[a][i] && !upper(par[a][i], b)) a = par[a][i];
}
return par[a][0];
}
main() {
std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n>>k;
vector <pii> edges;
for (int i = 1; i <= n - 1; i++) {
int a, b;
cin>>a>>b;
v[a].pb(b);
v[b].pb(a);
edges.pb({a,b});
}
dfs(1, 0);
for (int i = 1; i <= n; i++) {
cin>>x[i];
}
init();
for (int i = 1; i <= n; i++) {
vec[x[i]].pb({in[i], i});
}
for (int i = 1; i <= k; i++) {
for (int j = 0; j < vec[i].size(); j++) {
int a = vec[i][j].s, b = vec[i][(j + 1) % vec[i].size()].s, lc = lca(a, b);
merge(a, lc); merge(b, lc);
}
}
for (pii sth : edges) {
if (get_col(sth.f) != get_col(sth.s)) {
deg[get_col(sth.f)]++; deg[get_col(sth.s)]++;
}
}
int cnt = 0;
for (int i = 1; i <= n; i++) {
cnt += (int)(deg[i] == 1);
}
cout<<(cnt + 1) / 2<<"\n";
}
Compilation message
mergers.cpp:56:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
56 | main() {
| ^~~~
mergers.cpp: In function 'int main()':
mergers.cpp:76:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for (int j = 0; j < vec[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23836 KB |
Output is correct |
3 |
Correct |
15 ms |
23824 KB |
Output is correct |
4 |
Correct |
14 ms |
23748 KB |
Output is correct |
5 |
Correct |
14 ms |
23808 KB |
Output is correct |
6 |
Correct |
14 ms |
23828 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
16 ms |
23856 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23812 KB |
Output is correct |
11 |
Correct |
14 ms |
23856 KB |
Output is correct |
12 |
Correct |
13 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23804 KB |
Output is correct |
14 |
Correct |
13 ms |
23876 KB |
Output is correct |
15 |
Correct |
14 ms |
23864 KB |
Output is correct |
16 |
Correct |
13 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
15 ms |
23764 KB |
Output is correct |
19 |
Correct |
14 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
13 ms |
23764 KB |
Output is correct |
22 |
Correct |
13 ms |
23764 KB |
Output is correct |
23 |
Correct |
12 ms |
23764 KB |
Output is correct |
24 |
Correct |
13 ms |
23876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23836 KB |
Output is correct |
3 |
Correct |
15 ms |
23824 KB |
Output is correct |
4 |
Correct |
14 ms |
23748 KB |
Output is correct |
5 |
Correct |
14 ms |
23808 KB |
Output is correct |
6 |
Correct |
14 ms |
23828 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
16 ms |
23856 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23812 KB |
Output is correct |
11 |
Correct |
14 ms |
23856 KB |
Output is correct |
12 |
Correct |
13 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23804 KB |
Output is correct |
14 |
Correct |
13 ms |
23876 KB |
Output is correct |
15 |
Correct |
14 ms |
23864 KB |
Output is correct |
16 |
Correct |
13 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
15 ms |
23764 KB |
Output is correct |
19 |
Correct |
14 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
13 ms |
23764 KB |
Output is correct |
22 |
Correct |
13 ms |
23764 KB |
Output is correct |
23 |
Correct |
12 ms |
23764 KB |
Output is correct |
24 |
Correct |
13 ms |
23876 KB |
Output is correct |
25 |
Correct |
13 ms |
23764 KB |
Output is correct |
26 |
Correct |
14 ms |
24384 KB |
Output is correct |
27 |
Correct |
13 ms |
24312 KB |
Output is correct |
28 |
Correct |
14 ms |
24404 KB |
Output is correct |
29 |
Correct |
16 ms |
24352 KB |
Output is correct |
30 |
Correct |
14 ms |
24276 KB |
Output is correct |
31 |
Correct |
12 ms |
23764 KB |
Output is correct |
32 |
Correct |
15 ms |
24400 KB |
Output is correct |
33 |
Correct |
12 ms |
23764 KB |
Output is correct |
34 |
Correct |
15 ms |
24316 KB |
Output is correct |
35 |
Correct |
15 ms |
24336 KB |
Output is correct |
36 |
Correct |
16 ms |
24412 KB |
Output is correct |
37 |
Correct |
17 ms |
24292 KB |
Output is correct |
38 |
Correct |
12 ms |
23768 KB |
Output is correct |
39 |
Correct |
19 ms |
24244 KB |
Output is correct |
40 |
Correct |
14 ms |
24276 KB |
Output is correct |
41 |
Correct |
17 ms |
24344 KB |
Output is correct |
42 |
Correct |
14 ms |
24300 KB |
Output is correct |
43 |
Correct |
14 ms |
24428 KB |
Output is correct |
44 |
Correct |
12 ms |
23764 KB |
Output is correct |
45 |
Correct |
14 ms |
24428 KB |
Output is correct |
46 |
Correct |
14 ms |
24356 KB |
Output is correct |
47 |
Correct |
14 ms |
23764 KB |
Output is correct |
48 |
Correct |
14 ms |
24276 KB |
Output is correct |
49 |
Correct |
14 ms |
24428 KB |
Output is correct |
50 |
Correct |
19 ms |
24404 KB |
Output is correct |
51 |
Correct |
16 ms |
24256 KB |
Output is correct |
52 |
Correct |
16 ms |
24276 KB |
Output is correct |
53 |
Correct |
14 ms |
24388 KB |
Output is correct |
54 |
Correct |
14 ms |
24276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23836 KB |
Output is correct |
3 |
Correct |
15 ms |
23824 KB |
Output is correct |
4 |
Correct |
14 ms |
23748 KB |
Output is correct |
5 |
Correct |
14 ms |
23808 KB |
Output is correct |
6 |
Correct |
14 ms |
23828 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
16 ms |
23856 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23812 KB |
Output is correct |
11 |
Correct |
14 ms |
23856 KB |
Output is correct |
12 |
Correct |
13 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23804 KB |
Output is correct |
14 |
Correct |
13 ms |
23876 KB |
Output is correct |
15 |
Correct |
14 ms |
23864 KB |
Output is correct |
16 |
Correct |
13 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
15 ms |
23764 KB |
Output is correct |
19 |
Correct |
14 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
13 ms |
23764 KB |
Output is correct |
22 |
Correct |
13 ms |
23764 KB |
Output is correct |
23 |
Correct |
12 ms |
23764 KB |
Output is correct |
24 |
Correct |
13 ms |
23876 KB |
Output is correct |
25 |
Correct |
11 ms |
23764 KB |
Output is correct |
26 |
Correct |
72 ms |
39376 KB |
Output is correct |
27 |
Correct |
110 ms |
39124 KB |
Output is correct |
28 |
Correct |
15 ms |
24276 KB |
Output is correct |
29 |
Correct |
14 ms |
23880 KB |
Output is correct |
30 |
Correct |
13 ms |
23764 KB |
Output is correct |
31 |
Correct |
100 ms |
39120 KB |
Output is correct |
32 |
Correct |
15 ms |
24276 KB |
Output is correct |
33 |
Correct |
139 ms |
43224 KB |
Output is correct |
34 |
Correct |
111 ms |
39092 KB |
Output is correct |
35 |
Correct |
15 ms |
24344 KB |
Output is correct |
36 |
Correct |
126 ms |
39504 KB |
Output is correct |
37 |
Correct |
15 ms |
24276 KB |
Output is correct |
38 |
Correct |
15 ms |
24276 KB |
Output is correct |
39 |
Correct |
75 ms |
39352 KB |
Output is correct |
40 |
Correct |
14 ms |
24428 KB |
Output is correct |
41 |
Correct |
101 ms |
39568 KB |
Output is correct |
42 |
Correct |
154 ms |
40468 KB |
Output is correct |
43 |
Correct |
14 ms |
23764 KB |
Output is correct |
44 |
Correct |
159 ms |
44028 KB |
Output is correct |
45 |
Correct |
171 ms |
41224 KB |
Output is correct |
46 |
Correct |
15 ms |
24276 KB |
Output is correct |
47 |
Correct |
18 ms |
24276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
39396 KB |
Output is correct |
2 |
Correct |
85 ms |
41792 KB |
Output is correct |
3 |
Correct |
16 ms |
24280 KB |
Output is correct |
4 |
Correct |
16 ms |
24276 KB |
Output is correct |
5 |
Correct |
13 ms |
23764 KB |
Output is correct |
6 |
Correct |
14 ms |
23780 KB |
Output is correct |
7 |
Correct |
15 ms |
24248 KB |
Output is correct |
8 |
Correct |
143 ms |
40268 KB |
Output is correct |
9 |
Correct |
14 ms |
24236 KB |
Output is correct |
10 |
Correct |
123 ms |
39484 KB |
Output is correct |
11 |
Correct |
13 ms |
23840 KB |
Output is correct |
12 |
Correct |
103 ms |
39424 KB |
Output is correct |
13 |
Correct |
97 ms |
40132 KB |
Output is correct |
14 |
Correct |
91 ms |
41504 KB |
Output is correct |
15 |
Correct |
77 ms |
39344 KB |
Output is correct |
16 |
Correct |
17 ms |
24276 KB |
Output is correct |
17 |
Correct |
15 ms |
23764 KB |
Output is correct |
18 |
Correct |
93 ms |
41528 KB |
Output is correct |
19 |
Correct |
106 ms |
43776 KB |
Output is correct |
20 |
Correct |
15 ms |
24276 KB |
Output is correct |
21 |
Correct |
15 ms |
23888 KB |
Output is correct |
22 |
Correct |
90 ms |
40540 KB |
Output is correct |
23 |
Correct |
15 ms |
24296 KB |
Output is correct |
24 |
Correct |
109 ms |
39824 KB |
Output is correct |
25 |
Correct |
122 ms |
42988 KB |
Output is correct |
26 |
Correct |
14 ms |
24384 KB |
Output is correct |
27 |
Correct |
15 ms |
24404 KB |
Output is correct |
28 |
Correct |
15 ms |
24364 KB |
Output is correct |
29 |
Correct |
15 ms |
24276 KB |
Output is correct |
30 |
Correct |
16 ms |
24372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23836 KB |
Output is correct |
3 |
Correct |
15 ms |
23824 KB |
Output is correct |
4 |
Correct |
14 ms |
23748 KB |
Output is correct |
5 |
Correct |
14 ms |
23808 KB |
Output is correct |
6 |
Correct |
14 ms |
23828 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
16 ms |
23856 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23812 KB |
Output is correct |
11 |
Correct |
14 ms |
23856 KB |
Output is correct |
12 |
Correct |
13 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23804 KB |
Output is correct |
14 |
Correct |
13 ms |
23876 KB |
Output is correct |
15 |
Correct |
14 ms |
23864 KB |
Output is correct |
16 |
Correct |
13 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
15 ms |
23764 KB |
Output is correct |
19 |
Correct |
14 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
13 ms |
23764 KB |
Output is correct |
22 |
Correct |
13 ms |
23764 KB |
Output is correct |
23 |
Correct |
12 ms |
23764 KB |
Output is correct |
24 |
Correct |
13 ms |
23876 KB |
Output is correct |
25 |
Correct |
13 ms |
23764 KB |
Output is correct |
26 |
Correct |
14 ms |
24384 KB |
Output is correct |
27 |
Correct |
13 ms |
24312 KB |
Output is correct |
28 |
Correct |
14 ms |
24404 KB |
Output is correct |
29 |
Correct |
16 ms |
24352 KB |
Output is correct |
30 |
Correct |
14 ms |
24276 KB |
Output is correct |
31 |
Correct |
12 ms |
23764 KB |
Output is correct |
32 |
Correct |
15 ms |
24400 KB |
Output is correct |
33 |
Correct |
12 ms |
23764 KB |
Output is correct |
34 |
Correct |
15 ms |
24316 KB |
Output is correct |
35 |
Correct |
15 ms |
24336 KB |
Output is correct |
36 |
Correct |
16 ms |
24412 KB |
Output is correct |
37 |
Correct |
17 ms |
24292 KB |
Output is correct |
38 |
Correct |
12 ms |
23768 KB |
Output is correct |
39 |
Correct |
19 ms |
24244 KB |
Output is correct |
40 |
Correct |
14 ms |
24276 KB |
Output is correct |
41 |
Correct |
17 ms |
24344 KB |
Output is correct |
42 |
Correct |
14 ms |
24300 KB |
Output is correct |
43 |
Correct |
14 ms |
24428 KB |
Output is correct |
44 |
Correct |
12 ms |
23764 KB |
Output is correct |
45 |
Correct |
14 ms |
24428 KB |
Output is correct |
46 |
Correct |
14 ms |
24356 KB |
Output is correct |
47 |
Correct |
14 ms |
23764 KB |
Output is correct |
48 |
Correct |
14 ms |
24276 KB |
Output is correct |
49 |
Correct |
14 ms |
24428 KB |
Output is correct |
50 |
Correct |
19 ms |
24404 KB |
Output is correct |
51 |
Correct |
16 ms |
24256 KB |
Output is correct |
52 |
Correct |
16 ms |
24276 KB |
Output is correct |
53 |
Correct |
14 ms |
24388 KB |
Output is correct |
54 |
Correct |
14 ms |
24276 KB |
Output is correct |
55 |
Correct |
11 ms |
23764 KB |
Output is correct |
56 |
Correct |
72 ms |
39376 KB |
Output is correct |
57 |
Correct |
110 ms |
39124 KB |
Output is correct |
58 |
Correct |
15 ms |
24276 KB |
Output is correct |
59 |
Correct |
14 ms |
23880 KB |
Output is correct |
60 |
Correct |
13 ms |
23764 KB |
Output is correct |
61 |
Correct |
100 ms |
39120 KB |
Output is correct |
62 |
Correct |
15 ms |
24276 KB |
Output is correct |
63 |
Correct |
139 ms |
43224 KB |
Output is correct |
64 |
Correct |
111 ms |
39092 KB |
Output is correct |
65 |
Correct |
15 ms |
24344 KB |
Output is correct |
66 |
Correct |
126 ms |
39504 KB |
Output is correct |
67 |
Correct |
15 ms |
24276 KB |
Output is correct |
68 |
Correct |
15 ms |
24276 KB |
Output is correct |
69 |
Correct |
75 ms |
39352 KB |
Output is correct |
70 |
Correct |
14 ms |
24428 KB |
Output is correct |
71 |
Correct |
101 ms |
39568 KB |
Output is correct |
72 |
Correct |
154 ms |
40468 KB |
Output is correct |
73 |
Correct |
14 ms |
23764 KB |
Output is correct |
74 |
Correct |
159 ms |
44028 KB |
Output is correct |
75 |
Correct |
171 ms |
41224 KB |
Output is correct |
76 |
Correct |
15 ms |
24276 KB |
Output is correct |
77 |
Correct |
18 ms |
24276 KB |
Output is correct |
78 |
Correct |
96 ms |
39396 KB |
Output is correct |
79 |
Correct |
85 ms |
41792 KB |
Output is correct |
80 |
Correct |
16 ms |
24280 KB |
Output is correct |
81 |
Correct |
16 ms |
24276 KB |
Output is correct |
82 |
Correct |
13 ms |
23764 KB |
Output is correct |
83 |
Correct |
14 ms |
23780 KB |
Output is correct |
84 |
Correct |
15 ms |
24248 KB |
Output is correct |
85 |
Correct |
143 ms |
40268 KB |
Output is correct |
86 |
Correct |
14 ms |
24236 KB |
Output is correct |
87 |
Correct |
123 ms |
39484 KB |
Output is correct |
88 |
Correct |
13 ms |
23840 KB |
Output is correct |
89 |
Correct |
103 ms |
39424 KB |
Output is correct |
90 |
Correct |
97 ms |
40132 KB |
Output is correct |
91 |
Correct |
91 ms |
41504 KB |
Output is correct |
92 |
Correct |
77 ms |
39344 KB |
Output is correct |
93 |
Correct |
17 ms |
24276 KB |
Output is correct |
94 |
Correct |
15 ms |
23764 KB |
Output is correct |
95 |
Correct |
93 ms |
41528 KB |
Output is correct |
96 |
Correct |
106 ms |
43776 KB |
Output is correct |
97 |
Correct |
15 ms |
24276 KB |
Output is correct |
98 |
Correct |
15 ms |
23888 KB |
Output is correct |
99 |
Correct |
90 ms |
40540 KB |
Output is correct |
100 |
Correct |
15 ms |
24296 KB |
Output is correct |
101 |
Correct |
109 ms |
39824 KB |
Output is correct |
102 |
Correct |
122 ms |
42988 KB |
Output is correct |
103 |
Correct |
14 ms |
24384 KB |
Output is correct |
104 |
Correct |
15 ms |
24404 KB |
Output is correct |
105 |
Correct |
15 ms |
24364 KB |
Output is correct |
106 |
Correct |
15 ms |
24276 KB |
Output is correct |
107 |
Correct |
16 ms |
24372 KB |
Output is correct |
108 |
Correct |
557 ms |
113800 KB |
Output is correct |
109 |
Correct |
1189 ms |
120272 KB |
Output is correct |
110 |
Correct |
1259 ms |
126004 KB |
Output is correct |
111 |
Correct |
873 ms |
135944 KB |
Output is correct |
112 |
Correct |
944 ms |
130180 KB |
Output is correct |
113 |
Correct |
607 ms |
122308 KB |
Output is correct |
114 |
Correct |
617 ms |
107792 KB |
Output is correct |
115 |
Correct |
628 ms |
108580 KB |
Output is correct |
116 |
Correct |
841 ms |
113492 KB |
Output is correct |
117 |
Correct |
650 ms |
122256 KB |
Output is correct |
118 |
Correct |
673 ms |
109432 KB |
Output is correct |
119 |
Correct |
691 ms |
122260 KB |
Output is correct |
120 |
Correct |
789 ms |
130432 KB |
Output is correct |
121 |
Correct |
662 ms |
122300 KB |
Output is correct |
122 |
Correct |
778 ms |
116724 KB |
Output is correct |
123 |
Correct |
543 ms |
123908 KB |
Output is correct |
124 |
Correct |
792 ms |
120036 KB |
Output is correct |