#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define Mp make_pair
#define sep ' '
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define kill(res) cout << res << '\n', exit(0);
#define set_dec(x) cout << fixed << setprecision(x);
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll L = 22;
const ll N = 5e5 + 50;
const ll Mod = 1e9 + 7;
ll n, k, d[N], h[N], par[N][L], st[N], val[N], timer, sum[N], cnt, col[N], deg[N];
bool mark[N];
vector<int> adj[N], ost[N];
void dfs(int v, int p = 0){
par[v][0] = p;
for(int i = 1; i < L; i++){
if(!par[v][i-1]) continue;
par[v][i] = par[par[v][i-1]][i-1];
}
st[v] = ++timer;
val[timer] = v;
for(int u: adj[v]){
if(u != p){
h[u] = h[v] + 1;
dfs(u, v);
}
}
}
int getPar(int v, int k){
for(int i = 0; i < L; i++){
if(k & (1 << i)) v = par[v][i];
}
return v;
}
int lca(int v, int u){
if(h[v] < h[u]) swap(u, v);
v = getPar(v, h[v] - h[u]);
if(v == u) return v;
for(int i = L-1; i >= 0; i--){
if(par[v][i] != par[u][i]){
v = par[v][i]; u = par[u][i];
}
}
return par[v][0];
}
void pre(int v, int p = 0){
for(int u: adj[v]){
if(u == p) continue;
pre(u, v); sum[v] += sum[u];
}
}
void ff(int v){
col[v] = cnt;
mark[v] = true;
for(int u: adj[v]){
if(h[u] > h[v] && sum[u] == 0) continue;
if(h[u] < h[v] && sum[v] == 0) continue;
if(mark[u]) continue;
ff(u);
}
}
int main(){
fast_io;
cin >> n >> k;
int u, v;
for(int i = 1; i < n; i++){
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
for(int i = 1; i <= n; i++) cin >> d[i];
dfs(1);
for(int i = 1; i <= n; i++) ost[d[i]].pb(i);
for(int i = 1; i <= k; i++){
if(ost[i].size() <= 1) continue;
ll mn = n+1, mx = 0;
for(int j: ost[i]){
mn = min(mn, st[j]); mx = max(mx, st[j]);
}
int top = lca(val[mn], val[mx]);
for(int j: ost[i]){
sum[j]++; sum[top]--;
}
}
pre(1);
for(int i = 1; i <= n; i++){
if(!mark[i]){
cnt++; ff(i);
}
}
for(int i = 1; i <= n; i++){
for(int j: adj[i]){
if(h[i] < h[j] && sum[j]) continue;
if(h[i] > h[j] && sum[i]) continue;
deg[col[i]]++; deg[col[j]]++;
}
}
ll leaf = 0;
for(int i = 1; i <= cnt; i++) if(deg[i] == 2) leaf++;
cout << (leaf+1)/2;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
35416 KB |
Output is correct |
2 |
Correct |
7 ms |
35420 KB |
Output is correct |
3 |
Correct |
7 ms |
35420 KB |
Output is correct |
4 |
Correct |
7 ms |
35420 KB |
Output is correct |
5 |
Correct |
7 ms |
35508 KB |
Output is correct |
6 |
Correct |
7 ms |
35420 KB |
Output is correct |
7 |
Correct |
7 ms |
33476 KB |
Output is correct |
8 |
Correct |
7 ms |
35420 KB |
Output is correct |
9 |
Correct |
8 ms |
35416 KB |
Output is correct |
10 |
Correct |
7 ms |
35420 KB |
Output is correct |
11 |
Correct |
8 ms |
35420 KB |
Output is correct |
12 |
Correct |
7 ms |
35420 KB |
Output is correct |
13 |
Correct |
8 ms |
35464 KB |
Output is correct |
14 |
Correct |
7 ms |
35420 KB |
Output is correct |
15 |
Correct |
8 ms |
35420 KB |
Output is correct |
16 |
Correct |
7 ms |
33372 KB |
Output is correct |
17 |
Correct |
7 ms |
35416 KB |
Output is correct |
18 |
Correct |
8 ms |
35420 KB |
Output is correct |
19 |
Correct |
6 ms |
33488 KB |
Output is correct |
20 |
Correct |
7 ms |
33372 KB |
Output is correct |
21 |
Correct |
7 ms |
35420 KB |
Output is correct |
22 |
Correct |
7 ms |
35420 KB |
Output is correct |
23 |
Correct |
7 ms |
35536 KB |
Output is correct |
24 |
Correct |
7 ms |
35420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
35416 KB |
Output is correct |
2 |
Correct |
7 ms |
35420 KB |
Output is correct |
3 |
Correct |
7 ms |
35420 KB |
Output is correct |
4 |
Correct |
7 ms |
35420 KB |
Output is correct |
5 |
Correct |
7 ms |
35508 KB |
Output is correct |
6 |
Correct |
7 ms |
35420 KB |
Output is correct |
7 |
Correct |
7 ms |
33476 KB |
Output is correct |
8 |
Correct |
7 ms |
35420 KB |
Output is correct |
9 |
Correct |
8 ms |
35416 KB |
Output is correct |
10 |
Correct |
7 ms |
35420 KB |
Output is correct |
11 |
Correct |
8 ms |
35420 KB |
Output is correct |
12 |
Correct |
7 ms |
35420 KB |
Output is correct |
13 |
Correct |
8 ms |
35464 KB |
Output is correct |
14 |
Correct |
7 ms |
35420 KB |
Output is correct |
15 |
Correct |
8 ms |
35420 KB |
Output is correct |
16 |
Correct |
7 ms |
33372 KB |
Output is correct |
17 |
Correct |
7 ms |
35416 KB |
Output is correct |
18 |
Correct |
8 ms |
35420 KB |
Output is correct |
19 |
Correct |
6 ms |
33488 KB |
Output is correct |
20 |
Correct |
7 ms |
33372 KB |
Output is correct |
21 |
Correct |
7 ms |
35420 KB |
Output is correct |
22 |
Correct |
7 ms |
35420 KB |
Output is correct |
23 |
Correct |
7 ms |
35536 KB |
Output is correct |
24 |
Correct |
7 ms |
35420 KB |
Output is correct |
25 |
Correct |
7 ms |
35416 KB |
Output is correct |
26 |
Correct |
8 ms |
35676 KB |
Output is correct |
27 |
Correct |
8 ms |
35536 KB |
Output is correct |
28 |
Correct |
8 ms |
35676 KB |
Output is correct |
29 |
Correct |
8 ms |
35672 KB |
Output is correct |
30 |
Correct |
8 ms |
35676 KB |
Output is correct |
31 |
Correct |
7 ms |
35420 KB |
Output is correct |
32 |
Correct |
8 ms |
35932 KB |
Output is correct |
33 |
Correct |
7 ms |
35540 KB |
Output is correct |
34 |
Correct |
8 ms |
35676 KB |
Output is correct |
35 |
Correct |
9 ms |
33880 KB |
Output is correct |
36 |
Correct |
8 ms |
35676 KB |
Output is correct |
37 |
Correct |
8 ms |
35772 KB |
Output is correct |
38 |
Correct |
7 ms |
35420 KB |
Output is correct |
39 |
Correct |
8 ms |
35668 KB |
Output is correct |
40 |
Correct |
8 ms |
35676 KB |
Output is correct |
41 |
Correct |
9 ms |
35524 KB |
Output is correct |
42 |
Correct |
8 ms |
35672 KB |
Output is correct |
43 |
Correct |
8 ms |
35932 KB |
Output is correct |
44 |
Correct |
7 ms |
35472 KB |
Output is correct |
45 |
Correct |
9 ms |
35776 KB |
Output is correct |
46 |
Correct |
9 ms |
35676 KB |
Output is correct |
47 |
Correct |
7 ms |
35416 KB |
Output is correct |
48 |
Correct |
9 ms |
35676 KB |
Output is correct |
49 |
Correct |
8 ms |
33628 KB |
Output is correct |
50 |
Correct |
8 ms |
33628 KB |
Output is correct |
51 |
Correct |
8 ms |
35616 KB |
Output is correct |
52 |
Correct |
8 ms |
35676 KB |
Output is correct |
53 |
Correct |
8 ms |
35776 KB |
Output is correct |
54 |
Correct |
8 ms |
35676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
35416 KB |
Output is correct |
2 |
Correct |
7 ms |
35420 KB |
Output is correct |
3 |
Correct |
7 ms |
35420 KB |
Output is correct |
4 |
Correct |
7 ms |
35420 KB |
Output is correct |
5 |
Correct |
7 ms |
35508 KB |
Output is correct |
6 |
Correct |
7 ms |
35420 KB |
Output is correct |
7 |
Correct |
7 ms |
33476 KB |
Output is correct |
8 |
Correct |
7 ms |
35420 KB |
Output is correct |
9 |
Correct |
8 ms |
35416 KB |
Output is correct |
10 |
Correct |
7 ms |
35420 KB |
Output is correct |
11 |
Correct |
8 ms |
35420 KB |
Output is correct |
12 |
Correct |
7 ms |
35420 KB |
Output is correct |
13 |
Correct |
8 ms |
35464 KB |
Output is correct |
14 |
Correct |
7 ms |
35420 KB |
Output is correct |
15 |
Correct |
8 ms |
35420 KB |
Output is correct |
16 |
Correct |
7 ms |
33372 KB |
Output is correct |
17 |
Correct |
7 ms |
35416 KB |
Output is correct |
18 |
Correct |
8 ms |
35420 KB |
Output is correct |
19 |
Correct |
6 ms |
33488 KB |
Output is correct |
20 |
Correct |
7 ms |
33372 KB |
Output is correct |
21 |
Correct |
7 ms |
35420 KB |
Output is correct |
22 |
Correct |
7 ms |
35420 KB |
Output is correct |
23 |
Correct |
7 ms |
35536 KB |
Output is correct |
24 |
Correct |
7 ms |
35420 KB |
Output is correct |
25 |
Correct |
7 ms |
35672 KB |
Output is correct |
26 |
Correct |
49 ms |
58916 KB |
Output is correct |
27 |
Correct |
66 ms |
58576 KB |
Output is correct |
28 |
Correct |
9 ms |
36024 KB |
Output is correct |
29 |
Correct |
7 ms |
35416 KB |
Output is correct |
30 |
Correct |
7 ms |
35420 KB |
Output is correct |
31 |
Correct |
72 ms |
58548 KB |
Output is correct |
32 |
Correct |
8 ms |
35676 KB |
Output is correct |
33 |
Correct |
101 ms |
65724 KB |
Output is correct |
34 |
Correct |
68 ms |
58536 KB |
Output is correct |
35 |
Correct |
9 ms |
35932 KB |
Output is correct |
36 |
Correct |
87 ms |
59392 KB |
Output is correct |
37 |
Correct |
8 ms |
35928 KB |
Output is correct |
38 |
Correct |
8 ms |
35676 KB |
Output is correct |
39 |
Correct |
45 ms |
58780 KB |
Output is correct |
40 |
Correct |
8 ms |
35928 KB |
Output is correct |
41 |
Correct |
67 ms |
58384 KB |
Output is correct |
42 |
Correct |
95 ms |
62196 KB |
Output is correct |
43 |
Correct |
7 ms |
35416 KB |
Output is correct |
44 |
Correct |
90 ms |
65960 KB |
Output is correct |
45 |
Correct |
86 ms |
62288 KB |
Output is correct |
46 |
Correct |
8 ms |
35676 KB |
Output is correct |
47 |
Correct |
8 ms |
35676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
58896 KB |
Output is correct |
2 |
Correct |
59 ms |
59664 KB |
Output is correct |
3 |
Correct |
8 ms |
35676 KB |
Output is correct |
4 |
Correct |
8 ms |
35676 KB |
Output is correct |
5 |
Correct |
8 ms |
35420 KB |
Output is correct |
6 |
Correct |
7 ms |
35420 KB |
Output is correct |
7 |
Correct |
8 ms |
35676 KB |
Output is correct |
8 |
Correct |
95 ms |
59908 KB |
Output is correct |
9 |
Correct |
8 ms |
35676 KB |
Output is correct |
10 |
Correct |
69 ms |
58792 KB |
Output is correct |
11 |
Correct |
7 ms |
35420 KB |
Output is correct |
12 |
Correct |
82 ms |
58708 KB |
Output is correct |
13 |
Correct |
107 ms |
59728 KB |
Output is correct |
14 |
Correct |
75 ms |
60296 KB |
Output is correct |
15 |
Correct |
49 ms |
58716 KB |
Output is correct |
16 |
Correct |
8 ms |
35676 KB |
Output is correct |
17 |
Correct |
7 ms |
35508 KB |
Output is correct |
18 |
Correct |
68 ms |
61496 KB |
Output is correct |
19 |
Correct |
91 ms |
64192 KB |
Output is correct |
20 |
Correct |
8 ms |
35676 KB |
Output is correct |
21 |
Correct |
7 ms |
35532 KB |
Output is correct |
22 |
Correct |
61 ms |
60104 KB |
Output is correct |
23 |
Correct |
8 ms |
35672 KB |
Output is correct |
24 |
Correct |
101 ms |
59192 KB |
Output is correct |
25 |
Correct |
86 ms |
62800 KB |
Output is correct |
26 |
Correct |
8 ms |
33628 KB |
Output is correct |
27 |
Correct |
8 ms |
33828 KB |
Output is correct |
28 |
Correct |
8 ms |
35600 KB |
Output is correct |
29 |
Correct |
8 ms |
35676 KB |
Output is correct |
30 |
Correct |
8 ms |
35784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
35416 KB |
Output is correct |
2 |
Correct |
7 ms |
35420 KB |
Output is correct |
3 |
Correct |
7 ms |
35420 KB |
Output is correct |
4 |
Correct |
7 ms |
35420 KB |
Output is correct |
5 |
Correct |
7 ms |
35508 KB |
Output is correct |
6 |
Correct |
7 ms |
35420 KB |
Output is correct |
7 |
Correct |
7 ms |
33476 KB |
Output is correct |
8 |
Correct |
7 ms |
35420 KB |
Output is correct |
9 |
Correct |
8 ms |
35416 KB |
Output is correct |
10 |
Correct |
7 ms |
35420 KB |
Output is correct |
11 |
Correct |
8 ms |
35420 KB |
Output is correct |
12 |
Correct |
7 ms |
35420 KB |
Output is correct |
13 |
Correct |
8 ms |
35464 KB |
Output is correct |
14 |
Correct |
7 ms |
35420 KB |
Output is correct |
15 |
Correct |
8 ms |
35420 KB |
Output is correct |
16 |
Correct |
7 ms |
33372 KB |
Output is correct |
17 |
Correct |
7 ms |
35416 KB |
Output is correct |
18 |
Correct |
8 ms |
35420 KB |
Output is correct |
19 |
Correct |
6 ms |
33488 KB |
Output is correct |
20 |
Correct |
7 ms |
33372 KB |
Output is correct |
21 |
Correct |
7 ms |
35420 KB |
Output is correct |
22 |
Correct |
7 ms |
35420 KB |
Output is correct |
23 |
Correct |
7 ms |
35536 KB |
Output is correct |
24 |
Correct |
7 ms |
35420 KB |
Output is correct |
25 |
Correct |
7 ms |
35416 KB |
Output is correct |
26 |
Correct |
8 ms |
35676 KB |
Output is correct |
27 |
Correct |
8 ms |
35536 KB |
Output is correct |
28 |
Correct |
8 ms |
35676 KB |
Output is correct |
29 |
Correct |
8 ms |
35672 KB |
Output is correct |
30 |
Correct |
8 ms |
35676 KB |
Output is correct |
31 |
Correct |
7 ms |
35420 KB |
Output is correct |
32 |
Correct |
8 ms |
35932 KB |
Output is correct |
33 |
Correct |
7 ms |
35540 KB |
Output is correct |
34 |
Correct |
8 ms |
35676 KB |
Output is correct |
35 |
Correct |
9 ms |
33880 KB |
Output is correct |
36 |
Correct |
8 ms |
35676 KB |
Output is correct |
37 |
Correct |
8 ms |
35772 KB |
Output is correct |
38 |
Correct |
7 ms |
35420 KB |
Output is correct |
39 |
Correct |
8 ms |
35668 KB |
Output is correct |
40 |
Correct |
8 ms |
35676 KB |
Output is correct |
41 |
Correct |
9 ms |
35524 KB |
Output is correct |
42 |
Correct |
8 ms |
35672 KB |
Output is correct |
43 |
Correct |
8 ms |
35932 KB |
Output is correct |
44 |
Correct |
7 ms |
35472 KB |
Output is correct |
45 |
Correct |
9 ms |
35776 KB |
Output is correct |
46 |
Correct |
9 ms |
35676 KB |
Output is correct |
47 |
Correct |
7 ms |
35416 KB |
Output is correct |
48 |
Correct |
9 ms |
35676 KB |
Output is correct |
49 |
Correct |
8 ms |
33628 KB |
Output is correct |
50 |
Correct |
8 ms |
33628 KB |
Output is correct |
51 |
Correct |
8 ms |
35616 KB |
Output is correct |
52 |
Correct |
8 ms |
35676 KB |
Output is correct |
53 |
Correct |
8 ms |
35776 KB |
Output is correct |
54 |
Correct |
8 ms |
35676 KB |
Output is correct |
55 |
Correct |
7 ms |
35672 KB |
Output is correct |
56 |
Correct |
49 ms |
58916 KB |
Output is correct |
57 |
Correct |
66 ms |
58576 KB |
Output is correct |
58 |
Correct |
9 ms |
36024 KB |
Output is correct |
59 |
Correct |
7 ms |
35416 KB |
Output is correct |
60 |
Correct |
7 ms |
35420 KB |
Output is correct |
61 |
Correct |
72 ms |
58548 KB |
Output is correct |
62 |
Correct |
8 ms |
35676 KB |
Output is correct |
63 |
Correct |
101 ms |
65724 KB |
Output is correct |
64 |
Correct |
68 ms |
58536 KB |
Output is correct |
65 |
Correct |
9 ms |
35932 KB |
Output is correct |
66 |
Correct |
87 ms |
59392 KB |
Output is correct |
67 |
Correct |
8 ms |
35928 KB |
Output is correct |
68 |
Correct |
8 ms |
35676 KB |
Output is correct |
69 |
Correct |
45 ms |
58780 KB |
Output is correct |
70 |
Correct |
8 ms |
35928 KB |
Output is correct |
71 |
Correct |
67 ms |
58384 KB |
Output is correct |
72 |
Correct |
95 ms |
62196 KB |
Output is correct |
73 |
Correct |
7 ms |
35416 KB |
Output is correct |
74 |
Correct |
90 ms |
65960 KB |
Output is correct |
75 |
Correct |
86 ms |
62288 KB |
Output is correct |
76 |
Correct |
8 ms |
35676 KB |
Output is correct |
77 |
Correct |
8 ms |
35676 KB |
Output is correct |
78 |
Correct |
48 ms |
58896 KB |
Output is correct |
79 |
Correct |
59 ms |
59664 KB |
Output is correct |
80 |
Correct |
8 ms |
35676 KB |
Output is correct |
81 |
Correct |
8 ms |
35676 KB |
Output is correct |
82 |
Correct |
8 ms |
35420 KB |
Output is correct |
83 |
Correct |
7 ms |
35420 KB |
Output is correct |
84 |
Correct |
8 ms |
35676 KB |
Output is correct |
85 |
Correct |
95 ms |
59908 KB |
Output is correct |
86 |
Correct |
8 ms |
35676 KB |
Output is correct |
87 |
Correct |
69 ms |
58792 KB |
Output is correct |
88 |
Correct |
7 ms |
35420 KB |
Output is correct |
89 |
Correct |
82 ms |
58708 KB |
Output is correct |
90 |
Correct |
107 ms |
59728 KB |
Output is correct |
91 |
Correct |
75 ms |
60296 KB |
Output is correct |
92 |
Correct |
49 ms |
58716 KB |
Output is correct |
93 |
Correct |
8 ms |
35676 KB |
Output is correct |
94 |
Correct |
7 ms |
35508 KB |
Output is correct |
95 |
Correct |
68 ms |
61496 KB |
Output is correct |
96 |
Correct |
91 ms |
64192 KB |
Output is correct |
97 |
Correct |
8 ms |
35676 KB |
Output is correct |
98 |
Correct |
7 ms |
35532 KB |
Output is correct |
99 |
Correct |
61 ms |
60104 KB |
Output is correct |
100 |
Correct |
8 ms |
35672 KB |
Output is correct |
101 |
Correct |
101 ms |
59192 KB |
Output is correct |
102 |
Correct |
86 ms |
62800 KB |
Output is correct |
103 |
Correct |
8 ms |
33628 KB |
Output is correct |
104 |
Correct |
8 ms |
33828 KB |
Output is correct |
105 |
Correct |
8 ms |
35600 KB |
Output is correct |
106 |
Correct |
8 ms |
35676 KB |
Output is correct |
107 |
Correct |
8 ms |
35784 KB |
Output is correct |
108 |
Correct |
440 ms |
170088 KB |
Output is correct |
109 |
Correct |
831 ms |
179788 KB |
Output is correct |
110 |
Correct |
823 ms |
190904 KB |
Output is correct |
111 |
Correct |
822 ms |
202836 KB |
Output is correct |
112 |
Correct |
805 ms |
192992 KB |
Output is correct |
113 |
Correct |
384 ms |
179504 KB |
Output is correct |
114 |
Correct |
551 ms |
161792 KB |
Output is correct |
115 |
Correct |
514 ms |
161792 KB |
Output is correct |
116 |
Correct |
650 ms |
166376 KB |
Output is correct |
117 |
Correct |
589 ms |
179608 KB |
Output is correct |
118 |
Correct |
524 ms |
162724 KB |
Output is correct |
119 |
Correct |
575 ms |
179740 KB |
Output is correct |
120 |
Correct |
764 ms |
193604 KB |
Output is correct |
121 |
Correct |
565 ms |
179740 KB |
Output is correct |
122 |
Correct |
672 ms |
171604 KB |
Output is correct |
123 |
Correct |
367 ms |
177360 KB |
Output is correct |
124 |
Correct |
702 ms |
178256 KB |
Output is correct |