#include<bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
#define sz(a) ((ll)(a).size())
using namespace std;
const ll maxn=1000005;
pll sp[20][maxn];
vector <pll> euler;
vector <ll> leaf, A[maxn], C[maxn];
ll P[maxn], col[maxn], lca[maxn], pa[maxn], dsu[maxn], cnt[maxn];
void dfs(ll u)
{
lca[u]=sz(euler)+1, euler.pb({lca[u], u});
for (ll v:A[u])
if (v!=pa[u])
pa[v]=u, dfs(v), euler.pb({lca[u], u});
if (sz(A[u])==1) leaf.pb(u);
}
void initlca()
{
ll n=sz(euler);
for (ll i=0; i<n; i++)
sp[0][i+1]=euler[i];
for (ll i=1; i<20; i++)
for (ll j=1; j+(1<<i)<=n+1; j++)
sp[i][j]=min(sp[i-1][j], sp[i-1][j+(1<<i-1)]);
}
ll LCA(ll u, ll v)
{
if (!u || !v) return u+v;
ll l=lca[u], r=lca[v];
if (l>r) swap(l, r);
ll j=__lg(r-l+1);
return min(sp[j][l], sp[j][r-(1<<j)+1]).se;
}
ll Find(ll i)
{
if (dsu[i]<0) return i;
return dsu[i]=Find(dsu[i]);
}
void Union(ll a, ll b)
{
a=Find(a), b=Find(b);
if (a==b) return;
if (dsu[a]>dsu[b]) swap(a, b);
P[a]=LCA(P[a], P[b]), dsu[a]+=dsu[b], dsu[b]=a;
}
void dfs1(ll u)
{
for (ll v:A[u])
if (v!=pa[u])
dfs1(v), cnt[u]+=cnt[v];
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n, k; cin >> n >> k;
for (ll i=1; i<n; i++)
{
ll u, v; cin >> u >> v;
A[u].pb(v), A[v].pb(u);
}
dfs(1), initlca();
for (ll i=1; i<=n; i++)
cin >> col[i], C[col[i]].pb(i), dsu[i]=-1;
for (ll i=1; i<=n; i++)
for (ll j=0; j+1<sz(C[i]); j++)
{
ll u=C[i][j], v=C[i][j+1];
cnt[u]++, cnt[v]++, cnt[LCA(u, v)]-=2;
}
dfs1(1);
for (ll i=1; i<=n; i++)
if (pa[i] && cnt[i])
Union(i, pa[i]);
for (ll i=1; i<=n; i++) P[i]=0;
for (ll i=1; i<=n; i++)
if (!cnt[i] && pa[i])
P[Find(i)]++, P[Find(pa[i])]++;
ll cnt=0;
for (ll i=1; i<=n; i++)
cnt+=P[i]==1;
cout << (cnt+1)/2;
}
Compilation message
mergers.cpp: In function 'void initlca()':
mergers.cpp:34:53: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
34 | sp[i][j]=min(sp[i-1][j], sp[i-1][j+(1<<i-1)]);
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
61788 KB |
Output is correct |
2 |
Correct |
19 ms |
70236 KB |
Output is correct |
3 |
Correct |
15 ms |
70236 KB |
Output is correct |
4 |
Correct |
14 ms |
64004 KB |
Output is correct |
5 |
Correct |
13 ms |
61788 KB |
Output is correct |
6 |
Correct |
13 ms |
70236 KB |
Output is correct |
7 |
Correct |
12 ms |
57692 KB |
Output is correct |
8 |
Correct |
13 ms |
70236 KB |
Output is correct |
9 |
Correct |
13 ms |
63836 KB |
Output is correct |
10 |
Correct |
13 ms |
59740 KB |
Output is correct |
11 |
Correct |
13 ms |
70236 KB |
Output is correct |
12 |
Correct |
13 ms |
70236 KB |
Output is correct |
13 |
Correct |
12 ms |
64092 KB |
Output is correct |
14 |
Correct |
13 ms |
70256 KB |
Output is correct |
15 |
Correct |
13 ms |
70236 KB |
Output is correct |
16 |
Correct |
12 ms |
63836 KB |
Output is correct |
17 |
Correct |
13 ms |
61788 KB |
Output is correct |
18 |
Correct |
14 ms |
70236 KB |
Output is correct |
19 |
Correct |
14 ms |
63836 KB |
Output is correct |
20 |
Correct |
15 ms |
64080 KB |
Output is correct |
21 |
Correct |
13 ms |
70236 KB |
Output is correct |
22 |
Correct |
13 ms |
70236 KB |
Output is correct |
23 |
Correct |
14 ms |
64088 KB |
Output is correct |
24 |
Correct |
12 ms |
63836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
61788 KB |
Output is correct |
2 |
Correct |
19 ms |
70236 KB |
Output is correct |
3 |
Correct |
15 ms |
70236 KB |
Output is correct |
4 |
Correct |
14 ms |
64004 KB |
Output is correct |
5 |
Correct |
13 ms |
61788 KB |
Output is correct |
6 |
Correct |
13 ms |
70236 KB |
Output is correct |
7 |
Correct |
12 ms |
57692 KB |
Output is correct |
8 |
Correct |
13 ms |
70236 KB |
Output is correct |
9 |
Correct |
13 ms |
63836 KB |
Output is correct |
10 |
Correct |
13 ms |
59740 KB |
Output is correct |
11 |
Correct |
13 ms |
70236 KB |
Output is correct |
12 |
Correct |
13 ms |
70236 KB |
Output is correct |
13 |
Correct |
12 ms |
64092 KB |
Output is correct |
14 |
Correct |
13 ms |
70256 KB |
Output is correct |
15 |
Correct |
13 ms |
70236 KB |
Output is correct |
16 |
Correct |
12 ms |
63836 KB |
Output is correct |
17 |
Correct |
13 ms |
61788 KB |
Output is correct |
18 |
Correct |
14 ms |
70236 KB |
Output is correct |
19 |
Correct |
14 ms |
63836 KB |
Output is correct |
20 |
Correct |
15 ms |
64080 KB |
Output is correct |
21 |
Correct |
13 ms |
70236 KB |
Output is correct |
22 |
Correct |
13 ms |
70236 KB |
Output is correct |
23 |
Correct |
14 ms |
64088 KB |
Output is correct |
24 |
Correct |
12 ms |
63836 KB |
Output is correct |
25 |
Correct |
14 ms |
68188 KB |
Output is correct |
26 |
Correct |
20 ms |
82780 KB |
Output is correct |
27 |
Correct |
16 ms |
82780 KB |
Output is correct |
28 |
Correct |
18 ms |
83036 KB |
Output is correct |
29 |
Correct |
17 ms |
82940 KB |
Output is correct |
30 |
Correct |
20 ms |
82780 KB |
Output is correct |
31 |
Correct |
14 ms |
68080 KB |
Output is correct |
32 |
Correct |
17 ms |
83036 KB |
Output is correct |
33 |
Correct |
13 ms |
68188 KB |
Output is correct |
34 |
Correct |
16 ms |
82968 KB |
Output is correct |
35 |
Correct |
18 ms |
84828 KB |
Output is correct |
36 |
Correct |
17 ms |
82780 KB |
Output is correct |
37 |
Correct |
20 ms |
82776 KB |
Output is correct |
38 |
Correct |
14 ms |
70232 KB |
Output is correct |
39 |
Correct |
19 ms |
82780 KB |
Output is correct |
40 |
Correct |
16 ms |
82780 KB |
Output is correct |
41 |
Correct |
19 ms |
82780 KB |
Output is correct |
42 |
Correct |
16 ms |
82780 KB |
Output is correct |
43 |
Correct |
16 ms |
83192 KB |
Output is correct |
44 |
Correct |
15 ms |
70236 KB |
Output is correct |
45 |
Correct |
18 ms |
82972 KB |
Output is correct |
46 |
Correct |
17 ms |
82864 KB |
Output is correct |
47 |
Correct |
14 ms |
70236 KB |
Output is correct |
48 |
Correct |
16 ms |
82968 KB |
Output is correct |
49 |
Correct |
16 ms |
84828 KB |
Output is correct |
50 |
Correct |
18 ms |
85084 KB |
Output is correct |
51 |
Correct |
17 ms |
82956 KB |
Output is correct |
52 |
Correct |
16 ms |
82780 KB |
Output is correct |
53 |
Correct |
17 ms |
82780 KB |
Output is correct |
54 |
Correct |
17 ms |
82900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
61788 KB |
Output is correct |
2 |
Correct |
19 ms |
70236 KB |
Output is correct |
3 |
Correct |
15 ms |
70236 KB |
Output is correct |
4 |
Correct |
14 ms |
64004 KB |
Output is correct |
5 |
Correct |
13 ms |
61788 KB |
Output is correct |
6 |
Correct |
13 ms |
70236 KB |
Output is correct |
7 |
Correct |
12 ms |
57692 KB |
Output is correct |
8 |
Correct |
13 ms |
70236 KB |
Output is correct |
9 |
Correct |
13 ms |
63836 KB |
Output is correct |
10 |
Correct |
13 ms |
59740 KB |
Output is correct |
11 |
Correct |
13 ms |
70236 KB |
Output is correct |
12 |
Correct |
13 ms |
70236 KB |
Output is correct |
13 |
Correct |
12 ms |
64092 KB |
Output is correct |
14 |
Correct |
13 ms |
70256 KB |
Output is correct |
15 |
Correct |
13 ms |
70236 KB |
Output is correct |
16 |
Correct |
12 ms |
63836 KB |
Output is correct |
17 |
Correct |
13 ms |
61788 KB |
Output is correct |
18 |
Correct |
14 ms |
70236 KB |
Output is correct |
19 |
Correct |
14 ms |
63836 KB |
Output is correct |
20 |
Correct |
15 ms |
64080 KB |
Output is correct |
21 |
Correct |
13 ms |
70236 KB |
Output is correct |
22 |
Correct |
13 ms |
70236 KB |
Output is correct |
23 |
Correct |
14 ms |
64088 KB |
Output is correct |
24 |
Correct |
12 ms |
63836 KB |
Output is correct |
25 |
Correct |
14 ms |
68188 KB |
Output is correct |
26 |
Correct |
86 ms |
156708 KB |
Output is correct |
27 |
Correct |
100 ms |
157036 KB |
Output is correct |
28 |
Correct |
16 ms |
82900 KB |
Output is correct |
29 |
Correct |
13 ms |
68200 KB |
Output is correct |
30 |
Correct |
14 ms |
68196 KB |
Output is correct |
31 |
Correct |
92 ms |
156156 KB |
Output is correct |
32 |
Correct |
17 ms |
82940 KB |
Output is correct |
33 |
Correct |
111 ms |
165308 KB |
Output is correct |
34 |
Correct |
84 ms |
156092 KB |
Output is correct |
35 |
Correct |
16 ms |
82780 KB |
Output is correct |
36 |
Correct |
112 ms |
156120 KB |
Output is correct |
37 |
Correct |
16 ms |
82780 KB |
Output is correct |
38 |
Correct |
16 ms |
82924 KB |
Output is correct |
39 |
Correct |
70 ms |
156724 KB |
Output is correct |
40 |
Correct |
17 ms |
83192 KB |
Output is correct |
41 |
Correct |
94 ms |
156764 KB |
Output is correct |
42 |
Correct |
88 ms |
158600 KB |
Output is correct |
43 |
Correct |
15 ms |
70236 KB |
Output is correct |
44 |
Correct |
102 ms |
167184 KB |
Output is correct |
45 |
Correct |
92 ms |
161972 KB |
Output is correct |
46 |
Correct |
16 ms |
82892 KB |
Output is correct |
47 |
Correct |
17 ms |
82780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
157752 KB |
Output is correct |
2 |
Correct |
84 ms |
159760 KB |
Output is correct |
3 |
Correct |
17 ms |
82960 KB |
Output is correct |
4 |
Correct |
19 ms |
82780 KB |
Output is correct |
5 |
Correct |
14 ms |
68184 KB |
Output is correct |
6 |
Correct |
14 ms |
68188 KB |
Output is correct |
7 |
Correct |
18 ms |
82780 KB |
Output is correct |
8 |
Correct |
97 ms |
157420 KB |
Output is correct |
9 |
Correct |
17 ms |
82780 KB |
Output is correct |
10 |
Correct |
85 ms |
156088 KB |
Output is correct |
11 |
Correct |
14 ms |
70236 KB |
Output is correct |
12 |
Correct |
89 ms |
155408 KB |
Output is correct |
13 |
Correct |
112 ms |
157484 KB |
Output is correct |
14 |
Correct |
101 ms |
159928 KB |
Output is correct |
15 |
Correct |
71 ms |
156980 KB |
Output is correct |
16 |
Correct |
17 ms |
82780 KB |
Output is correct |
17 |
Correct |
15 ms |
70144 KB |
Output is correct |
18 |
Correct |
78 ms |
158776 KB |
Output is correct |
19 |
Correct |
105 ms |
164704 KB |
Output is correct |
20 |
Correct |
16 ms |
82780 KB |
Output is correct |
21 |
Correct |
13 ms |
70128 KB |
Output is correct |
22 |
Correct |
76 ms |
157480 KB |
Output is correct |
23 |
Correct |
16 ms |
82776 KB |
Output is correct |
24 |
Correct |
115 ms |
156896 KB |
Output is correct |
25 |
Correct |
96 ms |
164020 KB |
Output is correct |
26 |
Correct |
16 ms |
84824 KB |
Output is correct |
27 |
Correct |
17 ms |
85080 KB |
Output is correct |
28 |
Correct |
20 ms |
83068 KB |
Output is correct |
29 |
Correct |
16 ms |
82780 KB |
Output is correct |
30 |
Correct |
16 ms |
82780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
61788 KB |
Output is correct |
2 |
Correct |
19 ms |
70236 KB |
Output is correct |
3 |
Correct |
15 ms |
70236 KB |
Output is correct |
4 |
Correct |
14 ms |
64004 KB |
Output is correct |
5 |
Correct |
13 ms |
61788 KB |
Output is correct |
6 |
Correct |
13 ms |
70236 KB |
Output is correct |
7 |
Correct |
12 ms |
57692 KB |
Output is correct |
8 |
Correct |
13 ms |
70236 KB |
Output is correct |
9 |
Correct |
13 ms |
63836 KB |
Output is correct |
10 |
Correct |
13 ms |
59740 KB |
Output is correct |
11 |
Correct |
13 ms |
70236 KB |
Output is correct |
12 |
Correct |
13 ms |
70236 KB |
Output is correct |
13 |
Correct |
12 ms |
64092 KB |
Output is correct |
14 |
Correct |
13 ms |
70256 KB |
Output is correct |
15 |
Correct |
13 ms |
70236 KB |
Output is correct |
16 |
Correct |
12 ms |
63836 KB |
Output is correct |
17 |
Correct |
13 ms |
61788 KB |
Output is correct |
18 |
Correct |
14 ms |
70236 KB |
Output is correct |
19 |
Correct |
14 ms |
63836 KB |
Output is correct |
20 |
Correct |
15 ms |
64080 KB |
Output is correct |
21 |
Correct |
13 ms |
70236 KB |
Output is correct |
22 |
Correct |
13 ms |
70236 KB |
Output is correct |
23 |
Correct |
14 ms |
64088 KB |
Output is correct |
24 |
Correct |
12 ms |
63836 KB |
Output is correct |
25 |
Correct |
14 ms |
68188 KB |
Output is correct |
26 |
Correct |
20 ms |
82780 KB |
Output is correct |
27 |
Correct |
16 ms |
82780 KB |
Output is correct |
28 |
Correct |
18 ms |
83036 KB |
Output is correct |
29 |
Correct |
17 ms |
82940 KB |
Output is correct |
30 |
Correct |
20 ms |
82780 KB |
Output is correct |
31 |
Correct |
14 ms |
68080 KB |
Output is correct |
32 |
Correct |
17 ms |
83036 KB |
Output is correct |
33 |
Correct |
13 ms |
68188 KB |
Output is correct |
34 |
Correct |
16 ms |
82968 KB |
Output is correct |
35 |
Correct |
18 ms |
84828 KB |
Output is correct |
36 |
Correct |
17 ms |
82780 KB |
Output is correct |
37 |
Correct |
20 ms |
82776 KB |
Output is correct |
38 |
Correct |
14 ms |
70232 KB |
Output is correct |
39 |
Correct |
19 ms |
82780 KB |
Output is correct |
40 |
Correct |
16 ms |
82780 KB |
Output is correct |
41 |
Correct |
19 ms |
82780 KB |
Output is correct |
42 |
Correct |
16 ms |
82780 KB |
Output is correct |
43 |
Correct |
16 ms |
83192 KB |
Output is correct |
44 |
Correct |
15 ms |
70236 KB |
Output is correct |
45 |
Correct |
18 ms |
82972 KB |
Output is correct |
46 |
Correct |
17 ms |
82864 KB |
Output is correct |
47 |
Correct |
14 ms |
70236 KB |
Output is correct |
48 |
Correct |
16 ms |
82968 KB |
Output is correct |
49 |
Correct |
16 ms |
84828 KB |
Output is correct |
50 |
Correct |
18 ms |
85084 KB |
Output is correct |
51 |
Correct |
17 ms |
82956 KB |
Output is correct |
52 |
Correct |
16 ms |
82780 KB |
Output is correct |
53 |
Correct |
17 ms |
82780 KB |
Output is correct |
54 |
Correct |
17 ms |
82900 KB |
Output is correct |
55 |
Correct |
14 ms |
68188 KB |
Output is correct |
56 |
Correct |
86 ms |
156708 KB |
Output is correct |
57 |
Correct |
100 ms |
157036 KB |
Output is correct |
58 |
Correct |
16 ms |
82900 KB |
Output is correct |
59 |
Correct |
13 ms |
68200 KB |
Output is correct |
60 |
Correct |
14 ms |
68196 KB |
Output is correct |
61 |
Correct |
92 ms |
156156 KB |
Output is correct |
62 |
Correct |
17 ms |
82940 KB |
Output is correct |
63 |
Correct |
111 ms |
165308 KB |
Output is correct |
64 |
Correct |
84 ms |
156092 KB |
Output is correct |
65 |
Correct |
16 ms |
82780 KB |
Output is correct |
66 |
Correct |
112 ms |
156120 KB |
Output is correct |
67 |
Correct |
16 ms |
82780 KB |
Output is correct |
68 |
Correct |
16 ms |
82924 KB |
Output is correct |
69 |
Correct |
70 ms |
156724 KB |
Output is correct |
70 |
Correct |
17 ms |
83192 KB |
Output is correct |
71 |
Correct |
94 ms |
156764 KB |
Output is correct |
72 |
Correct |
88 ms |
158600 KB |
Output is correct |
73 |
Correct |
15 ms |
70236 KB |
Output is correct |
74 |
Correct |
102 ms |
167184 KB |
Output is correct |
75 |
Correct |
92 ms |
161972 KB |
Output is correct |
76 |
Correct |
16 ms |
82892 KB |
Output is correct |
77 |
Correct |
17 ms |
82780 KB |
Output is correct |
78 |
Correct |
70 ms |
157752 KB |
Output is correct |
79 |
Correct |
84 ms |
159760 KB |
Output is correct |
80 |
Correct |
17 ms |
82960 KB |
Output is correct |
81 |
Correct |
19 ms |
82780 KB |
Output is correct |
82 |
Correct |
14 ms |
68184 KB |
Output is correct |
83 |
Correct |
14 ms |
68188 KB |
Output is correct |
84 |
Correct |
18 ms |
82780 KB |
Output is correct |
85 |
Correct |
97 ms |
157420 KB |
Output is correct |
86 |
Correct |
17 ms |
82780 KB |
Output is correct |
87 |
Correct |
85 ms |
156088 KB |
Output is correct |
88 |
Correct |
14 ms |
70236 KB |
Output is correct |
89 |
Correct |
89 ms |
155408 KB |
Output is correct |
90 |
Correct |
112 ms |
157484 KB |
Output is correct |
91 |
Correct |
101 ms |
159928 KB |
Output is correct |
92 |
Correct |
71 ms |
156980 KB |
Output is correct |
93 |
Correct |
17 ms |
82780 KB |
Output is correct |
94 |
Correct |
15 ms |
70144 KB |
Output is correct |
95 |
Correct |
78 ms |
158776 KB |
Output is correct |
96 |
Correct |
105 ms |
164704 KB |
Output is correct |
97 |
Correct |
16 ms |
82780 KB |
Output is correct |
98 |
Correct |
13 ms |
70128 KB |
Output is correct |
99 |
Correct |
76 ms |
157480 KB |
Output is correct |
100 |
Correct |
16 ms |
82776 KB |
Output is correct |
101 |
Correct |
115 ms |
156896 KB |
Output is correct |
102 |
Correct |
96 ms |
164020 KB |
Output is correct |
103 |
Correct |
16 ms |
84824 KB |
Output is correct |
104 |
Correct |
17 ms |
85080 KB |
Output is correct |
105 |
Correct |
20 ms |
83068 KB |
Output is correct |
106 |
Correct |
16 ms |
82780 KB |
Output is correct |
107 |
Correct |
16 ms |
82780 KB |
Output is correct |
108 |
Runtime error |
219 ms |
262144 KB |
Execution killed with signal 9 |
109 |
Halted |
0 ms |
0 KB |
- |