#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 5e5 + 5, maxk = 5e5+5, lg = 20;
int n, k;
vector<int> ADJ[maxn];
int A[maxn];
int SZ[maxn], FA[maxn], newid[maxn], lim[maxn], cnt = 0;
bool cmp(int x, int y) {
return SZ[x] > SZ[y];
}
void DFS0(int u) {
SZ[u] = 1;
for (int v:ADJ[u]) {
FA[v] = u;
ADJ[v].erase(find(ADJ[v].begin(), ADJ[v].end(), u));
DFS0(v);
SZ[u] += SZ[v];
}
sort(ADJ[u].begin(), ADJ[u].end(), cmp);
}
void DFS1(int u) {
cnt++;
newid[u] = cnt;
for (int v:ADJ[u]) DFS1(v);
lim[newid[u]] = cnt;
}
vector<int> adj[maxn];
int fa[maxn], sz[maxn];
vector<int> mem[maxk];
int a[maxn];
int st[maxn][lg];
void dfs0(int u) {
for (int i=1;i<lg;i++) {
if (!st[u][i-1] || !st[st[u][i-1]][i-1]) break;
st[u][i] = st[st[u][i-1]][i-1];
}
for (int v:adj[u]) {
st[v][0] = u;
dfs0(v);
}
}
int psum[maxn];
int lca(int u, int c) {
int l = mem[c][0], r = mem[c].back();
for (int i=lg-1;i>=0;i--) if (st[u][i]) {
int v = st[u][i];
if (!(v<=l && r<=lim[v])) u = v;
}
if (!(u<=l && r<=lim[u])) u = fa[u];
return u;
}
bool sep[maxn], sepleaf[maxn];
int sum[maxn];
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> k;
for (int i=1;i<=n-1;i++) {
int u, v;
cin >> u >> v;
ADJ[u].push_back(v), ADJ[v].push_back(u);
}
for (int i=1;i<=n;i++) cin >> A[i];
DFS0(1);
DFS1(1);
// for (int i=1;i<=n;i++) cout << newid[i] << " "; cout << endl;
// for (int i=1;i<=n;i++) cout << lim[i] << " "; cout << endl;
for (int i=1;i<=n;i++) {
for (int j:ADJ[i]) adj[newid[i]].push_back(newid[j]);
if (i!=1) fa[newid[i]] = newid[FA[i]];
else fa[i] = 0;
sz[newid[i]] = SZ[i];
}
for (int i=1;i<=n;i++) {
a[newid[i]] = A[i];
mem[A[i]].push_back(newid[i]);
}
dfs0(1);
for (int i=1;i<=k;i++) {
sort(mem[i].begin(), mem[i].end());
psum[lca(mem[i][0], i)] += mem[i].size();
}
for (int i=1;i<=n;i++) psum[i] += psum[i-1];
for (int i=2;i<=n;i++) {
sep[i] = (psum[lim[i]] - psum[i-1] == lim[i] - i + 1);
// cout << psum[lim[i]] - psum[i-1] << " " << lim[i] - i + 1 << endl;
}
// for (int i=1;i<=n;i++) cout << sep[i]; cout << endl;
for (int i=1;i<=n;i++) sum[i] = sum[i-1] + sep[i];
for (int i=1;i<=n;i++) sepleaf[i] = (sep[i] && sum[lim[i]] - sum[i-1] == 1);
bool add = false;
for (int i=2;i<=n;i++) if (sum[lim[i]] - sum[i-1] == sum[n] && sep[i] && !sepleaf[i]) add = true;
int ans = 0;
for (int i=1;i<=n;i++) ans += sepleaf[i];
if (ans%2==1) cout << (ans+1)/2 << endl;
else cout << ans/2 + add << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
35676 KB |
Output is correct |
2 |
Correct |
14 ms |
35572 KB |
Output is correct |
3 |
Correct |
14 ms |
35784 KB |
Output is correct |
4 |
Correct |
15 ms |
35676 KB |
Output is correct |
5 |
Correct |
15 ms |
35676 KB |
Output is correct |
6 |
Correct |
14 ms |
35572 KB |
Output is correct |
7 |
Correct |
14 ms |
35676 KB |
Output is correct |
8 |
Correct |
15 ms |
35600 KB |
Output is correct |
9 |
Correct |
18 ms |
35676 KB |
Output is correct |
10 |
Correct |
14 ms |
35676 KB |
Output is correct |
11 |
Correct |
14 ms |
35588 KB |
Output is correct |
12 |
Correct |
14 ms |
35676 KB |
Output is correct |
13 |
Correct |
16 ms |
35612 KB |
Output is correct |
14 |
Correct |
15 ms |
35596 KB |
Output is correct |
15 |
Correct |
15 ms |
35804 KB |
Output is correct |
16 |
Correct |
14 ms |
35676 KB |
Output is correct |
17 |
Correct |
14 ms |
35764 KB |
Output is correct |
18 |
Correct |
15 ms |
35792 KB |
Output is correct |
19 |
Correct |
14 ms |
35676 KB |
Output is correct |
20 |
Correct |
14 ms |
35648 KB |
Output is correct |
21 |
Correct |
14 ms |
35676 KB |
Output is correct |
22 |
Correct |
14 ms |
35676 KB |
Output is correct |
23 |
Correct |
14 ms |
35656 KB |
Output is correct |
24 |
Correct |
15 ms |
35676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
35676 KB |
Output is correct |
2 |
Correct |
14 ms |
35572 KB |
Output is correct |
3 |
Correct |
14 ms |
35784 KB |
Output is correct |
4 |
Correct |
15 ms |
35676 KB |
Output is correct |
5 |
Correct |
15 ms |
35676 KB |
Output is correct |
6 |
Correct |
14 ms |
35572 KB |
Output is correct |
7 |
Correct |
14 ms |
35676 KB |
Output is correct |
8 |
Correct |
15 ms |
35600 KB |
Output is correct |
9 |
Correct |
18 ms |
35676 KB |
Output is correct |
10 |
Correct |
14 ms |
35676 KB |
Output is correct |
11 |
Correct |
14 ms |
35588 KB |
Output is correct |
12 |
Correct |
14 ms |
35676 KB |
Output is correct |
13 |
Correct |
16 ms |
35612 KB |
Output is correct |
14 |
Correct |
15 ms |
35596 KB |
Output is correct |
15 |
Correct |
15 ms |
35804 KB |
Output is correct |
16 |
Correct |
14 ms |
35676 KB |
Output is correct |
17 |
Correct |
14 ms |
35764 KB |
Output is correct |
18 |
Correct |
15 ms |
35792 KB |
Output is correct |
19 |
Correct |
14 ms |
35676 KB |
Output is correct |
20 |
Correct |
14 ms |
35648 KB |
Output is correct |
21 |
Correct |
14 ms |
35676 KB |
Output is correct |
22 |
Correct |
14 ms |
35676 KB |
Output is correct |
23 |
Correct |
14 ms |
35656 KB |
Output is correct |
24 |
Correct |
15 ms |
35676 KB |
Output is correct |
25 |
Correct |
14 ms |
35672 KB |
Output is correct |
26 |
Correct |
18 ms |
36700 KB |
Output is correct |
27 |
Correct |
15 ms |
36588 KB |
Output is correct |
28 |
Correct |
16 ms |
36884 KB |
Output is correct |
29 |
Correct |
16 ms |
36664 KB |
Output is correct |
30 |
Correct |
16 ms |
36700 KB |
Output is correct |
31 |
Correct |
14 ms |
35752 KB |
Output is correct |
32 |
Correct |
16 ms |
36956 KB |
Output is correct |
33 |
Correct |
14 ms |
35768 KB |
Output is correct |
34 |
Correct |
16 ms |
36700 KB |
Output is correct |
35 |
Correct |
17 ms |
36592 KB |
Output is correct |
36 |
Correct |
20 ms |
36700 KB |
Output is correct |
37 |
Correct |
18 ms |
36700 KB |
Output is correct |
38 |
Correct |
17 ms |
35672 KB |
Output is correct |
39 |
Correct |
20 ms |
36688 KB |
Output is correct |
40 |
Correct |
20 ms |
36700 KB |
Output is correct |
41 |
Correct |
18 ms |
36700 KB |
Output is correct |
42 |
Correct |
17 ms |
36732 KB |
Output is correct |
43 |
Correct |
19 ms |
36952 KB |
Output is correct |
44 |
Correct |
17 ms |
35676 KB |
Output is correct |
45 |
Correct |
18 ms |
36700 KB |
Output is correct |
46 |
Correct |
20 ms |
36700 KB |
Output is correct |
47 |
Correct |
18 ms |
35676 KB |
Output is correct |
48 |
Correct |
17 ms |
36512 KB |
Output is correct |
49 |
Correct |
20 ms |
36656 KB |
Output is correct |
50 |
Correct |
19 ms |
36952 KB |
Output is correct |
51 |
Correct |
18 ms |
36696 KB |
Output is correct |
52 |
Correct |
18 ms |
36700 KB |
Output is correct |
53 |
Correct |
18 ms |
36560 KB |
Output is correct |
54 |
Correct |
19 ms |
36700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
35676 KB |
Output is correct |
2 |
Correct |
14 ms |
35572 KB |
Output is correct |
3 |
Correct |
14 ms |
35784 KB |
Output is correct |
4 |
Correct |
15 ms |
35676 KB |
Output is correct |
5 |
Correct |
15 ms |
35676 KB |
Output is correct |
6 |
Correct |
14 ms |
35572 KB |
Output is correct |
7 |
Correct |
14 ms |
35676 KB |
Output is correct |
8 |
Correct |
15 ms |
35600 KB |
Output is correct |
9 |
Correct |
18 ms |
35676 KB |
Output is correct |
10 |
Correct |
14 ms |
35676 KB |
Output is correct |
11 |
Correct |
14 ms |
35588 KB |
Output is correct |
12 |
Correct |
14 ms |
35676 KB |
Output is correct |
13 |
Correct |
16 ms |
35612 KB |
Output is correct |
14 |
Correct |
15 ms |
35596 KB |
Output is correct |
15 |
Correct |
15 ms |
35804 KB |
Output is correct |
16 |
Correct |
14 ms |
35676 KB |
Output is correct |
17 |
Correct |
14 ms |
35764 KB |
Output is correct |
18 |
Correct |
15 ms |
35792 KB |
Output is correct |
19 |
Correct |
14 ms |
35676 KB |
Output is correct |
20 |
Correct |
14 ms |
35648 KB |
Output is correct |
21 |
Correct |
14 ms |
35676 KB |
Output is correct |
22 |
Correct |
14 ms |
35676 KB |
Output is correct |
23 |
Correct |
14 ms |
35656 KB |
Output is correct |
24 |
Correct |
15 ms |
35676 KB |
Output is correct |
25 |
Correct |
16 ms |
35676 KB |
Output is correct |
26 |
Correct |
66 ms |
66676 KB |
Output is correct |
27 |
Correct |
81 ms |
67456 KB |
Output is correct |
28 |
Correct |
18 ms |
36696 KB |
Output is correct |
29 |
Correct |
17 ms |
35676 KB |
Output is correct |
30 |
Correct |
17 ms |
35676 KB |
Output is correct |
31 |
Correct |
79 ms |
67308 KB |
Output is correct |
32 |
Correct |
19 ms |
36700 KB |
Output is correct |
33 |
Correct |
97 ms |
75348 KB |
Output is correct |
34 |
Correct |
79 ms |
67368 KB |
Output is correct |
35 |
Correct |
17 ms |
36608 KB |
Output is correct |
36 |
Correct |
88 ms |
68800 KB |
Output is correct |
37 |
Correct |
18 ms |
36700 KB |
Output is correct |
38 |
Correct |
18 ms |
36696 KB |
Output is correct |
39 |
Correct |
61 ms |
66536 KB |
Output is correct |
40 |
Correct |
18 ms |
36956 KB |
Output is correct |
41 |
Correct |
75 ms |
67076 KB |
Output is correct |
42 |
Correct |
92 ms |
70500 KB |
Output is correct |
43 |
Correct |
18 ms |
35676 KB |
Output is correct |
44 |
Correct |
93 ms |
75460 KB |
Output is correct |
45 |
Correct |
80 ms |
71256 KB |
Output is correct |
46 |
Correct |
19 ms |
36700 KB |
Output is correct |
47 |
Correct |
19 ms |
36700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
66676 KB |
Output is correct |
2 |
Correct |
75 ms |
68932 KB |
Output is correct |
3 |
Correct |
18 ms |
36700 KB |
Output is correct |
4 |
Correct |
18 ms |
36644 KB |
Output is correct |
5 |
Correct |
17 ms |
35676 KB |
Output is correct |
6 |
Correct |
16 ms |
35676 KB |
Output is correct |
7 |
Correct |
17 ms |
36456 KB |
Output is correct |
8 |
Correct |
88 ms |
68436 KB |
Output is correct |
9 |
Correct |
16 ms |
36700 KB |
Output is correct |
10 |
Correct |
81 ms |
67664 KB |
Output is correct |
11 |
Correct |
15 ms |
35672 KB |
Output is correct |
12 |
Correct |
81 ms |
68180 KB |
Output is correct |
13 |
Correct |
93 ms |
68460 KB |
Output is correct |
14 |
Correct |
99 ms |
69668 KB |
Output is correct |
15 |
Correct |
66 ms |
66364 KB |
Output is correct |
16 |
Correct |
18 ms |
36696 KB |
Output is correct |
17 |
Correct |
18 ms |
35676 KB |
Output is correct |
18 |
Correct |
75 ms |
68676 KB |
Output is correct |
19 |
Correct |
115 ms |
74576 KB |
Output is correct |
20 |
Correct |
19 ms |
36700 KB |
Output is correct |
21 |
Correct |
18 ms |
35676 KB |
Output is correct |
22 |
Correct |
74 ms |
67400 KB |
Output is correct |
23 |
Correct |
20 ms |
36692 KB |
Output is correct |
24 |
Correct |
84 ms |
68524 KB |
Output is correct |
25 |
Correct |
113 ms |
72272 KB |
Output is correct |
26 |
Correct |
17 ms |
36700 KB |
Output is correct |
27 |
Correct |
20 ms |
36696 KB |
Output is correct |
28 |
Correct |
19 ms |
36700 KB |
Output is correct |
29 |
Correct |
19 ms |
36472 KB |
Output is correct |
30 |
Correct |
18 ms |
36700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
35676 KB |
Output is correct |
2 |
Correct |
14 ms |
35572 KB |
Output is correct |
3 |
Correct |
14 ms |
35784 KB |
Output is correct |
4 |
Correct |
15 ms |
35676 KB |
Output is correct |
5 |
Correct |
15 ms |
35676 KB |
Output is correct |
6 |
Correct |
14 ms |
35572 KB |
Output is correct |
7 |
Correct |
14 ms |
35676 KB |
Output is correct |
8 |
Correct |
15 ms |
35600 KB |
Output is correct |
9 |
Correct |
18 ms |
35676 KB |
Output is correct |
10 |
Correct |
14 ms |
35676 KB |
Output is correct |
11 |
Correct |
14 ms |
35588 KB |
Output is correct |
12 |
Correct |
14 ms |
35676 KB |
Output is correct |
13 |
Correct |
16 ms |
35612 KB |
Output is correct |
14 |
Correct |
15 ms |
35596 KB |
Output is correct |
15 |
Correct |
15 ms |
35804 KB |
Output is correct |
16 |
Correct |
14 ms |
35676 KB |
Output is correct |
17 |
Correct |
14 ms |
35764 KB |
Output is correct |
18 |
Correct |
15 ms |
35792 KB |
Output is correct |
19 |
Correct |
14 ms |
35676 KB |
Output is correct |
20 |
Correct |
14 ms |
35648 KB |
Output is correct |
21 |
Correct |
14 ms |
35676 KB |
Output is correct |
22 |
Correct |
14 ms |
35676 KB |
Output is correct |
23 |
Correct |
14 ms |
35656 KB |
Output is correct |
24 |
Correct |
15 ms |
35676 KB |
Output is correct |
25 |
Correct |
14 ms |
35672 KB |
Output is correct |
26 |
Correct |
18 ms |
36700 KB |
Output is correct |
27 |
Correct |
15 ms |
36588 KB |
Output is correct |
28 |
Correct |
16 ms |
36884 KB |
Output is correct |
29 |
Correct |
16 ms |
36664 KB |
Output is correct |
30 |
Correct |
16 ms |
36700 KB |
Output is correct |
31 |
Correct |
14 ms |
35752 KB |
Output is correct |
32 |
Correct |
16 ms |
36956 KB |
Output is correct |
33 |
Correct |
14 ms |
35768 KB |
Output is correct |
34 |
Correct |
16 ms |
36700 KB |
Output is correct |
35 |
Correct |
17 ms |
36592 KB |
Output is correct |
36 |
Correct |
20 ms |
36700 KB |
Output is correct |
37 |
Correct |
18 ms |
36700 KB |
Output is correct |
38 |
Correct |
17 ms |
35672 KB |
Output is correct |
39 |
Correct |
20 ms |
36688 KB |
Output is correct |
40 |
Correct |
20 ms |
36700 KB |
Output is correct |
41 |
Correct |
18 ms |
36700 KB |
Output is correct |
42 |
Correct |
17 ms |
36732 KB |
Output is correct |
43 |
Correct |
19 ms |
36952 KB |
Output is correct |
44 |
Correct |
17 ms |
35676 KB |
Output is correct |
45 |
Correct |
18 ms |
36700 KB |
Output is correct |
46 |
Correct |
20 ms |
36700 KB |
Output is correct |
47 |
Correct |
18 ms |
35676 KB |
Output is correct |
48 |
Correct |
17 ms |
36512 KB |
Output is correct |
49 |
Correct |
20 ms |
36656 KB |
Output is correct |
50 |
Correct |
19 ms |
36952 KB |
Output is correct |
51 |
Correct |
18 ms |
36696 KB |
Output is correct |
52 |
Correct |
18 ms |
36700 KB |
Output is correct |
53 |
Correct |
18 ms |
36560 KB |
Output is correct |
54 |
Correct |
19 ms |
36700 KB |
Output is correct |
55 |
Correct |
16 ms |
35676 KB |
Output is correct |
56 |
Correct |
66 ms |
66676 KB |
Output is correct |
57 |
Correct |
81 ms |
67456 KB |
Output is correct |
58 |
Correct |
18 ms |
36696 KB |
Output is correct |
59 |
Correct |
17 ms |
35676 KB |
Output is correct |
60 |
Correct |
17 ms |
35676 KB |
Output is correct |
61 |
Correct |
79 ms |
67308 KB |
Output is correct |
62 |
Correct |
19 ms |
36700 KB |
Output is correct |
63 |
Correct |
97 ms |
75348 KB |
Output is correct |
64 |
Correct |
79 ms |
67368 KB |
Output is correct |
65 |
Correct |
17 ms |
36608 KB |
Output is correct |
66 |
Correct |
88 ms |
68800 KB |
Output is correct |
67 |
Correct |
18 ms |
36700 KB |
Output is correct |
68 |
Correct |
18 ms |
36696 KB |
Output is correct |
69 |
Correct |
61 ms |
66536 KB |
Output is correct |
70 |
Correct |
18 ms |
36956 KB |
Output is correct |
71 |
Correct |
75 ms |
67076 KB |
Output is correct |
72 |
Correct |
92 ms |
70500 KB |
Output is correct |
73 |
Correct |
18 ms |
35676 KB |
Output is correct |
74 |
Correct |
93 ms |
75460 KB |
Output is correct |
75 |
Correct |
80 ms |
71256 KB |
Output is correct |
76 |
Correct |
19 ms |
36700 KB |
Output is correct |
77 |
Correct |
19 ms |
36700 KB |
Output is correct |
78 |
Correct |
68 ms |
66676 KB |
Output is correct |
79 |
Correct |
75 ms |
68932 KB |
Output is correct |
80 |
Correct |
18 ms |
36700 KB |
Output is correct |
81 |
Correct |
18 ms |
36644 KB |
Output is correct |
82 |
Correct |
17 ms |
35676 KB |
Output is correct |
83 |
Correct |
16 ms |
35676 KB |
Output is correct |
84 |
Correct |
17 ms |
36456 KB |
Output is correct |
85 |
Correct |
88 ms |
68436 KB |
Output is correct |
86 |
Correct |
16 ms |
36700 KB |
Output is correct |
87 |
Correct |
81 ms |
67664 KB |
Output is correct |
88 |
Correct |
15 ms |
35672 KB |
Output is correct |
89 |
Correct |
81 ms |
68180 KB |
Output is correct |
90 |
Correct |
93 ms |
68460 KB |
Output is correct |
91 |
Correct |
99 ms |
69668 KB |
Output is correct |
92 |
Correct |
66 ms |
66364 KB |
Output is correct |
93 |
Correct |
18 ms |
36696 KB |
Output is correct |
94 |
Correct |
18 ms |
35676 KB |
Output is correct |
95 |
Correct |
75 ms |
68676 KB |
Output is correct |
96 |
Correct |
115 ms |
74576 KB |
Output is correct |
97 |
Correct |
19 ms |
36700 KB |
Output is correct |
98 |
Correct |
18 ms |
35676 KB |
Output is correct |
99 |
Correct |
74 ms |
67400 KB |
Output is correct |
100 |
Correct |
20 ms |
36692 KB |
Output is correct |
101 |
Correct |
84 ms |
68524 KB |
Output is correct |
102 |
Correct |
113 ms |
72272 KB |
Output is correct |
103 |
Correct |
17 ms |
36700 KB |
Output is correct |
104 |
Correct |
20 ms |
36696 KB |
Output is correct |
105 |
Correct |
19 ms |
36700 KB |
Output is correct |
106 |
Correct |
19 ms |
36472 KB |
Output is correct |
107 |
Correct |
18 ms |
36700 KB |
Output is correct |
108 |
Correct |
332 ms |
195328 KB |
Output is correct |
109 |
Correct |
629 ms |
215000 KB |
Output is correct |
110 |
Correct |
607 ms |
226228 KB |
Output is correct |
111 |
Correct |
836 ms |
234324 KB |
Output is correct |
112 |
Correct |
728 ms |
220744 KB |
Output is correct |
113 |
Correct |
357 ms |
201512 KB |
Output is correct |
114 |
Correct |
437 ms |
193268 KB |
Output is correct |
115 |
Correct |
465 ms |
193200 KB |
Output is correct |
116 |
Correct |
484 ms |
198480 KB |
Output is correct |
117 |
Correct |
612 ms |
206676 KB |
Output is correct |
118 |
Correct |
435 ms |
193108 KB |
Output is correct |
119 |
Correct |
631 ms |
206672 KB |
Output is correct |
120 |
Correct |
753 ms |
220752 KB |
Output is correct |
121 |
Correct |
552 ms |
206676 KB |
Output is correct |
122 |
Correct |
478 ms |
201160 KB |
Output is correct |
123 |
Correct |
369 ms |
202252 KB |
Output is correct |
124 |
Correct |
688 ms |
206932 KB |
Output is correct |