#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long ll;
const int N = 5e5+5;
int n, m, ans, cur, s[N], dep[N], sp[N][20], sub[N], ret[N], mark[N], acc[N], cnt[N];
vector<int> adj[N], vec[N];
void pre(int u, int p)
{
sub[u] = 1;
dep[u] = dep[p]+1;
sp[u][0] = p;
for(int i=1; i<20; i++) sp[u][i] = sp[sp[u][i-1]][i-1];
for(int v : adj[u]) if(v != p) pre(v, u), sub[u] += sub[v];
}
int equate(int u, int v)
{
int c = dep[u]-dep[v];
for(int i=0; i<20; i++) if(c & (1<<i)) u = sp[u][i];
return u;
}
int LCA(int u, int v)
{
if(dep[u] < dep[v]) swap(u, v);
u = equate(u, v);
if(u == v) return u;
for(int i=19; i>=0; i--) if(sp[u][i] != sp[v][i]) u = sp[u][i], v = sp[v][i];
return sp[u][0];
}
void calc(int u, int p)
{
for(auto v : adj[u]) if(v != p) calc(v, u), ret[u] += ret[v];
if(ret[u] == sub[u]) mark[u] = 1;
}
void DFS(int u, int p)
{
for(auto v : adj[u]) if(v != p) DFS(v, u), acc[u] += acc[v];
if(mark[u] && !acc[u]) cnt[u]=1;
acc[u] += mark[u];
}
int main()
{
scanf("%d %d", &n, &m);
memset(sp, -1, sizeof sp);
for(int i=1, a, b; i<n; i++) scanf("%d %d", &a, &b),
adj[a].push_back(b), adj[b].push_back(a);
for(int i=1; i<=n; i++) scanf("%d", &s[i]), vec[s[i]].push_back(i);
pre(1, 0);
for(int i=1; i<=m; i++)
{
if(vec[i].empty()) continue;
int node = vec[i][0];
for(auto u : vec[i]) node = LCA(node, u);
ret[node] += vec[i].size();
}
if(n == 1) return cout << 0 << endl, 0;
calc(1, 0);
mark[1] = 0;
DFS(1, 0);
if(m > 1)
{
memset(sp, -1, sizeof sp);
memset(mark, 0, sizeof mark);
memset(dep, 0, sizeof dep);
memset(ret, 0, sizeof ret);
memset(sub, 0, sizeof sub);
memset(acc, 0, sizeof acc);
int nd;
if(s[1] == 1) nd = vec[2][0];
else nd = vec[1][0];
pre(nd, 0);
for(int i=1; i<=m; i++)
{
if(vec[i].empty()) continue;
int node = vec[i][0];
for(auto u : vec[i]) node = LCA(node, u);
ret[node] += vec[i].size();
}
calc(nd, 0);
mark[nd] = 0;
DFS(nd, 0);
}
for(int i=1; i<=n; i++) ans += cnt[i];
cout << (ans+1)/2 << endl;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:44:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:47:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1, a, b; i<n; i++) scanf("%d %d", &a, &b),
~~~~~~~~~~~~~~~~~~~~~~~~
adj[a].push_back(b), adj[b].push_back(a);
~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
mergers.cpp:48:44: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1; i<=n; i++) scanf("%d", &s[i]), vec[s[i]].push_back(i);
~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
72696 KB |
Output is correct |
2 |
Correct |
72 ms |
72824 KB |
Output is correct |
3 |
Correct |
73 ms |
72696 KB |
Output is correct |
4 |
Correct |
73 ms |
72952 KB |
Output is correct |
5 |
Correct |
72 ms |
72696 KB |
Output is correct |
6 |
Correct |
57 ms |
62968 KB |
Output is correct |
7 |
Correct |
57 ms |
62968 KB |
Output is correct |
8 |
Correct |
72 ms |
72696 KB |
Output is correct |
9 |
Correct |
73 ms |
72740 KB |
Output is correct |
10 |
Correct |
72 ms |
72696 KB |
Output is correct |
11 |
Correct |
72 ms |
72696 KB |
Output is correct |
12 |
Correct |
73 ms |
72680 KB |
Output is correct |
13 |
Correct |
73 ms |
72696 KB |
Output is correct |
14 |
Correct |
72 ms |
72696 KB |
Output is correct |
15 |
Correct |
80 ms |
72724 KB |
Output is correct |
16 |
Correct |
72 ms |
72752 KB |
Output is correct |
17 |
Correct |
73 ms |
72696 KB |
Output is correct |
18 |
Correct |
73 ms |
72824 KB |
Output is correct |
19 |
Correct |
74 ms |
72800 KB |
Output is correct |
20 |
Correct |
72 ms |
72824 KB |
Output is correct |
21 |
Correct |
72 ms |
72696 KB |
Output is correct |
22 |
Correct |
75 ms |
72828 KB |
Output is correct |
23 |
Correct |
73 ms |
72704 KB |
Output is correct |
24 |
Correct |
73 ms |
72760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
72696 KB |
Output is correct |
2 |
Correct |
72 ms |
72824 KB |
Output is correct |
3 |
Correct |
73 ms |
72696 KB |
Output is correct |
4 |
Correct |
73 ms |
72952 KB |
Output is correct |
5 |
Correct |
72 ms |
72696 KB |
Output is correct |
6 |
Correct |
57 ms |
62968 KB |
Output is correct |
7 |
Correct |
57 ms |
62968 KB |
Output is correct |
8 |
Correct |
72 ms |
72696 KB |
Output is correct |
9 |
Correct |
73 ms |
72740 KB |
Output is correct |
10 |
Correct |
72 ms |
72696 KB |
Output is correct |
11 |
Correct |
72 ms |
72696 KB |
Output is correct |
12 |
Correct |
73 ms |
72680 KB |
Output is correct |
13 |
Correct |
73 ms |
72696 KB |
Output is correct |
14 |
Correct |
72 ms |
72696 KB |
Output is correct |
15 |
Correct |
80 ms |
72724 KB |
Output is correct |
16 |
Correct |
72 ms |
72752 KB |
Output is correct |
17 |
Correct |
73 ms |
72696 KB |
Output is correct |
18 |
Correct |
73 ms |
72824 KB |
Output is correct |
19 |
Correct |
74 ms |
72800 KB |
Output is correct |
20 |
Correct |
72 ms |
72824 KB |
Output is correct |
21 |
Correct |
72 ms |
72696 KB |
Output is correct |
22 |
Correct |
75 ms |
72828 KB |
Output is correct |
23 |
Correct |
73 ms |
72704 KB |
Output is correct |
24 |
Correct |
73 ms |
72760 KB |
Output is correct |
25 |
Correct |
73 ms |
72768 KB |
Output is correct |
26 |
Correct |
76 ms |
72924 KB |
Output is correct |
27 |
Correct |
76 ms |
72928 KB |
Output is correct |
28 |
Correct |
75 ms |
72952 KB |
Output is correct |
29 |
Correct |
76 ms |
72952 KB |
Output is correct |
30 |
Correct |
76 ms |
72824 KB |
Output is correct |
31 |
Correct |
72 ms |
72696 KB |
Output is correct |
32 |
Correct |
78 ms |
73116 KB |
Output is correct |
33 |
Correct |
72 ms |
72824 KB |
Output is correct |
34 |
Correct |
75 ms |
72952 KB |
Output is correct |
35 |
Correct |
75 ms |
72952 KB |
Output is correct |
36 |
Correct |
76 ms |
72840 KB |
Output is correct |
37 |
Correct |
77 ms |
73012 KB |
Output is correct |
38 |
Correct |
73 ms |
72828 KB |
Output is correct |
39 |
Correct |
76 ms |
72952 KB |
Output is correct |
40 |
Correct |
59 ms |
63224 KB |
Output is correct |
41 |
Correct |
76 ms |
72952 KB |
Output is correct |
42 |
Correct |
75 ms |
72952 KB |
Output is correct |
43 |
Correct |
59 ms |
63352 KB |
Output is correct |
44 |
Correct |
73 ms |
72824 KB |
Output is correct |
45 |
Correct |
76 ms |
73052 KB |
Output is correct |
46 |
Correct |
76 ms |
72952 KB |
Output is correct |
47 |
Correct |
84 ms |
72752 KB |
Output is correct |
48 |
Correct |
93 ms |
72952 KB |
Output is correct |
49 |
Correct |
75 ms |
72952 KB |
Output is correct |
50 |
Correct |
77 ms |
73080 KB |
Output is correct |
51 |
Correct |
76 ms |
72856 KB |
Output is correct |
52 |
Correct |
74 ms |
72988 KB |
Output is correct |
53 |
Correct |
75 ms |
72916 KB |
Output is correct |
54 |
Correct |
76 ms |
72952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
72696 KB |
Output is correct |
2 |
Correct |
72 ms |
72824 KB |
Output is correct |
3 |
Correct |
73 ms |
72696 KB |
Output is correct |
4 |
Correct |
73 ms |
72952 KB |
Output is correct |
5 |
Correct |
72 ms |
72696 KB |
Output is correct |
6 |
Correct |
57 ms |
62968 KB |
Output is correct |
7 |
Correct |
57 ms |
62968 KB |
Output is correct |
8 |
Correct |
72 ms |
72696 KB |
Output is correct |
9 |
Correct |
73 ms |
72740 KB |
Output is correct |
10 |
Correct |
72 ms |
72696 KB |
Output is correct |
11 |
Correct |
72 ms |
72696 KB |
Output is correct |
12 |
Correct |
73 ms |
72680 KB |
Output is correct |
13 |
Correct |
73 ms |
72696 KB |
Output is correct |
14 |
Correct |
72 ms |
72696 KB |
Output is correct |
15 |
Correct |
80 ms |
72724 KB |
Output is correct |
16 |
Correct |
72 ms |
72752 KB |
Output is correct |
17 |
Correct |
73 ms |
72696 KB |
Output is correct |
18 |
Correct |
73 ms |
72824 KB |
Output is correct |
19 |
Correct |
74 ms |
72800 KB |
Output is correct |
20 |
Correct |
72 ms |
72824 KB |
Output is correct |
21 |
Correct |
72 ms |
72696 KB |
Output is correct |
22 |
Correct |
75 ms |
72828 KB |
Output is correct |
23 |
Correct |
73 ms |
72704 KB |
Output is correct |
24 |
Correct |
73 ms |
72760 KB |
Output is correct |
25 |
Correct |
74 ms |
72816 KB |
Output is correct |
26 |
Correct |
185 ms |
77280 KB |
Output is correct |
27 |
Correct |
233 ms |
76916 KB |
Output is correct |
28 |
Correct |
76 ms |
72828 KB |
Output is correct |
29 |
Correct |
72 ms |
72824 KB |
Output is correct |
30 |
Correct |
72 ms |
72744 KB |
Output is correct |
31 |
Correct |
236 ms |
76940 KB |
Output is correct |
32 |
Correct |
74 ms |
73080 KB |
Output is correct |
33 |
Correct |
326 ms |
82524 KB |
Output is correct |
34 |
Correct |
260 ms |
77060 KB |
Output is correct |
35 |
Correct |
75 ms |
72828 KB |
Output is correct |
36 |
Correct |
297 ms |
77928 KB |
Output is correct |
37 |
Correct |
60 ms |
63224 KB |
Output is correct |
38 |
Correct |
75 ms |
72952 KB |
Output is correct |
39 |
Correct |
180 ms |
77712 KB |
Output is correct |
40 |
Correct |
60 ms |
63352 KB |
Output is correct |
41 |
Correct |
179 ms |
69092 KB |
Output is correct |
42 |
Correct |
310 ms |
80120 KB |
Output is correct |
43 |
Correct |
74 ms |
72696 KB |
Output is correct |
44 |
Correct |
222 ms |
75096 KB |
Output is correct |
45 |
Correct |
288 ms |
80376 KB |
Output is correct |
46 |
Correct |
75 ms |
72952 KB |
Output is correct |
47 |
Correct |
77 ms |
72952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
177 ms |
77252 KB |
Output is correct |
2 |
Correct |
219 ms |
80120 KB |
Output is correct |
3 |
Correct |
76 ms |
72932 KB |
Output is correct |
4 |
Correct |
83 ms |
72896 KB |
Output is correct |
5 |
Correct |
72 ms |
72824 KB |
Output is correct |
6 |
Correct |
72 ms |
72680 KB |
Output is correct |
7 |
Correct |
75 ms |
72952 KB |
Output is correct |
8 |
Correct |
284 ms |
78392 KB |
Output is correct |
9 |
Correct |
76 ms |
72952 KB |
Output is correct |
10 |
Correct |
243 ms |
77304 KB |
Output is correct |
11 |
Correct |
73 ms |
72700 KB |
Output is correct |
12 |
Correct |
299 ms |
77708 KB |
Output is correct |
13 |
Correct |
396 ms |
78712 KB |
Output is correct |
14 |
Correct |
267 ms |
80376 KB |
Output is correct |
15 |
Correct |
182 ms |
77552 KB |
Output is correct |
16 |
Correct |
76 ms |
72952 KB |
Output is correct |
17 |
Correct |
73 ms |
72824 KB |
Output is correct |
18 |
Correct |
210 ms |
80192 KB |
Output is correct |
19 |
Correct |
337 ms |
85496 KB |
Output is correct |
20 |
Correct |
90 ms |
72952 KB |
Output is correct |
21 |
Correct |
78 ms |
72568 KB |
Output is correct |
22 |
Correct |
205 ms |
78960 KB |
Output is correct |
23 |
Correct |
92 ms |
72924 KB |
Output is correct |
24 |
Correct |
304 ms |
78120 KB |
Output is correct |
25 |
Correct |
297 ms |
82428 KB |
Output is correct |
26 |
Correct |
76 ms |
73080 KB |
Output is correct |
27 |
Correct |
76 ms |
73080 KB |
Output is correct |
28 |
Correct |
77 ms |
72924 KB |
Output is correct |
29 |
Correct |
88 ms |
73076 KB |
Output is correct |
30 |
Correct |
75 ms |
72924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
72696 KB |
Output is correct |
2 |
Correct |
72 ms |
72824 KB |
Output is correct |
3 |
Correct |
73 ms |
72696 KB |
Output is correct |
4 |
Correct |
73 ms |
72952 KB |
Output is correct |
5 |
Correct |
72 ms |
72696 KB |
Output is correct |
6 |
Correct |
57 ms |
62968 KB |
Output is correct |
7 |
Correct |
57 ms |
62968 KB |
Output is correct |
8 |
Correct |
72 ms |
72696 KB |
Output is correct |
9 |
Correct |
73 ms |
72740 KB |
Output is correct |
10 |
Correct |
72 ms |
72696 KB |
Output is correct |
11 |
Correct |
72 ms |
72696 KB |
Output is correct |
12 |
Correct |
73 ms |
72680 KB |
Output is correct |
13 |
Correct |
73 ms |
72696 KB |
Output is correct |
14 |
Correct |
72 ms |
72696 KB |
Output is correct |
15 |
Correct |
80 ms |
72724 KB |
Output is correct |
16 |
Correct |
72 ms |
72752 KB |
Output is correct |
17 |
Correct |
73 ms |
72696 KB |
Output is correct |
18 |
Correct |
73 ms |
72824 KB |
Output is correct |
19 |
Correct |
74 ms |
72800 KB |
Output is correct |
20 |
Correct |
72 ms |
72824 KB |
Output is correct |
21 |
Correct |
72 ms |
72696 KB |
Output is correct |
22 |
Correct |
75 ms |
72828 KB |
Output is correct |
23 |
Correct |
73 ms |
72704 KB |
Output is correct |
24 |
Correct |
73 ms |
72760 KB |
Output is correct |
25 |
Correct |
73 ms |
72768 KB |
Output is correct |
26 |
Correct |
76 ms |
72924 KB |
Output is correct |
27 |
Correct |
76 ms |
72928 KB |
Output is correct |
28 |
Correct |
75 ms |
72952 KB |
Output is correct |
29 |
Correct |
76 ms |
72952 KB |
Output is correct |
30 |
Correct |
76 ms |
72824 KB |
Output is correct |
31 |
Correct |
72 ms |
72696 KB |
Output is correct |
32 |
Correct |
78 ms |
73116 KB |
Output is correct |
33 |
Correct |
72 ms |
72824 KB |
Output is correct |
34 |
Correct |
75 ms |
72952 KB |
Output is correct |
35 |
Correct |
75 ms |
72952 KB |
Output is correct |
36 |
Correct |
76 ms |
72840 KB |
Output is correct |
37 |
Correct |
77 ms |
73012 KB |
Output is correct |
38 |
Correct |
73 ms |
72828 KB |
Output is correct |
39 |
Correct |
76 ms |
72952 KB |
Output is correct |
40 |
Correct |
59 ms |
63224 KB |
Output is correct |
41 |
Correct |
76 ms |
72952 KB |
Output is correct |
42 |
Correct |
75 ms |
72952 KB |
Output is correct |
43 |
Correct |
59 ms |
63352 KB |
Output is correct |
44 |
Correct |
73 ms |
72824 KB |
Output is correct |
45 |
Correct |
76 ms |
73052 KB |
Output is correct |
46 |
Correct |
76 ms |
72952 KB |
Output is correct |
47 |
Correct |
84 ms |
72752 KB |
Output is correct |
48 |
Correct |
93 ms |
72952 KB |
Output is correct |
49 |
Correct |
75 ms |
72952 KB |
Output is correct |
50 |
Correct |
77 ms |
73080 KB |
Output is correct |
51 |
Correct |
76 ms |
72856 KB |
Output is correct |
52 |
Correct |
74 ms |
72988 KB |
Output is correct |
53 |
Correct |
75 ms |
72916 KB |
Output is correct |
54 |
Correct |
76 ms |
72952 KB |
Output is correct |
55 |
Correct |
74 ms |
72816 KB |
Output is correct |
56 |
Correct |
185 ms |
77280 KB |
Output is correct |
57 |
Correct |
233 ms |
76916 KB |
Output is correct |
58 |
Correct |
76 ms |
72828 KB |
Output is correct |
59 |
Correct |
72 ms |
72824 KB |
Output is correct |
60 |
Correct |
72 ms |
72744 KB |
Output is correct |
61 |
Correct |
236 ms |
76940 KB |
Output is correct |
62 |
Correct |
74 ms |
73080 KB |
Output is correct |
63 |
Correct |
326 ms |
82524 KB |
Output is correct |
64 |
Correct |
260 ms |
77060 KB |
Output is correct |
65 |
Correct |
75 ms |
72828 KB |
Output is correct |
66 |
Correct |
297 ms |
77928 KB |
Output is correct |
67 |
Correct |
60 ms |
63224 KB |
Output is correct |
68 |
Correct |
75 ms |
72952 KB |
Output is correct |
69 |
Correct |
180 ms |
77712 KB |
Output is correct |
70 |
Correct |
60 ms |
63352 KB |
Output is correct |
71 |
Correct |
179 ms |
69092 KB |
Output is correct |
72 |
Correct |
310 ms |
80120 KB |
Output is correct |
73 |
Correct |
74 ms |
72696 KB |
Output is correct |
74 |
Correct |
222 ms |
75096 KB |
Output is correct |
75 |
Correct |
288 ms |
80376 KB |
Output is correct |
76 |
Correct |
75 ms |
72952 KB |
Output is correct |
77 |
Correct |
77 ms |
72952 KB |
Output is correct |
78 |
Correct |
177 ms |
77252 KB |
Output is correct |
79 |
Correct |
219 ms |
80120 KB |
Output is correct |
80 |
Correct |
76 ms |
72932 KB |
Output is correct |
81 |
Correct |
83 ms |
72896 KB |
Output is correct |
82 |
Correct |
72 ms |
72824 KB |
Output is correct |
83 |
Correct |
72 ms |
72680 KB |
Output is correct |
84 |
Correct |
75 ms |
72952 KB |
Output is correct |
85 |
Correct |
284 ms |
78392 KB |
Output is correct |
86 |
Correct |
76 ms |
72952 KB |
Output is correct |
87 |
Correct |
243 ms |
77304 KB |
Output is correct |
88 |
Correct |
73 ms |
72700 KB |
Output is correct |
89 |
Correct |
299 ms |
77708 KB |
Output is correct |
90 |
Correct |
396 ms |
78712 KB |
Output is correct |
91 |
Correct |
267 ms |
80376 KB |
Output is correct |
92 |
Correct |
182 ms |
77552 KB |
Output is correct |
93 |
Correct |
76 ms |
72952 KB |
Output is correct |
94 |
Correct |
73 ms |
72824 KB |
Output is correct |
95 |
Correct |
210 ms |
80192 KB |
Output is correct |
96 |
Correct |
337 ms |
85496 KB |
Output is correct |
97 |
Correct |
90 ms |
72952 KB |
Output is correct |
98 |
Correct |
78 ms |
72568 KB |
Output is correct |
99 |
Correct |
205 ms |
78960 KB |
Output is correct |
100 |
Correct |
92 ms |
72924 KB |
Output is correct |
101 |
Correct |
304 ms |
78120 KB |
Output is correct |
102 |
Correct |
297 ms |
82428 KB |
Output is correct |
103 |
Correct |
76 ms |
73080 KB |
Output is correct |
104 |
Correct |
76 ms |
73080 KB |
Output is correct |
105 |
Correct |
77 ms |
72924 KB |
Output is correct |
106 |
Correct |
88 ms |
73076 KB |
Output is correct |
107 |
Correct |
75 ms |
72924 KB |
Output is correct |
108 |
Correct |
1101 ms |
101504 KB |
Output is correct |
109 |
Correct |
2151 ms |
109620 KB |
Output is correct |
110 |
Correct |
1392 ms |
115208 KB |
Output is correct |
111 |
Correct |
2158 ms |
125756 KB |
Output is correct |
112 |
Correct |
2118 ms |
122984 KB |
Output is correct |
113 |
Correct |
1151 ms |
109004 KB |
Output is correct |
114 |
Incorrect |
1290 ms |
93988 KB |
Output isn't correct |
115 |
Halted |
0 ms |
0 KB |
- |