#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 5e5;
int N, K;
vector<int> adj[MAXN+10], A[MAXN+10];
int par[MAXN+10][30], dep[MAXN+10];
void dfs(int now, int bef, int d)
{
par[now][0]=bef; dep[now]=d;
for(int nxt : adj[now])
{
if(nxt==bef) continue;
dfs(nxt, now, d+1);
}
}
int lca(int u, int v)
{
int i, j;
if(dep[u]>dep[v]) swap(u, v);
for(i=20; i>=0; i--) if(dep[par[v][i]]>=dep[u]) v=par[v][i];
if(u==v) return u;
for(i=20; i>=0; i--) if(par[v][i]!=par[u][i]) v=par[v][i], u=par[u][i];
return par[u][0];
}
int B[MAXN+10], ppar[MAXN+10], cnt[MAXN+10], ans;
int Find(int x) { return x==ppar[x] ? x : ppar[x]=Find(ppar[x]); }
void Union(int x, int y) { x=Find(x); y=Find(y); ppar[x]=y; }
void dfs2(int now, int bef)
{
for(int nxt : adj[now])
{
if(nxt==bef) continue;
dfs2(nxt, now);
B[now]+=B[nxt];
}
if(now!=bef && B[now]) Union(now, bef);
}
int main()
{
int i, j;
scanf("%d%d", &N, &K);
for(i=1; i<=N; i++) ppar[i]=i;
for(i=1; i<N; i++)
{
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for(i=1; i<=N; i++)
{
int t;
scanf("%d", &t);
A[t].push_back(i);
}
dfs(1, 1, 1);
for(i=1; i<=20; i++) for(j=1; j<=N; j++) par[j][i]=par[par[j][i-1]][i-1];
for(i=1; i<=K; i++)
{
for(j=1; j<A[i].size(); j++)
{
int u=A[i][j-1], v=A[i][j], w=lca(u, v);
B[u]++; B[v]++; B[w]--; B[w]--;
}
}
dfs2(1, 1);
for(i=2; i<=N; i++)
{
int u=i, v=par[u][0];
if(Find(u)==Find(v)) continue;
cnt[Find(u)]++; cnt[Find(v)]++;
}
for(i=1; i<=N; i++) if(cnt[i]==1) ans++;
if(ans==1) ans=0;
ans++; ans/=2;
printf("%d", ans);
}
Compilation message
mergers.cpp: In function 'int lca(int, int)':
mergers.cpp:26:9: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
mergers.cpp: In function 'int main()':
mergers.cpp:75:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=1; j<A[i].size(); j++)
~^~~~~~~~~~~~
mergers.cpp:54:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &K);
~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:59:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:66:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &t);
~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
23800 KB |
Output is correct |
2 |
Correct |
19 ms |
23800 KB |
Output is correct |
3 |
Correct |
19 ms |
23804 KB |
Output is correct |
4 |
Correct |
19 ms |
23800 KB |
Output is correct |
5 |
Correct |
19 ms |
23800 KB |
Output is correct |
6 |
Correct |
19 ms |
23800 KB |
Output is correct |
7 |
Correct |
20 ms |
23800 KB |
Output is correct |
8 |
Correct |
20 ms |
23800 KB |
Output is correct |
9 |
Correct |
19 ms |
23800 KB |
Output is correct |
10 |
Correct |
19 ms |
23800 KB |
Output is correct |
11 |
Correct |
19 ms |
23800 KB |
Output is correct |
12 |
Correct |
21 ms |
23928 KB |
Output is correct |
13 |
Correct |
19 ms |
23800 KB |
Output is correct |
14 |
Correct |
19 ms |
23800 KB |
Output is correct |
15 |
Correct |
20 ms |
23800 KB |
Output is correct |
16 |
Correct |
19 ms |
23800 KB |
Output is correct |
17 |
Correct |
19 ms |
23800 KB |
Output is correct |
18 |
Correct |
19 ms |
23800 KB |
Output is correct |
19 |
Correct |
19 ms |
23800 KB |
Output is correct |
20 |
Correct |
19 ms |
23928 KB |
Output is correct |
21 |
Correct |
19 ms |
23800 KB |
Output is correct |
22 |
Correct |
19 ms |
23672 KB |
Output is correct |
23 |
Correct |
19 ms |
23800 KB |
Output is correct |
24 |
Correct |
19 ms |
23800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
23800 KB |
Output is correct |
2 |
Correct |
19 ms |
23800 KB |
Output is correct |
3 |
Correct |
19 ms |
23804 KB |
Output is correct |
4 |
Correct |
19 ms |
23800 KB |
Output is correct |
5 |
Correct |
19 ms |
23800 KB |
Output is correct |
6 |
Correct |
19 ms |
23800 KB |
Output is correct |
7 |
Correct |
20 ms |
23800 KB |
Output is correct |
8 |
Correct |
20 ms |
23800 KB |
Output is correct |
9 |
Correct |
19 ms |
23800 KB |
Output is correct |
10 |
Correct |
19 ms |
23800 KB |
Output is correct |
11 |
Correct |
19 ms |
23800 KB |
Output is correct |
12 |
Correct |
21 ms |
23928 KB |
Output is correct |
13 |
Correct |
19 ms |
23800 KB |
Output is correct |
14 |
Correct |
19 ms |
23800 KB |
Output is correct |
15 |
Correct |
20 ms |
23800 KB |
Output is correct |
16 |
Correct |
19 ms |
23800 KB |
Output is correct |
17 |
Correct |
19 ms |
23800 KB |
Output is correct |
18 |
Correct |
19 ms |
23800 KB |
Output is correct |
19 |
Correct |
19 ms |
23800 KB |
Output is correct |
20 |
Correct |
19 ms |
23928 KB |
Output is correct |
21 |
Correct |
19 ms |
23800 KB |
Output is correct |
22 |
Correct |
19 ms |
23672 KB |
Output is correct |
23 |
Correct |
19 ms |
23800 KB |
Output is correct |
24 |
Correct |
19 ms |
23800 KB |
Output is correct |
25 |
Correct |
18 ms |
23800 KB |
Output is correct |
26 |
Correct |
22 ms |
24440 KB |
Output is correct |
27 |
Correct |
23 ms |
24440 KB |
Output is correct |
28 |
Correct |
22 ms |
24568 KB |
Output is correct |
29 |
Correct |
21 ms |
24440 KB |
Output is correct |
30 |
Correct |
21 ms |
24440 KB |
Output is correct |
31 |
Correct |
19 ms |
23800 KB |
Output is correct |
32 |
Correct |
21 ms |
24568 KB |
Output is correct |
33 |
Correct |
19 ms |
23800 KB |
Output is correct |
34 |
Correct |
21 ms |
24312 KB |
Output is correct |
35 |
Correct |
22 ms |
24440 KB |
Output is correct |
36 |
Correct |
22 ms |
24440 KB |
Output is correct |
37 |
Correct |
22 ms |
24440 KB |
Output is correct |
38 |
Correct |
19 ms |
23928 KB |
Output is correct |
39 |
Correct |
22 ms |
24440 KB |
Output is correct |
40 |
Correct |
22 ms |
24312 KB |
Output is correct |
41 |
Correct |
22 ms |
24312 KB |
Output is correct |
42 |
Correct |
21 ms |
24568 KB |
Output is correct |
43 |
Correct |
22 ms |
24568 KB |
Output is correct |
44 |
Correct |
20 ms |
23800 KB |
Output is correct |
45 |
Correct |
22 ms |
24440 KB |
Output is correct |
46 |
Correct |
22 ms |
24440 KB |
Output is correct |
47 |
Correct |
19 ms |
23800 KB |
Output is correct |
48 |
Correct |
22 ms |
24440 KB |
Output is correct |
49 |
Correct |
21 ms |
24568 KB |
Output is correct |
50 |
Correct |
22 ms |
24440 KB |
Output is correct |
51 |
Correct |
21 ms |
24440 KB |
Output is correct |
52 |
Correct |
22 ms |
24440 KB |
Output is correct |
53 |
Correct |
22 ms |
24440 KB |
Output is correct |
54 |
Correct |
22 ms |
24312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
23800 KB |
Output is correct |
2 |
Correct |
19 ms |
23800 KB |
Output is correct |
3 |
Correct |
19 ms |
23804 KB |
Output is correct |
4 |
Correct |
19 ms |
23800 KB |
Output is correct |
5 |
Correct |
19 ms |
23800 KB |
Output is correct |
6 |
Correct |
19 ms |
23800 KB |
Output is correct |
7 |
Correct |
20 ms |
23800 KB |
Output is correct |
8 |
Correct |
20 ms |
23800 KB |
Output is correct |
9 |
Correct |
19 ms |
23800 KB |
Output is correct |
10 |
Correct |
19 ms |
23800 KB |
Output is correct |
11 |
Correct |
19 ms |
23800 KB |
Output is correct |
12 |
Correct |
21 ms |
23928 KB |
Output is correct |
13 |
Correct |
19 ms |
23800 KB |
Output is correct |
14 |
Correct |
19 ms |
23800 KB |
Output is correct |
15 |
Correct |
20 ms |
23800 KB |
Output is correct |
16 |
Correct |
19 ms |
23800 KB |
Output is correct |
17 |
Correct |
19 ms |
23800 KB |
Output is correct |
18 |
Correct |
19 ms |
23800 KB |
Output is correct |
19 |
Correct |
19 ms |
23800 KB |
Output is correct |
20 |
Correct |
19 ms |
23928 KB |
Output is correct |
21 |
Correct |
19 ms |
23800 KB |
Output is correct |
22 |
Correct |
19 ms |
23672 KB |
Output is correct |
23 |
Correct |
19 ms |
23800 KB |
Output is correct |
24 |
Correct |
19 ms |
23800 KB |
Output is correct |
25 |
Correct |
19 ms |
23800 KB |
Output is correct |
26 |
Correct |
147 ms |
42280 KB |
Output is correct |
27 |
Correct |
196 ms |
41952 KB |
Output is correct |
28 |
Correct |
21 ms |
24312 KB |
Output is correct |
29 |
Correct |
19 ms |
23800 KB |
Output is correct |
30 |
Correct |
19 ms |
23800 KB |
Output is correct |
31 |
Correct |
171 ms |
42104 KB |
Output is correct |
32 |
Correct |
21 ms |
24440 KB |
Output is correct |
33 |
Correct |
245 ms |
47608 KB |
Output is correct |
34 |
Correct |
192 ms |
42104 KB |
Output is correct |
35 |
Correct |
23 ms |
24440 KB |
Output is correct |
36 |
Correct |
207 ms |
42548 KB |
Output is correct |
37 |
Correct |
22 ms |
24312 KB |
Output is correct |
38 |
Correct |
22 ms |
24312 KB |
Output is correct |
39 |
Correct |
142 ms |
42264 KB |
Output is correct |
40 |
Correct |
22 ms |
24568 KB |
Output is correct |
41 |
Correct |
174 ms |
41844 KB |
Output is correct |
42 |
Correct |
265 ms |
43896 KB |
Output is correct |
43 |
Correct |
19 ms |
23800 KB |
Output is correct |
44 |
Correct |
238 ms |
47860 KB |
Output is correct |
45 |
Correct |
246 ms |
45048 KB |
Output is correct |
46 |
Correct |
22 ms |
24440 KB |
Output is correct |
47 |
Correct |
23 ms |
24312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
139 ms |
40924 KB |
Output is correct |
2 |
Correct |
120 ms |
43376 KB |
Output is correct |
3 |
Correct |
21 ms |
24440 KB |
Output is correct |
4 |
Correct |
22 ms |
24440 KB |
Output is correct |
5 |
Correct |
19 ms |
23804 KB |
Output is correct |
6 |
Correct |
21 ms |
23800 KB |
Output is correct |
7 |
Correct |
21 ms |
24440 KB |
Output is correct |
8 |
Correct |
183 ms |
43640 KB |
Output is correct |
9 |
Correct |
22 ms |
24312 KB |
Output is correct |
10 |
Correct |
177 ms |
42488 KB |
Output is correct |
11 |
Correct |
19 ms |
23800 KB |
Output is correct |
12 |
Correct |
188 ms |
42360 KB |
Output is correct |
13 |
Correct |
180 ms |
43640 KB |
Output is correct |
14 |
Correct |
156 ms |
45304 KB |
Output is correct |
15 |
Correct |
141 ms |
42220 KB |
Output is correct |
16 |
Correct |
22 ms |
24440 KB |
Output is correct |
17 |
Correct |
19 ms |
24056 KB |
Output is correct |
18 |
Correct |
129 ms |
45296 KB |
Output is correct |
19 |
Correct |
181 ms |
48376 KB |
Output is correct |
20 |
Correct |
22 ms |
24440 KB |
Output is correct |
21 |
Correct |
19 ms |
23800 KB |
Output is correct |
22 |
Correct |
144 ms |
43964 KB |
Output is correct |
23 |
Correct |
22 ms |
24440 KB |
Output is correct |
24 |
Correct |
189 ms |
43000 KB |
Output is correct |
25 |
Correct |
163 ms |
47352 KB |
Output is correct |
26 |
Correct |
21 ms |
24440 KB |
Output is correct |
27 |
Correct |
22 ms |
24440 KB |
Output is correct |
28 |
Correct |
21 ms |
24440 KB |
Output is correct |
29 |
Correct |
22 ms |
24440 KB |
Output is correct |
30 |
Correct |
23 ms |
24440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
23800 KB |
Output is correct |
2 |
Correct |
19 ms |
23800 KB |
Output is correct |
3 |
Correct |
19 ms |
23804 KB |
Output is correct |
4 |
Correct |
19 ms |
23800 KB |
Output is correct |
5 |
Correct |
19 ms |
23800 KB |
Output is correct |
6 |
Correct |
19 ms |
23800 KB |
Output is correct |
7 |
Correct |
20 ms |
23800 KB |
Output is correct |
8 |
Correct |
20 ms |
23800 KB |
Output is correct |
9 |
Correct |
19 ms |
23800 KB |
Output is correct |
10 |
Correct |
19 ms |
23800 KB |
Output is correct |
11 |
Correct |
19 ms |
23800 KB |
Output is correct |
12 |
Correct |
21 ms |
23928 KB |
Output is correct |
13 |
Correct |
19 ms |
23800 KB |
Output is correct |
14 |
Correct |
19 ms |
23800 KB |
Output is correct |
15 |
Correct |
20 ms |
23800 KB |
Output is correct |
16 |
Correct |
19 ms |
23800 KB |
Output is correct |
17 |
Correct |
19 ms |
23800 KB |
Output is correct |
18 |
Correct |
19 ms |
23800 KB |
Output is correct |
19 |
Correct |
19 ms |
23800 KB |
Output is correct |
20 |
Correct |
19 ms |
23928 KB |
Output is correct |
21 |
Correct |
19 ms |
23800 KB |
Output is correct |
22 |
Correct |
19 ms |
23672 KB |
Output is correct |
23 |
Correct |
19 ms |
23800 KB |
Output is correct |
24 |
Correct |
19 ms |
23800 KB |
Output is correct |
25 |
Correct |
18 ms |
23800 KB |
Output is correct |
26 |
Correct |
22 ms |
24440 KB |
Output is correct |
27 |
Correct |
23 ms |
24440 KB |
Output is correct |
28 |
Correct |
22 ms |
24568 KB |
Output is correct |
29 |
Correct |
21 ms |
24440 KB |
Output is correct |
30 |
Correct |
21 ms |
24440 KB |
Output is correct |
31 |
Correct |
19 ms |
23800 KB |
Output is correct |
32 |
Correct |
21 ms |
24568 KB |
Output is correct |
33 |
Correct |
19 ms |
23800 KB |
Output is correct |
34 |
Correct |
21 ms |
24312 KB |
Output is correct |
35 |
Correct |
22 ms |
24440 KB |
Output is correct |
36 |
Correct |
22 ms |
24440 KB |
Output is correct |
37 |
Correct |
22 ms |
24440 KB |
Output is correct |
38 |
Correct |
19 ms |
23928 KB |
Output is correct |
39 |
Correct |
22 ms |
24440 KB |
Output is correct |
40 |
Correct |
22 ms |
24312 KB |
Output is correct |
41 |
Correct |
22 ms |
24312 KB |
Output is correct |
42 |
Correct |
21 ms |
24568 KB |
Output is correct |
43 |
Correct |
22 ms |
24568 KB |
Output is correct |
44 |
Correct |
20 ms |
23800 KB |
Output is correct |
45 |
Correct |
22 ms |
24440 KB |
Output is correct |
46 |
Correct |
22 ms |
24440 KB |
Output is correct |
47 |
Correct |
19 ms |
23800 KB |
Output is correct |
48 |
Correct |
22 ms |
24440 KB |
Output is correct |
49 |
Correct |
21 ms |
24568 KB |
Output is correct |
50 |
Correct |
22 ms |
24440 KB |
Output is correct |
51 |
Correct |
21 ms |
24440 KB |
Output is correct |
52 |
Correct |
22 ms |
24440 KB |
Output is correct |
53 |
Correct |
22 ms |
24440 KB |
Output is correct |
54 |
Correct |
22 ms |
24312 KB |
Output is correct |
55 |
Correct |
19 ms |
23800 KB |
Output is correct |
56 |
Correct |
147 ms |
42280 KB |
Output is correct |
57 |
Correct |
196 ms |
41952 KB |
Output is correct |
58 |
Correct |
21 ms |
24312 KB |
Output is correct |
59 |
Correct |
19 ms |
23800 KB |
Output is correct |
60 |
Correct |
19 ms |
23800 KB |
Output is correct |
61 |
Correct |
171 ms |
42104 KB |
Output is correct |
62 |
Correct |
21 ms |
24440 KB |
Output is correct |
63 |
Correct |
245 ms |
47608 KB |
Output is correct |
64 |
Correct |
192 ms |
42104 KB |
Output is correct |
65 |
Correct |
23 ms |
24440 KB |
Output is correct |
66 |
Correct |
207 ms |
42548 KB |
Output is correct |
67 |
Correct |
22 ms |
24312 KB |
Output is correct |
68 |
Correct |
22 ms |
24312 KB |
Output is correct |
69 |
Correct |
142 ms |
42264 KB |
Output is correct |
70 |
Correct |
22 ms |
24568 KB |
Output is correct |
71 |
Correct |
174 ms |
41844 KB |
Output is correct |
72 |
Correct |
265 ms |
43896 KB |
Output is correct |
73 |
Correct |
19 ms |
23800 KB |
Output is correct |
74 |
Correct |
238 ms |
47860 KB |
Output is correct |
75 |
Correct |
246 ms |
45048 KB |
Output is correct |
76 |
Correct |
22 ms |
24440 KB |
Output is correct |
77 |
Correct |
23 ms |
24312 KB |
Output is correct |
78 |
Correct |
139 ms |
40924 KB |
Output is correct |
79 |
Correct |
120 ms |
43376 KB |
Output is correct |
80 |
Correct |
21 ms |
24440 KB |
Output is correct |
81 |
Correct |
22 ms |
24440 KB |
Output is correct |
82 |
Correct |
19 ms |
23804 KB |
Output is correct |
83 |
Correct |
21 ms |
23800 KB |
Output is correct |
84 |
Correct |
21 ms |
24440 KB |
Output is correct |
85 |
Correct |
183 ms |
43640 KB |
Output is correct |
86 |
Correct |
22 ms |
24312 KB |
Output is correct |
87 |
Correct |
177 ms |
42488 KB |
Output is correct |
88 |
Correct |
19 ms |
23800 KB |
Output is correct |
89 |
Correct |
188 ms |
42360 KB |
Output is correct |
90 |
Correct |
180 ms |
43640 KB |
Output is correct |
91 |
Correct |
156 ms |
45304 KB |
Output is correct |
92 |
Correct |
141 ms |
42220 KB |
Output is correct |
93 |
Correct |
22 ms |
24440 KB |
Output is correct |
94 |
Correct |
19 ms |
24056 KB |
Output is correct |
95 |
Correct |
129 ms |
45296 KB |
Output is correct |
96 |
Correct |
181 ms |
48376 KB |
Output is correct |
97 |
Correct |
22 ms |
24440 KB |
Output is correct |
98 |
Correct |
19 ms |
23800 KB |
Output is correct |
99 |
Correct |
144 ms |
43964 KB |
Output is correct |
100 |
Correct |
22 ms |
24440 KB |
Output is correct |
101 |
Correct |
189 ms |
43000 KB |
Output is correct |
102 |
Correct |
163 ms |
47352 KB |
Output is correct |
103 |
Correct |
21 ms |
24440 KB |
Output is correct |
104 |
Correct |
22 ms |
24440 KB |
Output is correct |
105 |
Correct |
21 ms |
24440 KB |
Output is correct |
106 |
Correct |
22 ms |
24440 KB |
Output is correct |
107 |
Correct |
23 ms |
24440 KB |
Output is correct |
108 |
Correct |
869 ms |
124388 KB |
Output is correct |
109 |
Correct |
1916 ms |
128816 KB |
Output is correct |
110 |
Correct |
2067 ms |
137776 KB |
Output is correct |
111 |
Correct |
1250 ms |
150648 KB |
Output is correct |
112 |
Correct |
1165 ms |
142764 KB |
Output is correct |
113 |
Correct |
775 ms |
132316 KB |
Output is correct |
114 |
Correct |
1206 ms |
114584 KB |
Output is correct |
115 |
Correct |
1183 ms |
114536 KB |
Output is correct |
116 |
Correct |
1347 ms |
120824 KB |
Output is correct |
117 |
Correct |
1058 ms |
132320 KB |
Output is correct |
118 |
Correct |
1093 ms |
116092 KB |
Output is correct |
119 |
Correct |
1031 ms |
132728 KB |
Output is correct |
120 |
Correct |
1191 ms |
143268 KB |
Output is correct |
121 |
Correct |
1022 ms |
132348 KB |
Output is correct |
122 |
Correct |
1268 ms |
125920 KB |
Output is correct |
123 |
Correct |
745 ms |
132060 KB |
Output is correct |
124 |
Correct |
1193 ms |
130136 KB |
Output is correct |