#include "bits/stdc++.h"
using namespace std;
#define ar array
typedef int64_t ll;
#define her cout<<"here"<<endl;
const int N = 5e5 + 5;
struct BIT{
int tree[N];
void add(int i, int v){
for(;i<N;i+=(i&-i)) tree[i] += v;
}
int get(int i){
int r = 0;
for(;i>0;i-=(i&-i)) r += tree[i];
return r;
}
}bit;
vector<int> edges[N], G[N];
int a[N], tin[N], tout[N], t;
int par[N][20], pref[N];
void dfs(int u, int p = -1){
tin[u] = ++t;
for(int j=1;j<20;j++){
par[u][j] = par[par[u][j-1]][j-1];
}
for(auto x : edges[u]){
if(x == p) continue;
par[x][0] = u;
dfs(x, u);
}
tout[u] = t;
}
int p[N], sz[N];
int f(int x) { return (p[x] == x ? x : p[x] = f(p[x])); }
void merge(int a, int b){
a = f(a), b = f(b);
if(a == b) return;
if(sz[a] < sz[b]) swap(a, b);
p[b] = a, sz[a] += sz[b];
}
void dfs2(int u, int p = -1){
for(auto x : edges[u]){
if(x == p) continue;
dfs2(x, u);
pref[u] += pref[x];
}
if(pref[u] && ~p){
merge(u, p);
}
}
bool is(int a, int b){ return (tin[a] <= tin[b] && tin[b] <= tout[a]); }
int lca(int a, int b){
if(is(a, b)) return a;
if(is(b, a)) return b;
for(int i=19;~i;i--){
if(!is(par[a][i], b)) a = par[a][i];
} return par[a][0];
}
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n, k; cin >> n >> k;
for(int i=1;i<n;i++){
int a, b; cin >> a >> b;
edges[a].push_back(b);
edges[b].push_back(a);
}
for(int i=1;i<=n;i++){
cin >> a[i];
G[a[i]].push_back(i);
}
par[1][0] = 1;
dfs(1);
for(int i=1;i<=k;i++){
vector<int>& t = G[i];
sort(t.begin(), t.end(), [&](int i, int j){
return tin[i] < tin[j];
});
int r = lca(t[0], t.back());
for(auto u : t){
pref[r]--;
pref[u]++;
}
G[i].clear();
}
for(int i=1;i<=n;i++) p[i] = i, sz[i] = 1;
dfs2(1);
for(int i=1;i<=n;i++){
for(auto x : edges[i]){
if(f(i) != f(x)){
G[f(i)].push_back(f(x));
}
}
}
int tot = 0;
for(int i=1;i<=n;i++){
tot += ((int)G[i].size() == 1);
}
cout<<(tot + 1) / 2<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23792 KB |
Output is correct |
3 |
Correct |
12 ms |
23792 KB |
Output is correct |
4 |
Correct |
12 ms |
23784 KB |
Output is correct |
5 |
Correct |
14 ms |
23764 KB |
Output is correct |
6 |
Correct |
14 ms |
23868 KB |
Output is correct |
7 |
Correct |
12 ms |
23828 KB |
Output is correct |
8 |
Correct |
12 ms |
23856 KB |
Output is correct |
9 |
Correct |
15 ms |
23808 KB |
Output is correct |
10 |
Correct |
13 ms |
23872 KB |
Output is correct |
11 |
Correct |
13 ms |
23796 KB |
Output is correct |
12 |
Correct |
12 ms |
23888 KB |
Output is correct |
13 |
Correct |
13 ms |
23860 KB |
Output is correct |
14 |
Correct |
13 ms |
23816 KB |
Output is correct |
15 |
Correct |
14 ms |
23764 KB |
Output is correct |
16 |
Correct |
11 ms |
23768 KB |
Output is correct |
17 |
Correct |
11 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23816 KB |
Output is correct |
19 |
Correct |
12 ms |
23808 KB |
Output is correct |
20 |
Correct |
12 ms |
23792 KB |
Output is correct |
21 |
Correct |
12 ms |
23820 KB |
Output is correct |
22 |
Correct |
13 ms |
23768 KB |
Output is correct |
23 |
Correct |
12 ms |
23848 KB |
Output is correct |
24 |
Correct |
12 ms |
23792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23792 KB |
Output is correct |
3 |
Correct |
12 ms |
23792 KB |
Output is correct |
4 |
Correct |
12 ms |
23784 KB |
Output is correct |
5 |
Correct |
14 ms |
23764 KB |
Output is correct |
6 |
Correct |
14 ms |
23868 KB |
Output is correct |
7 |
Correct |
12 ms |
23828 KB |
Output is correct |
8 |
Correct |
12 ms |
23856 KB |
Output is correct |
9 |
Correct |
15 ms |
23808 KB |
Output is correct |
10 |
Correct |
13 ms |
23872 KB |
Output is correct |
11 |
Correct |
13 ms |
23796 KB |
Output is correct |
12 |
Correct |
12 ms |
23888 KB |
Output is correct |
13 |
Correct |
13 ms |
23860 KB |
Output is correct |
14 |
Correct |
13 ms |
23816 KB |
Output is correct |
15 |
Correct |
14 ms |
23764 KB |
Output is correct |
16 |
Correct |
11 ms |
23768 KB |
Output is correct |
17 |
Correct |
11 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23816 KB |
Output is correct |
19 |
Correct |
12 ms |
23808 KB |
Output is correct |
20 |
Correct |
12 ms |
23792 KB |
Output is correct |
21 |
Correct |
12 ms |
23820 KB |
Output is correct |
22 |
Correct |
13 ms |
23768 KB |
Output is correct |
23 |
Correct |
12 ms |
23848 KB |
Output is correct |
24 |
Correct |
12 ms |
23792 KB |
Output is correct |
25 |
Correct |
11 ms |
23764 KB |
Output is correct |
26 |
Correct |
15 ms |
24344 KB |
Output is correct |
27 |
Correct |
13 ms |
24308 KB |
Output is correct |
28 |
Correct |
13 ms |
24416 KB |
Output is correct |
29 |
Correct |
14 ms |
24320 KB |
Output is correct |
30 |
Correct |
13 ms |
24280 KB |
Output is correct |
31 |
Correct |
14 ms |
23880 KB |
Output is correct |
32 |
Correct |
13 ms |
24532 KB |
Output is correct |
33 |
Correct |
11 ms |
23764 KB |
Output is correct |
34 |
Correct |
13 ms |
24280 KB |
Output is correct |
35 |
Correct |
14 ms |
24404 KB |
Output is correct |
36 |
Correct |
16 ms |
24216 KB |
Output is correct |
37 |
Correct |
13 ms |
24276 KB |
Output is correct |
38 |
Correct |
12 ms |
23800 KB |
Output is correct |
39 |
Correct |
14 ms |
24276 KB |
Output is correct |
40 |
Correct |
14 ms |
24312 KB |
Output is correct |
41 |
Correct |
15 ms |
24432 KB |
Output is correct |
42 |
Correct |
15 ms |
24352 KB |
Output is correct |
43 |
Correct |
17 ms |
24532 KB |
Output is correct |
44 |
Correct |
13 ms |
23892 KB |
Output is correct |
45 |
Correct |
17 ms |
24404 KB |
Output is correct |
46 |
Correct |
14 ms |
24276 KB |
Output is correct |
47 |
Correct |
12 ms |
23796 KB |
Output is correct |
48 |
Correct |
13 ms |
24340 KB |
Output is correct |
49 |
Correct |
13 ms |
24344 KB |
Output is correct |
50 |
Correct |
13 ms |
24404 KB |
Output is correct |
51 |
Correct |
13 ms |
24268 KB |
Output is correct |
52 |
Correct |
13 ms |
24344 KB |
Output is correct |
53 |
Correct |
13 ms |
24352 KB |
Output is correct |
54 |
Correct |
13 ms |
24276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23792 KB |
Output is correct |
3 |
Correct |
12 ms |
23792 KB |
Output is correct |
4 |
Correct |
12 ms |
23784 KB |
Output is correct |
5 |
Correct |
14 ms |
23764 KB |
Output is correct |
6 |
Correct |
14 ms |
23868 KB |
Output is correct |
7 |
Correct |
12 ms |
23828 KB |
Output is correct |
8 |
Correct |
12 ms |
23856 KB |
Output is correct |
9 |
Correct |
15 ms |
23808 KB |
Output is correct |
10 |
Correct |
13 ms |
23872 KB |
Output is correct |
11 |
Correct |
13 ms |
23796 KB |
Output is correct |
12 |
Correct |
12 ms |
23888 KB |
Output is correct |
13 |
Correct |
13 ms |
23860 KB |
Output is correct |
14 |
Correct |
13 ms |
23816 KB |
Output is correct |
15 |
Correct |
14 ms |
23764 KB |
Output is correct |
16 |
Correct |
11 ms |
23768 KB |
Output is correct |
17 |
Correct |
11 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23816 KB |
Output is correct |
19 |
Correct |
12 ms |
23808 KB |
Output is correct |
20 |
Correct |
12 ms |
23792 KB |
Output is correct |
21 |
Correct |
12 ms |
23820 KB |
Output is correct |
22 |
Correct |
13 ms |
23768 KB |
Output is correct |
23 |
Correct |
12 ms |
23848 KB |
Output is correct |
24 |
Correct |
12 ms |
23792 KB |
Output is correct |
25 |
Correct |
12 ms |
23824 KB |
Output is correct |
26 |
Correct |
76 ms |
39512 KB |
Output is correct |
27 |
Correct |
101 ms |
39188 KB |
Output is correct |
28 |
Correct |
13 ms |
24276 KB |
Output is correct |
29 |
Correct |
12 ms |
23824 KB |
Output is correct |
30 |
Correct |
13 ms |
23804 KB |
Output is correct |
31 |
Correct |
97 ms |
39176 KB |
Output is correct |
32 |
Correct |
13 ms |
24276 KB |
Output is correct |
33 |
Correct |
145 ms |
46344 KB |
Output is correct |
34 |
Correct |
106 ms |
39212 KB |
Output is correct |
35 |
Correct |
14 ms |
24276 KB |
Output is correct |
36 |
Correct |
105 ms |
39856 KB |
Output is correct |
37 |
Correct |
14 ms |
24360 KB |
Output is correct |
38 |
Correct |
13 ms |
24276 KB |
Output is correct |
39 |
Correct |
78 ms |
39440 KB |
Output is correct |
40 |
Correct |
16 ms |
24532 KB |
Output is correct |
41 |
Correct |
110 ms |
39116 KB |
Output is correct |
42 |
Correct |
110 ms |
41460 KB |
Output is correct |
43 |
Correct |
13 ms |
23892 KB |
Output is correct |
44 |
Correct |
124 ms |
46632 KB |
Output is correct |
45 |
Correct |
119 ms |
42924 KB |
Output is correct |
46 |
Correct |
14 ms |
24276 KB |
Output is correct |
47 |
Correct |
14 ms |
24224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
38064 KB |
Output is correct |
2 |
Correct |
85 ms |
41424 KB |
Output is correct |
3 |
Correct |
13 ms |
24312 KB |
Output is correct |
4 |
Correct |
14 ms |
24288 KB |
Output is correct |
5 |
Correct |
12 ms |
23844 KB |
Output is correct |
6 |
Correct |
12 ms |
23812 KB |
Output is correct |
7 |
Correct |
16 ms |
24332 KB |
Output is correct |
8 |
Correct |
115 ms |
40692 KB |
Output is correct |
9 |
Correct |
16 ms |
24276 KB |
Output is correct |
10 |
Correct |
96 ms |
39352 KB |
Output is correct |
11 |
Correct |
12 ms |
23892 KB |
Output is correct |
12 |
Correct |
105 ms |
39324 KB |
Output is correct |
13 |
Correct |
119 ms |
40828 KB |
Output is correct |
14 |
Correct |
108 ms |
42344 KB |
Output is correct |
15 |
Correct |
72 ms |
39460 KB |
Output is correct |
16 |
Correct |
13 ms |
24312 KB |
Output is correct |
17 |
Correct |
11 ms |
23828 KB |
Output is correct |
18 |
Correct |
100 ms |
42880 KB |
Output is correct |
19 |
Correct |
172 ms |
45944 KB |
Output is correct |
20 |
Correct |
14 ms |
24340 KB |
Output is correct |
21 |
Correct |
12 ms |
23892 KB |
Output is correct |
22 |
Correct |
85 ms |
41116 KB |
Output is correct |
23 |
Correct |
13 ms |
24276 KB |
Output is correct |
24 |
Correct |
103 ms |
39960 KB |
Output is correct |
25 |
Correct |
137 ms |
44768 KB |
Output is correct |
26 |
Correct |
14 ms |
24404 KB |
Output is correct |
27 |
Correct |
13 ms |
24448 KB |
Output is correct |
28 |
Correct |
16 ms |
24276 KB |
Output is correct |
29 |
Correct |
13 ms |
24276 KB |
Output is correct |
30 |
Correct |
13 ms |
24348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23792 KB |
Output is correct |
3 |
Correct |
12 ms |
23792 KB |
Output is correct |
4 |
Correct |
12 ms |
23784 KB |
Output is correct |
5 |
Correct |
14 ms |
23764 KB |
Output is correct |
6 |
Correct |
14 ms |
23868 KB |
Output is correct |
7 |
Correct |
12 ms |
23828 KB |
Output is correct |
8 |
Correct |
12 ms |
23856 KB |
Output is correct |
9 |
Correct |
15 ms |
23808 KB |
Output is correct |
10 |
Correct |
13 ms |
23872 KB |
Output is correct |
11 |
Correct |
13 ms |
23796 KB |
Output is correct |
12 |
Correct |
12 ms |
23888 KB |
Output is correct |
13 |
Correct |
13 ms |
23860 KB |
Output is correct |
14 |
Correct |
13 ms |
23816 KB |
Output is correct |
15 |
Correct |
14 ms |
23764 KB |
Output is correct |
16 |
Correct |
11 ms |
23768 KB |
Output is correct |
17 |
Correct |
11 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23816 KB |
Output is correct |
19 |
Correct |
12 ms |
23808 KB |
Output is correct |
20 |
Correct |
12 ms |
23792 KB |
Output is correct |
21 |
Correct |
12 ms |
23820 KB |
Output is correct |
22 |
Correct |
13 ms |
23768 KB |
Output is correct |
23 |
Correct |
12 ms |
23848 KB |
Output is correct |
24 |
Correct |
12 ms |
23792 KB |
Output is correct |
25 |
Correct |
11 ms |
23764 KB |
Output is correct |
26 |
Correct |
15 ms |
24344 KB |
Output is correct |
27 |
Correct |
13 ms |
24308 KB |
Output is correct |
28 |
Correct |
13 ms |
24416 KB |
Output is correct |
29 |
Correct |
14 ms |
24320 KB |
Output is correct |
30 |
Correct |
13 ms |
24280 KB |
Output is correct |
31 |
Correct |
14 ms |
23880 KB |
Output is correct |
32 |
Correct |
13 ms |
24532 KB |
Output is correct |
33 |
Correct |
11 ms |
23764 KB |
Output is correct |
34 |
Correct |
13 ms |
24280 KB |
Output is correct |
35 |
Correct |
14 ms |
24404 KB |
Output is correct |
36 |
Correct |
16 ms |
24216 KB |
Output is correct |
37 |
Correct |
13 ms |
24276 KB |
Output is correct |
38 |
Correct |
12 ms |
23800 KB |
Output is correct |
39 |
Correct |
14 ms |
24276 KB |
Output is correct |
40 |
Correct |
14 ms |
24312 KB |
Output is correct |
41 |
Correct |
15 ms |
24432 KB |
Output is correct |
42 |
Correct |
15 ms |
24352 KB |
Output is correct |
43 |
Correct |
17 ms |
24532 KB |
Output is correct |
44 |
Correct |
13 ms |
23892 KB |
Output is correct |
45 |
Correct |
17 ms |
24404 KB |
Output is correct |
46 |
Correct |
14 ms |
24276 KB |
Output is correct |
47 |
Correct |
12 ms |
23796 KB |
Output is correct |
48 |
Correct |
13 ms |
24340 KB |
Output is correct |
49 |
Correct |
13 ms |
24344 KB |
Output is correct |
50 |
Correct |
13 ms |
24404 KB |
Output is correct |
51 |
Correct |
13 ms |
24268 KB |
Output is correct |
52 |
Correct |
13 ms |
24344 KB |
Output is correct |
53 |
Correct |
13 ms |
24352 KB |
Output is correct |
54 |
Correct |
13 ms |
24276 KB |
Output is correct |
55 |
Correct |
12 ms |
23824 KB |
Output is correct |
56 |
Correct |
76 ms |
39512 KB |
Output is correct |
57 |
Correct |
101 ms |
39188 KB |
Output is correct |
58 |
Correct |
13 ms |
24276 KB |
Output is correct |
59 |
Correct |
12 ms |
23824 KB |
Output is correct |
60 |
Correct |
13 ms |
23804 KB |
Output is correct |
61 |
Correct |
97 ms |
39176 KB |
Output is correct |
62 |
Correct |
13 ms |
24276 KB |
Output is correct |
63 |
Correct |
145 ms |
46344 KB |
Output is correct |
64 |
Correct |
106 ms |
39212 KB |
Output is correct |
65 |
Correct |
14 ms |
24276 KB |
Output is correct |
66 |
Correct |
105 ms |
39856 KB |
Output is correct |
67 |
Correct |
14 ms |
24360 KB |
Output is correct |
68 |
Correct |
13 ms |
24276 KB |
Output is correct |
69 |
Correct |
78 ms |
39440 KB |
Output is correct |
70 |
Correct |
16 ms |
24532 KB |
Output is correct |
71 |
Correct |
110 ms |
39116 KB |
Output is correct |
72 |
Correct |
110 ms |
41460 KB |
Output is correct |
73 |
Correct |
13 ms |
23892 KB |
Output is correct |
74 |
Correct |
124 ms |
46632 KB |
Output is correct |
75 |
Correct |
119 ms |
42924 KB |
Output is correct |
76 |
Correct |
14 ms |
24276 KB |
Output is correct |
77 |
Correct |
14 ms |
24224 KB |
Output is correct |
78 |
Correct |
71 ms |
38064 KB |
Output is correct |
79 |
Correct |
85 ms |
41424 KB |
Output is correct |
80 |
Correct |
13 ms |
24312 KB |
Output is correct |
81 |
Correct |
14 ms |
24288 KB |
Output is correct |
82 |
Correct |
12 ms |
23844 KB |
Output is correct |
83 |
Correct |
12 ms |
23812 KB |
Output is correct |
84 |
Correct |
16 ms |
24332 KB |
Output is correct |
85 |
Correct |
115 ms |
40692 KB |
Output is correct |
86 |
Correct |
16 ms |
24276 KB |
Output is correct |
87 |
Correct |
96 ms |
39352 KB |
Output is correct |
88 |
Correct |
12 ms |
23892 KB |
Output is correct |
89 |
Correct |
105 ms |
39324 KB |
Output is correct |
90 |
Correct |
119 ms |
40828 KB |
Output is correct |
91 |
Correct |
108 ms |
42344 KB |
Output is correct |
92 |
Correct |
72 ms |
39460 KB |
Output is correct |
93 |
Correct |
13 ms |
24312 KB |
Output is correct |
94 |
Correct |
11 ms |
23828 KB |
Output is correct |
95 |
Correct |
100 ms |
42880 KB |
Output is correct |
96 |
Correct |
172 ms |
45944 KB |
Output is correct |
97 |
Correct |
14 ms |
24340 KB |
Output is correct |
98 |
Correct |
12 ms |
23892 KB |
Output is correct |
99 |
Correct |
85 ms |
41116 KB |
Output is correct |
100 |
Correct |
13 ms |
24276 KB |
Output is correct |
101 |
Correct |
103 ms |
39960 KB |
Output is correct |
102 |
Correct |
137 ms |
44768 KB |
Output is correct |
103 |
Correct |
14 ms |
24404 KB |
Output is correct |
104 |
Correct |
13 ms |
24448 KB |
Output is correct |
105 |
Correct |
16 ms |
24276 KB |
Output is correct |
106 |
Correct |
13 ms |
24276 KB |
Output is correct |
107 |
Correct |
13 ms |
24348 KB |
Output is correct |
108 |
Correct |
615 ms |
109664 KB |
Output is correct |
109 |
Correct |
827 ms |
118644 KB |
Output is correct |
110 |
Correct |
874 ms |
129768 KB |
Output is correct |
111 |
Correct |
969 ms |
139584 KB |
Output is correct |
112 |
Correct |
964 ms |
130884 KB |
Output is correct |
113 |
Correct |
629 ms |
119320 KB |
Output is correct |
114 |
Correct |
698 ms |
100488 KB |
Output is correct |
115 |
Correct |
716 ms |
100536 KB |
Output is correct |
116 |
Correct |
779 ms |
105228 KB |
Output is correct |
117 |
Correct |
822 ms |
117980 KB |
Output is correct |
118 |
Correct |
649 ms |
101096 KB |
Output is correct |
119 |
Correct |
799 ms |
117888 KB |
Output is correct |
120 |
Correct |
925 ms |
130436 KB |
Output is correct |
121 |
Correct |
782 ms |
117800 KB |
Output is correct |
122 |
Correct |
832 ms |
111448 KB |
Output is correct |
123 |
Correct |
533 ms |
121000 KB |
Output is correct |
124 |
Correct |
934 ms |
114684 KB |
Output is correct |