#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][21], 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<21; 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<21; 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=20; 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);
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();
assert(node);
}
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:48: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:49: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 |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23928 KB |
Output is correct |
4 |
Correct |
28 ms |
23900 KB |
Output is correct |
5 |
Correct |
24 ms |
23800 KB |
Output is correct |
6 |
Correct |
24 ms |
23800 KB |
Output is correct |
7 |
Correct |
23 ms |
23800 KB |
Output is correct |
8 |
Correct |
22 ms |
23928 KB |
Output is correct |
9 |
Correct |
23 ms |
23800 KB |
Output is correct |
10 |
Correct |
24 ms |
23928 KB |
Output is correct |
11 |
Correct |
24 ms |
23804 KB |
Output is correct |
12 |
Correct |
28 ms |
23928 KB |
Output is correct |
13 |
Correct |
24 ms |
23928 KB |
Output is correct |
14 |
Correct |
24 ms |
23928 KB |
Output is correct |
15 |
Correct |
24 ms |
23928 KB |
Output is correct |
16 |
Correct |
29 ms |
23928 KB |
Output is correct |
17 |
Correct |
23 ms |
23928 KB |
Output is correct |
18 |
Correct |
24 ms |
23800 KB |
Output is correct |
19 |
Correct |
24 ms |
23800 KB |
Output is correct |
20 |
Correct |
24 ms |
23928 KB |
Output is correct |
21 |
Correct |
24 ms |
23928 KB |
Output is correct |
22 |
Correct |
23 ms |
23800 KB |
Output is correct |
23 |
Correct |
28 ms |
23928 KB |
Output is correct |
24 |
Correct |
29 ms |
24056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23928 KB |
Output is correct |
4 |
Correct |
28 ms |
23900 KB |
Output is correct |
5 |
Correct |
24 ms |
23800 KB |
Output is correct |
6 |
Correct |
24 ms |
23800 KB |
Output is correct |
7 |
Correct |
23 ms |
23800 KB |
Output is correct |
8 |
Correct |
22 ms |
23928 KB |
Output is correct |
9 |
Correct |
23 ms |
23800 KB |
Output is correct |
10 |
Correct |
24 ms |
23928 KB |
Output is correct |
11 |
Correct |
24 ms |
23804 KB |
Output is correct |
12 |
Correct |
28 ms |
23928 KB |
Output is correct |
13 |
Correct |
24 ms |
23928 KB |
Output is correct |
14 |
Correct |
24 ms |
23928 KB |
Output is correct |
15 |
Correct |
24 ms |
23928 KB |
Output is correct |
16 |
Correct |
29 ms |
23928 KB |
Output is correct |
17 |
Correct |
23 ms |
23928 KB |
Output is correct |
18 |
Correct |
24 ms |
23800 KB |
Output is correct |
19 |
Correct |
24 ms |
23800 KB |
Output is correct |
20 |
Correct |
24 ms |
23928 KB |
Output is correct |
21 |
Correct |
24 ms |
23928 KB |
Output is correct |
22 |
Correct |
23 ms |
23800 KB |
Output is correct |
23 |
Correct |
28 ms |
23928 KB |
Output is correct |
24 |
Correct |
29 ms |
24056 KB |
Output is correct |
25 |
Correct |
24 ms |
23800 KB |
Output is correct |
26 |
Correct |
26 ms |
24312 KB |
Output is correct |
27 |
Correct |
26 ms |
24312 KB |
Output is correct |
28 |
Correct |
26 ms |
24440 KB |
Output is correct |
29 |
Correct |
27 ms |
24444 KB |
Output is correct |
30 |
Correct |
31 ms |
24312 KB |
Output is correct |
31 |
Correct |
29 ms |
23800 KB |
Output is correct |
32 |
Correct |
27 ms |
24440 KB |
Output is correct |
33 |
Correct |
23 ms |
23928 KB |
Output is correct |
34 |
Correct |
29 ms |
24312 KB |
Output is correct |
35 |
Correct |
27 ms |
24312 KB |
Output is correct |
36 |
Correct |
26 ms |
24312 KB |
Output is correct |
37 |
Correct |
27 ms |
24312 KB |
Output is correct |
38 |
Correct |
24 ms |
23856 KB |
Output is correct |
39 |
Correct |
27 ms |
24312 KB |
Output is correct |
40 |
Correct |
26 ms |
24312 KB |
Output is correct |
41 |
Correct |
26 ms |
24312 KB |
Output is correct |
42 |
Correct |
31 ms |
24356 KB |
Output is correct |
43 |
Correct |
27 ms |
24440 KB |
Output is correct |
44 |
Correct |
24 ms |
23932 KB |
Output is correct |
45 |
Correct |
28 ms |
24440 KB |
Output is correct |
46 |
Correct |
26 ms |
24312 KB |
Output is correct |
47 |
Correct |
23 ms |
23928 KB |
Output is correct |
48 |
Correct |
27 ms |
24440 KB |
Output is correct |
49 |
Correct |
26 ms |
24448 KB |
Output is correct |
50 |
Correct |
26 ms |
24440 KB |
Output is correct |
51 |
Correct |
26 ms |
24300 KB |
Output is correct |
52 |
Correct |
25 ms |
24312 KB |
Output is correct |
53 |
Correct |
26 ms |
24312 KB |
Output is correct |
54 |
Correct |
26 ms |
24184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23928 KB |
Output is correct |
4 |
Correct |
28 ms |
23900 KB |
Output is correct |
5 |
Correct |
24 ms |
23800 KB |
Output is correct |
6 |
Correct |
24 ms |
23800 KB |
Output is correct |
7 |
Correct |
23 ms |
23800 KB |
Output is correct |
8 |
Correct |
22 ms |
23928 KB |
Output is correct |
9 |
Correct |
23 ms |
23800 KB |
Output is correct |
10 |
Correct |
24 ms |
23928 KB |
Output is correct |
11 |
Correct |
24 ms |
23804 KB |
Output is correct |
12 |
Correct |
28 ms |
23928 KB |
Output is correct |
13 |
Correct |
24 ms |
23928 KB |
Output is correct |
14 |
Correct |
24 ms |
23928 KB |
Output is correct |
15 |
Correct |
24 ms |
23928 KB |
Output is correct |
16 |
Correct |
29 ms |
23928 KB |
Output is correct |
17 |
Correct |
23 ms |
23928 KB |
Output is correct |
18 |
Correct |
24 ms |
23800 KB |
Output is correct |
19 |
Correct |
24 ms |
23800 KB |
Output is correct |
20 |
Correct |
24 ms |
23928 KB |
Output is correct |
21 |
Correct |
24 ms |
23928 KB |
Output is correct |
22 |
Correct |
23 ms |
23800 KB |
Output is correct |
23 |
Correct |
28 ms |
23928 KB |
Output is correct |
24 |
Correct |
29 ms |
24056 KB |
Output is correct |
25 |
Correct |
24 ms |
23840 KB |
Output is correct |
26 |
Correct |
120 ms |
37404 KB |
Output is correct |
27 |
Correct |
150 ms |
37368 KB |
Output is correct |
28 |
Correct |
26 ms |
24312 KB |
Output is correct |
29 |
Correct |
24 ms |
23928 KB |
Output is correct |
30 |
Correct |
28 ms |
23928 KB |
Output is correct |
31 |
Correct |
148 ms |
37624 KB |
Output is correct |
32 |
Correct |
31 ms |
24312 KB |
Output is correct |
33 |
Correct |
295 ms |
43256 KB |
Output is correct |
34 |
Correct |
184 ms |
37460 KB |
Output is correct |
35 |
Correct |
26 ms |
24312 KB |
Output is correct |
36 |
Correct |
175 ms |
38140 KB |
Output is correct |
37 |
Correct |
26 ms |
24316 KB |
Output is correct |
38 |
Correct |
26 ms |
24312 KB |
Output is correct |
39 |
Correct |
113 ms |
37332 KB |
Output is correct |
40 |
Correct |
27 ms |
24440 KB |
Output is correct |
41 |
Correct |
148 ms |
37488 KB |
Output is correct |
42 |
Correct |
199 ms |
39516 KB |
Output is correct |
43 |
Correct |
23 ms |
23800 KB |
Output is correct |
44 |
Correct |
202 ms |
43388 KB |
Output is correct |
45 |
Correct |
192 ms |
40448 KB |
Output is correct |
46 |
Correct |
26 ms |
24312 KB |
Output is correct |
47 |
Correct |
26 ms |
24312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
115 ms |
37488 KB |
Output is correct |
2 |
Correct |
135 ms |
41128 KB |
Output is correct |
3 |
Correct |
32 ms |
24312 KB |
Output is correct |
4 |
Correct |
26 ms |
24312 KB |
Output is correct |
5 |
Correct |
27 ms |
23928 KB |
Output is correct |
6 |
Correct |
29 ms |
23928 KB |
Output is correct |
7 |
Correct |
26 ms |
24312 KB |
Output is correct |
8 |
Correct |
189 ms |
39304 KB |
Output is correct |
9 |
Correct |
26 ms |
24312 KB |
Output is correct |
10 |
Correct |
200 ms |
38276 KB |
Output is correct |
11 |
Correct |
30 ms |
23800 KB |
Output is correct |
12 |
Correct |
248 ms |
38244 KB |
Output is correct |
13 |
Correct |
184 ms |
39148 KB |
Output is correct |
14 |
Correct |
252 ms |
40784 KB |
Output is correct |
15 |
Correct |
115 ms |
37232 KB |
Output is correct |
16 |
Correct |
26 ms |
24312 KB |
Output is correct |
17 |
Correct |
24 ms |
23800 KB |
Output is correct |
18 |
Correct |
146 ms |
40816 KB |
Output is correct |
19 |
Correct |
210 ms |
44008 KB |
Output is correct |
20 |
Correct |
27 ms |
24312 KB |
Output is correct |
21 |
Correct |
23 ms |
23928 KB |
Output is correct |
22 |
Correct |
133 ms |
39532 KB |
Output is correct |
23 |
Correct |
26 ms |
24440 KB |
Output is correct |
24 |
Correct |
238 ms |
38616 KB |
Output is correct |
25 |
Correct |
199 ms |
42900 KB |
Output is correct |
26 |
Correct |
26 ms |
24312 KB |
Output is correct |
27 |
Correct |
26 ms |
24440 KB |
Output is correct |
28 |
Correct |
27 ms |
24312 KB |
Output is correct |
29 |
Correct |
25 ms |
24312 KB |
Output is correct |
30 |
Correct |
26 ms |
24312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23928 KB |
Output is correct |
4 |
Correct |
28 ms |
23900 KB |
Output is correct |
5 |
Correct |
24 ms |
23800 KB |
Output is correct |
6 |
Correct |
24 ms |
23800 KB |
Output is correct |
7 |
Correct |
23 ms |
23800 KB |
Output is correct |
8 |
Correct |
22 ms |
23928 KB |
Output is correct |
9 |
Correct |
23 ms |
23800 KB |
Output is correct |
10 |
Correct |
24 ms |
23928 KB |
Output is correct |
11 |
Correct |
24 ms |
23804 KB |
Output is correct |
12 |
Correct |
28 ms |
23928 KB |
Output is correct |
13 |
Correct |
24 ms |
23928 KB |
Output is correct |
14 |
Correct |
24 ms |
23928 KB |
Output is correct |
15 |
Correct |
24 ms |
23928 KB |
Output is correct |
16 |
Correct |
29 ms |
23928 KB |
Output is correct |
17 |
Correct |
23 ms |
23928 KB |
Output is correct |
18 |
Correct |
24 ms |
23800 KB |
Output is correct |
19 |
Correct |
24 ms |
23800 KB |
Output is correct |
20 |
Correct |
24 ms |
23928 KB |
Output is correct |
21 |
Correct |
24 ms |
23928 KB |
Output is correct |
22 |
Correct |
23 ms |
23800 KB |
Output is correct |
23 |
Correct |
28 ms |
23928 KB |
Output is correct |
24 |
Correct |
29 ms |
24056 KB |
Output is correct |
25 |
Correct |
24 ms |
23800 KB |
Output is correct |
26 |
Correct |
26 ms |
24312 KB |
Output is correct |
27 |
Correct |
26 ms |
24312 KB |
Output is correct |
28 |
Correct |
26 ms |
24440 KB |
Output is correct |
29 |
Correct |
27 ms |
24444 KB |
Output is correct |
30 |
Correct |
31 ms |
24312 KB |
Output is correct |
31 |
Correct |
29 ms |
23800 KB |
Output is correct |
32 |
Correct |
27 ms |
24440 KB |
Output is correct |
33 |
Correct |
23 ms |
23928 KB |
Output is correct |
34 |
Correct |
29 ms |
24312 KB |
Output is correct |
35 |
Correct |
27 ms |
24312 KB |
Output is correct |
36 |
Correct |
26 ms |
24312 KB |
Output is correct |
37 |
Correct |
27 ms |
24312 KB |
Output is correct |
38 |
Correct |
24 ms |
23856 KB |
Output is correct |
39 |
Correct |
27 ms |
24312 KB |
Output is correct |
40 |
Correct |
26 ms |
24312 KB |
Output is correct |
41 |
Correct |
26 ms |
24312 KB |
Output is correct |
42 |
Correct |
31 ms |
24356 KB |
Output is correct |
43 |
Correct |
27 ms |
24440 KB |
Output is correct |
44 |
Correct |
24 ms |
23932 KB |
Output is correct |
45 |
Correct |
28 ms |
24440 KB |
Output is correct |
46 |
Correct |
26 ms |
24312 KB |
Output is correct |
47 |
Correct |
23 ms |
23928 KB |
Output is correct |
48 |
Correct |
27 ms |
24440 KB |
Output is correct |
49 |
Correct |
26 ms |
24448 KB |
Output is correct |
50 |
Correct |
26 ms |
24440 KB |
Output is correct |
51 |
Correct |
26 ms |
24300 KB |
Output is correct |
52 |
Correct |
25 ms |
24312 KB |
Output is correct |
53 |
Correct |
26 ms |
24312 KB |
Output is correct |
54 |
Correct |
26 ms |
24184 KB |
Output is correct |
55 |
Correct |
24 ms |
23840 KB |
Output is correct |
56 |
Correct |
120 ms |
37404 KB |
Output is correct |
57 |
Correct |
150 ms |
37368 KB |
Output is correct |
58 |
Correct |
26 ms |
24312 KB |
Output is correct |
59 |
Correct |
24 ms |
23928 KB |
Output is correct |
60 |
Correct |
28 ms |
23928 KB |
Output is correct |
61 |
Correct |
148 ms |
37624 KB |
Output is correct |
62 |
Correct |
31 ms |
24312 KB |
Output is correct |
63 |
Correct |
295 ms |
43256 KB |
Output is correct |
64 |
Correct |
184 ms |
37460 KB |
Output is correct |
65 |
Correct |
26 ms |
24312 KB |
Output is correct |
66 |
Correct |
175 ms |
38140 KB |
Output is correct |
67 |
Correct |
26 ms |
24316 KB |
Output is correct |
68 |
Correct |
26 ms |
24312 KB |
Output is correct |
69 |
Correct |
113 ms |
37332 KB |
Output is correct |
70 |
Correct |
27 ms |
24440 KB |
Output is correct |
71 |
Correct |
148 ms |
37488 KB |
Output is correct |
72 |
Correct |
199 ms |
39516 KB |
Output is correct |
73 |
Correct |
23 ms |
23800 KB |
Output is correct |
74 |
Correct |
202 ms |
43388 KB |
Output is correct |
75 |
Correct |
192 ms |
40448 KB |
Output is correct |
76 |
Correct |
26 ms |
24312 KB |
Output is correct |
77 |
Correct |
26 ms |
24312 KB |
Output is correct |
78 |
Correct |
115 ms |
37488 KB |
Output is correct |
79 |
Correct |
135 ms |
41128 KB |
Output is correct |
80 |
Correct |
32 ms |
24312 KB |
Output is correct |
81 |
Correct |
26 ms |
24312 KB |
Output is correct |
82 |
Correct |
27 ms |
23928 KB |
Output is correct |
83 |
Correct |
29 ms |
23928 KB |
Output is correct |
84 |
Correct |
26 ms |
24312 KB |
Output is correct |
85 |
Correct |
189 ms |
39304 KB |
Output is correct |
86 |
Correct |
26 ms |
24312 KB |
Output is correct |
87 |
Correct |
200 ms |
38276 KB |
Output is correct |
88 |
Correct |
30 ms |
23800 KB |
Output is correct |
89 |
Correct |
248 ms |
38244 KB |
Output is correct |
90 |
Correct |
184 ms |
39148 KB |
Output is correct |
91 |
Correct |
252 ms |
40784 KB |
Output is correct |
92 |
Correct |
115 ms |
37232 KB |
Output is correct |
93 |
Correct |
26 ms |
24312 KB |
Output is correct |
94 |
Correct |
24 ms |
23800 KB |
Output is correct |
95 |
Correct |
146 ms |
40816 KB |
Output is correct |
96 |
Correct |
210 ms |
44008 KB |
Output is correct |
97 |
Correct |
27 ms |
24312 KB |
Output is correct |
98 |
Correct |
23 ms |
23928 KB |
Output is correct |
99 |
Correct |
133 ms |
39532 KB |
Output is correct |
100 |
Correct |
26 ms |
24440 KB |
Output is correct |
101 |
Correct |
238 ms |
38616 KB |
Output is correct |
102 |
Correct |
199 ms |
42900 KB |
Output is correct |
103 |
Correct |
26 ms |
24312 KB |
Output is correct |
104 |
Correct |
26 ms |
24440 KB |
Output is correct |
105 |
Correct |
27 ms |
24312 KB |
Output is correct |
106 |
Correct |
25 ms |
24312 KB |
Output is correct |
107 |
Correct |
26 ms |
24312 KB |
Output is correct |
108 |
Correct |
774 ms |
100940 KB |
Output is correct |
109 |
Correct |
1317 ms |
105504 KB |
Output is correct |
110 |
Correct |
1494 ms |
114552 KB |
Output is correct |
111 |
Correct |
1487 ms |
127208 KB |
Output is correct |
112 |
Correct |
1517 ms |
119132 KB |
Output is correct |
113 |
Correct |
874 ms |
108560 KB |
Output is correct |
114 |
Correct |
890 ms |
91280 KB |
Output is correct |
115 |
Correct |
992 ms |
97776 KB |
Output is correct |
116 |
Correct |
1089 ms |
103804 KB |
Output is correct |
117 |
Correct |
1260 ms |
114732 KB |
Output is correct |
118 |
Correct |
1002 ms |
100272 KB |
Output is correct |
119 |
Correct |
1394 ms |
114768 KB |
Output is correct |
120 |
Correct |
1489 ms |
125584 KB |
Output is correct |
121 |
Correct |
1172 ms |
114512 KB |
Output is correct |
122 |
Correct |
1152 ms |
108512 KB |
Output is correct |
123 |
Correct |
904 ms |
115940 KB |
Output is correct |
124 |
Correct |
1268 ms |
112728 KB |
Output is correct |