#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], deg[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, int last)
{
for(int v : adj[u]) if(p != v)
{
if(mark[v] && mark[last]) deg[v]++, deg[last]++, DFS(v, u, v);
else DFS(v, u, last);
}
}
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] += (int)vec[i].size();
}
calc(1, 0);
DFS(1, 0, 1);
for(int i=1; i<=n; i++) ans += (deg[i]==1);
cout << (ans+1)/2 << endl;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:46: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:49: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:50: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);
~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
62968 KB |
Output is correct |
2 |
Correct |
57 ms |
62940 KB |
Output is correct |
3 |
Correct |
56 ms |
62968 KB |
Output is correct |
4 |
Correct |
56 ms |
62968 KB |
Output is correct |
5 |
Correct |
56 ms |
62968 KB |
Output is correct |
6 |
Correct |
56 ms |
62972 KB |
Output is correct |
7 |
Correct |
56 ms |
62968 KB |
Output is correct |
8 |
Correct |
56 ms |
63020 KB |
Output is correct |
9 |
Correct |
56 ms |
63044 KB |
Output is correct |
10 |
Correct |
57 ms |
62968 KB |
Output is correct |
11 |
Correct |
72 ms |
63108 KB |
Output is correct |
12 |
Correct |
58 ms |
63068 KB |
Output is correct |
13 |
Correct |
57 ms |
62924 KB |
Output is correct |
14 |
Correct |
57 ms |
63016 KB |
Output is correct |
15 |
Correct |
57 ms |
62940 KB |
Output is correct |
16 |
Correct |
57 ms |
62968 KB |
Output is correct |
17 |
Correct |
62 ms |
62980 KB |
Output is correct |
18 |
Correct |
68 ms |
62972 KB |
Output is correct |
19 |
Correct |
57 ms |
62968 KB |
Output is correct |
20 |
Correct |
64 ms |
63096 KB |
Output is correct |
21 |
Correct |
73 ms |
62940 KB |
Output is correct |
22 |
Correct |
69 ms |
63028 KB |
Output is correct |
23 |
Correct |
68 ms |
62940 KB |
Output is correct |
24 |
Correct |
67 ms |
62940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
62968 KB |
Output is correct |
2 |
Correct |
57 ms |
62940 KB |
Output is correct |
3 |
Correct |
56 ms |
62968 KB |
Output is correct |
4 |
Correct |
56 ms |
62968 KB |
Output is correct |
5 |
Correct |
56 ms |
62968 KB |
Output is correct |
6 |
Correct |
56 ms |
62972 KB |
Output is correct |
7 |
Correct |
56 ms |
62968 KB |
Output is correct |
8 |
Correct |
56 ms |
63020 KB |
Output is correct |
9 |
Correct |
56 ms |
63044 KB |
Output is correct |
10 |
Correct |
57 ms |
62968 KB |
Output is correct |
11 |
Correct |
72 ms |
63108 KB |
Output is correct |
12 |
Correct |
58 ms |
63068 KB |
Output is correct |
13 |
Correct |
57 ms |
62924 KB |
Output is correct |
14 |
Correct |
57 ms |
63016 KB |
Output is correct |
15 |
Correct |
57 ms |
62940 KB |
Output is correct |
16 |
Correct |
57 ms |
62968 KB |
Output is correct |
17 |
Correct |
62 ms |
62980 KB |
Output is correct |
18 |
Correct |
68 ms |
62972 KB |
Output is correct |
19 |
Correct |
57 ms |
62968 KB |
Output is correct |
20 |
Correct |
64 ms |
63096 KB |
Output is correct |
21 |
Correct |
73 ms |
62940 KB |
Output is correct |
22 |
Correct |
69 ms |
63028 KB |
Output is correct |
23 |
Correct |
68 ms |
62940 KB |
Output is correct |
24 |
Correct |
67 ms |
62940 KB |
Output is correct |
25 |
Correct |
63 ms |
63024 KB |
Output is correct |
26 |
Correct |
59 ms |
63224 KB |
Output is correct |
27 |
Correct |
59 ms |
63196 KB |
Output is correct |
28 |
Correct |
60 ms |
63352 KB |
Output is correct |
29 |
Correct |
59 ms |
63224 KB |
Output is correct |
30 |
Correct |
60 ms |
63212 KB |
Output is correct |
31 |
Correct |
57 ms |
62964 KB |
Output is correct |
32 |
Correct |
59 ms |
63480 KB |
Output is correct |
33 |
Correct |
57 ms |
62968 KB |
Output is correct |
34 |
Correct |
59 ms |
63228 KB |
Output is correct |
35 |
Correct |
60 ms |
63352 KB |
Output is correct |
36 |
Correct |
59 ms |
63208 KB |
Output is correct |
37 |
Correct |
66 ms |
63248 KB |
Output is correct |
38 |
Correct |
57 ms |
62972 KB |
Output is correct |
39 |
Correct |
65 ms |
63252 KB |
Output is correct |
40 |
Correct |
63 ms |
63224 KB |
Output is correct |
41 |
Correct |
66 ms |
63228 KB |
Output is correct |
42 |
Correct |
59 ms |
63352 KB |
Output is correct |
43 |
Correct |
59 ms |
63352 KB |
Output is correct |
44 |
Correct |
68 ms |
62968 KB |
Output is correct |
45 |
Correct |
59 ms |
63352 KB |
Output is correct |
46 |
Correct |
60 ms |
63228 KB |
Output is correct |
47 |
Correct |
61 ms |
62968 KB |
Output is correct |
48 |
Correct |
70 ms |
63188 KB |
Output is correct |
49 |
Correct |
61 ms |
63352 KB |
Output is correct |
50 |
Correct |
63 ms |
63352 KB |
Output is correct |
51 |
Correct |
59 ms |
63224 KB |
Output is correct |
52 |
Correct |
58 ms |
63224 KB |
Output is correct |
53 |
Correct |
59 ms |
63224 KB |
Output is correct |
54 |
Correct |
61 ms |
63224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
62968 KB |
Output is correct |
2 |
Correct |
57 ms |
62940 KB |
Output is correct |
3 |
Correct |
56 ms |
62968 KB |
Output is correct |
4 |
Correct |
56 ms |
62968 KB |
Output is correct |
5 |
Correct |
56 ms |
62968 KB |
Output is correct |
6 |
Correct |
56 ms |
62972 KB |
Output is correct |
7 |
Correct |
56 ms |
62968 KB |
Output is correct |
8 |
Correct |
56 ms |
63020 KB |
Output is correct |
9 |
Correct |
56 ms |
63044 KB |
Output is correct |
10 |
Correct |
57 ms |
62968 KB |
Output is correct |
11 |
Correct |
72 ms |
63108 KB |
Output is correct |
12 |
Correct |
58 ms |
63068 KB |
Output is correct |
13 |
Correct |
57 ms |
62924 KB |
Output is correct |
14 |
Correct |
57 ms |
63016 KB |
Output is correct |
15 |
Correct |
57 ms |
62940 KB |
Output is correct |
16 |
Correct |
57 ms |
62968 KB |
Output is correct |
17 |
Correct |
62 ms |
62980 KB |
Output is correct |
18 |
Correct |
68 ms |
62972 KB |
Output is correct |
19 |
Correct |
57 ms |
62968 KB |
Output is correct |
20 |
Correct |
64 ms |
63096 KB |
Output is correct |
21 |
Correct |
73 ms |
62940 KB |
Output is correct |
22 |
Correct |
69 ms |
63028 KB |
Output is correct |
23 |
Correct |
68 ms |
62940 KB |
Output is correct |
24 |
Correct |
67 ms |
62940 KB |
Output is correct |
25 |
Correct |
68 ms |
62992 KB |
Output is correct |
26 |
Correct |
169 ms |
69596 KB |
Output is correct |
27 |
Correct |
251 ms |
69624 KB |
Output is correct |
28 |
Correct |
60 ms |
63224 KB |
Output is correct |
29 |
Correct |
57 ms |
62968 KB |
Output is correct |
30 |
Correct |
57 ms |
63080 KB |
Output is correct |
31 |
Correct |
179 ms |
69872 KB |
Output is correct |
32 |
Correct |
59 ms |
63224 KB |
Output is correct |
33 |
Correct |
235 ms |
75256 KB |
Output is correct |
34 |
Correct |
174 ms |
69660 KB |
Output is correct |
35 |
Correct |
59 ms |
63196 KB |
Output is correct |
36 |
Correct |
209 ms |
70396 KB |
Output is correct |
37 |
Correct |
59 ms |
63224 KB |
Output is correct |
38 |
Correct |
59 ms |
63148 KB |
Output is correct |
39 |
Correct |
138 ms |
69460 KB |
Output is correct |
40 |
Correct |
60 ms |
63424 KB |
Output is correct |
41 |
Correct |
169 ms |
69712 KB |
Output is correct |
42 |
Correct |
230 ms |
71524 KB |
Output is correct |
43 |
Correct |
57 ms |
62968 KB |
Output is correct |
44 |
Correct |
269 ms |
75720 KB |
Output is correct |
45 |
Correct |
211 ms |
72636 KB |
Output is correct |
46 |
Correct |
59 ms |
63204 KB |
Output is correct |
47 |
Correct |
59 ms |
63224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
136 ms |
69484 KB |
Output is correct |
2 |
Correct |
177 ms |
73300 KB |
Output is correct |
3 |
Correct |
61 ms |
63224 KB |
Output is correct |
4 |
Correct |
64 ms |
63340 KB |
Output is correct |
5 |
Correct |
65 ms |
62968 KB |
Output is correct |
6 |
Correct |
57 ms |
62968 KB |
Output is correct |
7 |
Correct |
59 ms |
63196 KB |
Output is correct |
8 |
Correct |
230 ms |
71436 KB |
Output is correct |
9 |
Correct |
60 ms |
63224 KB |
Output is correct |
10 |
Correct |
185 ms |
70460 KB |
Output is correct |
11 |
Correct |
57 ms |
62968 KB |
Output is correct |
12 |
Correct |
210 ms |
70364 KB |
Output is correct |
13 |
Correct |
265 ms |
71416 KB |
Output is correct |
14 |
Correct |
248 ms |
73052 KB |
Output is correct |
15 |
Correct |
174 ms |
69392 KB |
Output is correct |
16 |
Correct |
60 ms |
63316 KB |
Output is correct |
17 |
Correct |
58 ms |
63064 KB |
Output is correct |
18 |
Correct |
207 ms |
73044 KB |
Output is correct |
19 |
Correct |
230 ms |
76116 KB |
Output is correct |
20 |
Correct |
59 ms |
63352 KB |
Output is correct |
21 |
Correct |
57 ms |
63096 KB |
Output is correct |
22 |
Correct |
168 ms |
71832 KB |
Output is correct |
23 |
Correct |
60 ms |
63224 KB |
Output is correct |
24 |
Correct |
275 ms |
70648 KB |
Output is correct |
25 |
Correct |
280 ms |
74980 KB |
Output is correct |
26 |
Correct |
59 ms |
63352 KB |
Output is correct |
27 |
Correct |
70 ms |
63352 KB |
Output is correct |
28 |
Correct |
59 ms |
63224 KB |
Output is correct |
29 |
Correct |
69 ms |
63260 KB |
Output is correct |
30 |
Correct |
76 ms |
63236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
62968 KB |
Output is correct |
2 |
Correct |
57 ms |
62940 KB |
Output is correct |
3 |
Correct |
56 ms |
62968 KB |
Output is correct |
4 |
Correct |
56 ms |
62968 KB |
Output is correct |
5 |
Correct |
56 ms |
62968 KB |
Output is correct |
6 |
Correct |
56 ms |
62972 KB |
Output is correct |
7 |
Correct |
56 ms |
62968 KB |
Output is correct |
8 |
Correct |
56 ms |
63020 KB |
Output is correct |
9 |
Correct |
56 ms |
63044 KB |
Output is correct |
10 |
Correct |
57 ms |
62968 KB |
Output is correct |
11 |
Correct |
72 ms |
63108 KB |
Output is correct |
12 |
Correct |
58 ms |
63068 KB |
Output is correct |
13 |
Correct |
57 ms |
62924 KB |
Output is correct |
14 |
Correct |
57 ms |
63016 KB |
Output is correct |
15 |
Correct |
57 ms |
62940 KB |
Output is correct |
16 |
Correct |
57 ms |
62968 KB |
Output is correct |
17 |
Correct |
62 ms |
62980 KB |
Output is correct |
18 |
Correct |
68 ms |
62972 KB |
Output is correct |
19 |
Correct |
57 ms |
62968 KB |
Output is correct |
20 |
Correct |
64 ms |
63096 KB |
Output is correct |
21 |
Correct |
73 ms |
62940 KB |
Output is correct |
22 |
Correct |
69 ms |
63028 KB |
Output is correct |
23 |
Correct |
68 ms |
62940 KB |
Output is correct |
24 |
Correct |
67 ms |
62940 KB |
Output is correct |
25 |
Correct |
63 ms |
63024 KB |
Output is correct |
26 |
Correct |
59 ms |
63224 KB |
Output is correct |
27 |
Correct |
59 ms |
63196 KB |
Output is correct |
28 |
Correct |
60 ms |
63352 KB |
Output is correct |
29 |
Correct |
59 ms |
63224 KB |
Output is correct |
30 |
Correct |
60 ms |
63212 KB |
Output is correct |
31 |
Correct |
57 ms |
62964 KB |
Output is correct |
32 |
Correct |
59 ms |
63480 KB |
Output is correct |
33 |
Correct |
57 ms |
62968 KB |
Output is correct |
34 |
Correct |
59 ms |
63228 KB |
Output is correct |
35 |
Correct |
60 ms |
63352 KB |
Output is correct |
36 |
Correct |
59 ms |
63208 KB |
Output is correct |
37 |
Correct |
66 ms |
63248 KB |
Output is correct |
38 |
Correct |
57 ms |
62972 KB |
Output is correct |
39 |
Correct |
65 ms |
63252 KB |
Output is correct |
40 |
Correct |
63 ms |
63224 KB |
Output is correct |
41 |
Correct |
66 ms |
63228 KB |
Output is correct |
42 |
Correct |
59 ms |
63352 KB |
Output is correct |
43 |
Correct |
59 ms |
63352 KB |
Output is correct |
44 |
Correct |
68 ms |
62968 KB |
Output is correct |
45 |
Correct |
59 ms |
63352 KB |
Output is correct |
46 |
Correct |
60 ms |
63228 KB |
Output is correct |
47 |
Correct |
61 ms |
62968 KB |
Output is correct |
48 |
Correct |
70 ms |
63188 KB |
Output is correct |
49 |
Correct |
61 ms |
63352 KB |
Output is correct |
50 |
Correct |
63 ms |
63352 KB |
Output is correct |
51 |
Correct |
59 ms |
63224 KB |
Output is correct |
52 |
Correct |
58 ms |
63224 KB |
Output is correct |
53 |
Correct |
59 ms |
63224 KB |
Output is correct |
54 |
Correct |
61 ms |
63224 KB |
Output is correct |
55 |
Correct |
68 ms |
62992 KB |
Output is correct |
56 |
Correct |
169 ms |
69596 KB |
Output is correct |
57 |
Correct |
251 ms |
69624 KB |
Output is correct |
58 |
Correct |
60 ms |
63224 KB |
Output is correct |
59 |
Correct |
57 ms |
62968 KB |
Output is correct |
60 |
Correct |
57 ms |
63080 KB |
Output is correct |
61 |
Correct |
179 ms |
69872 KB |
Output is correct |
62 |
Correct |
59 ms |
63224 KB |
Output is correct |
63 |
Correct |
235 ms |
75256 KB |
Output is correct |
64 |
Correct |
174 ms |
69660 KB |
Output is correct |
65 |
Correct |
59 ms |
63196 KB |
Output is correct |
66 |
Correct |
209 ms |
70396 KB |
Output is correct |
67 |
Correct |
59 ms |
63224 KB |
Output is correct |
68 |
Correct |
59 ms |
63148 KB |
Output is correct |
69 |
Correct |
138 ms |
69460 KB |
Output is correct |
70 |
Correct |
60 ms |
63424 KB |
Output is correct |
71 |
Correct |
169 ms |
69712 KB |
Output is correct |
72 |
Correct |
230 ms |
71524 KB |
Output is correct |
73 |
Correct |
57 ms |
62968 KB |
Output is correct |
74 |
Correct |
269 ms |
75720 KB |
Output is correct |
75 |
Correct |
211 ms |
72636 KB |
Output is correct |
76 |
Correct |
59 ms |
63204 KB |
Output is correct |
77 |
Correct |
59 ms |
63224 KB |
Output is correct |
78 |
Correct |
136 ms |
69484 KB |
Output is correct |
79 |
Correct |
177 ms |
73300 KB |
Output is correct |
80 |
Correct |
61 ms |
63224 KB |
Output is correct |
81 |
Correct |
64 ms |
63340 KB |
Output is correct |
82 |
Correct |
65 ms |
62968 KB |
Output is correct |
83 |
Correct |
57 ms |
62968 KB |
Output is correct |
84 |
Correct |
59 ms |
63196 KB |
Output is correct |
85 |
Correct |
230 ms |
71436 KB |
Output is correct |
86 |
Correct |
60 ms |
63224 KB |
Output is correct |
87 |
Correct |
185 ms |
70460 KB |
Output is correct |
88 |
Correct |
57 ms |
62968 KB |
Output is correct |
89 |
Correct |
210 ms |
70364 KB |
Output is correct |
90 |
Correct |
265 ms |
71416 KB |
Output is correct |
91 |
Correct |
248 ms |
73052 KB |
Output is correct |
92 |
Correct |
174 ms |
69392 KB |
Output is correct |
93 |
Correct |
60 ms |
63316 KB |
Output is correct |
94 |
Correct |
58 ms |
63064 KB |
Output is correct |
95 |
Correct |
207 ms |
73044 KB |
Output is correct |
96 |
Correct |
230 ms |
76116 KB |
Output is correct |
97 |
Correct |
59 ms |
63352 KB |
Output is correct |
98 |
Correct |
57 ms |
63096 KB |
Output is correct |
99 |
Correct |
168 ms |
71832 KB |
Output is correct |
100 |
Correct |
60 ms |
63224 KB |
Output is correct |
101 |
Correct |
275 ms |
70648 KB |
Output is correct |
102 |
Correct |
280 ms |
74980 KB |
Output is correct |
103 |
Correct |
59 ms |
63352 KB |
Output is correct |
104 |
Correct |
70 ms |
63352 KB |
Output is correct |
105 |
Correct |
59 ms |
63224 KB |
Output is correct |
106 |
Correct |
69 ms |
63260 KB |
Output is correct |
107 |
Correct |
76 ms |
63236 KB |
Output is correct |
108 |
Incorrect |
799 ms |
99080 KB |
Output isn't correct |
109 |
Halted |
0 ms |
0 KB |
- |