#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
bool remender(ll a , ll b){return a%b;}
const int N = 500002 , l = 25;
vector<int> adj[N];
int timer;
int tin[N], tout[N];
int up[N][l + 5];
int cnt[N];
void dfs(int v, int p){
tin[v] = ++timer;
up[v][0] = p;
for (int i = 1; i <= l; ++i)
up[v][i] = up[up[v][i-1]][i-1];
for (int u : adj[v]) {
if (u != p)
dfs(u, v);
}
tout[v] = ++timer;
}
bool is_ancestor(int u, int v){
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int lca(int u, int v){
if (is_ancestor(u, v))
return u;
if (is_ancestor(v, u))
return v;
for (int i = l; i >= 0; --i) {
if (!is_ancestor(up[u][i], v))
u = up[u][i];
}
return up[u][0];
}
struct UFDS {
int p[N],siz[N];
void init(int n){
for(int i = 0; i <= n; i++){
p[i] = i;
siz[i] = 1;
}
}
int get(int x){
return p[x]=(x==p[x] ? x : get(p[x]));
}
void unio(int u , int v){
int a = get(u);
int b=get(v);
if(a==b)return;
if(siz[a]>siz[b])swap(a,b);
p[b]=a;
siz[a]+=siz[b];
}
};
UFDS d;
void dfs1(int node , int par = -1){
for(int i : adj[node]){
if(i!=par){
dfs1(i,node);
if(cnt[i] != 0)d.unio(i , node);
cnt[node] += cnt[i];
}
}
}
bool cmp(int x , int y){
return tin[x] < tin[y];
}
void solve(){
int n;
cin >> n;
d.init(n + 2);
int k;
cin >> k;
for(int i = 0; i < n-1; i++){
int a , b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
vector<int> t[k + 1];
for(int i = 0; i < n; i++){
int x;
cin >> x;
t[x].pb(i + 1);
}
memset(cnt,0,sizeof cnt);
dfs(1,1);
for(int i = 1; i <= k; i++){
sort(all(t[i]),cmp);
for(int j = 0; j < (int)t[i].size(); j++){
int lc = lca(t[i][j] , t[i][(j+1)%t[i].size()]);
cnt[lc]-=2;
cnt[t[i][j]]++;
cnt[t[i][(j+1)%t[i].size()]]++;
}
}
dfs1(1,-1);
vector<int> leaf(n+1,0);
for(int i = 1; i <= n; i++){
for(int j : adj[i]){
if(d.get(i) != d.get(j))leaf[d.get(i)]++,leaf[d.get(j)]++;
}
}
int ans = 0;
for(int i = 1; i <= n; i++){
if(leaf[i] == 2)ans++;
}
cout << (ans + 1)/2 << '\n';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;cin >> t;while(t--)
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14028 KB |
Output is correct |
2 |
Correct |
7 ms |
14028 KB |
Output is correct |
3 |
Correct |
7 ms |
14044 KB |
Output is correct |
4 |
Correct |
7 ms |
14048 KB |
Output is correct |
5 |
Correct |
7 ms |
14028 KB |
Output is correct |
6 |
Correct |
7 ms |
14008 KB |
Output is correct |
7 |
Correct |
7 ms |
14028 KB |
Output is correct |
8 |
Correct |
7 ms |
13984 KB |
Output is correct |
9 |
Correct |
8 ms |
14028 KB |
Output is correct |
10 |
Correct |
7 ms |
13984 KB |
Output is correct |
11 |
Correct |
7 ms |
14028 KB |
Output is correct |
12 |
Correct |
7 ms |
14028 KB |
Output is correct |
13 |
Correct |
7 ms |
14028 KB |
Output is correct |
14 |
Correct |
7 ms |
14028 KB |
Output is correct |
15 |
Correct |
8 ms |
14056 KB |
Output is correct |
16 |
Correct |
7 ms |
14028 KB |
Output is correct |
17 |
Correct |
8 ms |
14028 KB |
Output is correct |
18 |
Correct |
8 ms |
14028 KB |
Output is correct |
19 |
Correct |
7 ms |
13980 KB |
Output is correct |
20 |
Correct |
7 ms |
14028 KB |
Output is correct |
21 |
Correct |
7 ms |
14028 KB |
Output is correct |
22 |
Correct |
7 ms |
14028 KB |
Output is correct |
23 |
Correct |
7 ms |
14028 KB |
Output is correct |
24 |
Correct |
7 ms |
14028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14028 KB |
Output is correct |
2 |
Correct |
7 ms |
14028 KB |
Output is correct |
3 |
Correct |
7 ms |
14044 KB |
Output is correct |
4 |
Correct |
7 ms |
14048 KB |
Output is correct |
5 |
Correct |
7 ms |
14028 KB |
Output is correct |
6 |
Correct |
7 ms |
14008 KB |
Output is correct |
7 |
Correct |
7 ms |
14028 KB |
Output is correct |
8 |
Correct |
7 ms |
13984 KB |
Output is correct |
9 |
Correct |
8 ms |
14028 KB |
Output is correct |
10 |
Correct |
7 ms |
13984 KB |
Output is correct |
11 |
Correct |
7 ms |
14028 KB |
Output is correct |
12 |
Correct |
7 ms |
14028 KB |
Output is correct |
13 |
Correct |
7 ms |
14028 KB |
Output is correct |
14 |
Correct |
7 ms |
14028 KB |
Output is correct |
15 |
Correct |
8 ms |
14056 KB |
Output is correct |
16 |
Correct |
7 ms |
14028 KB |
Output is correct |
17 |
Correct |
8 ms |
14028 KB |
Output is correct |
18 |
Correct |
8 ms |
14028 KB |
Output is correct |
19 |
Correct |
7 ms |
13980 KB |
Output is correct |
20 |
Correct |
7 ms |
14028 KB |
Output is correct |
21 |
Correct |
7 ms |
14028 KB |
Output is correct |
22 |
Correct |
7 ms |
14028 KB |
Output is correct |
23 |
Correct |
7 ms |
14028 KB |
Output is correct |
24 |
Correct |
7 ms |
14028 KB |
Output is correct |
25 |
Correct |
7 ms |
13984 KB |
Output is correct |
26 |
Correct |
10 ms |
14608 KB |
Output is correct |
27 |
Correct |
9 ms |
14540 KB |
Output is correct |
28 |
Correct |
9 ms |
14960 KB |
Output is correct |
29 |
Correct |
9 ms |
14668 KB |
Output is correct |
30 |
Correct |
10 ms |
14540 KB |
Output is correct |
31 |
Correct |
8 ms |
13960 KB |
Output is correct |
32 |
Correct |
10 ms |
15052 KB |
Output is correct |
33 |
Correct |
7 ms |
14028 KB |
Output is correct |
34 |
Correct |
13 ms |
14540 KB |
Output is correct |
35 |
Correct |
10 ms |
14764 KB |
Output is correct |
36 |
Correct |
10 ms |
14620 KB |
Output is correct |
37 |
Correct |
10 ms |
14636 KB |
Output is correct |
38 |
Correct |
7 ms |
14028 KB |
Output is correct |
39 |
Correct |
10 ms |
14608 KB |
Output is correct |
40 |
Correct |
9 ms |
14540 KB |
Output is correct |
41 |
Correct |
9 ms |
14540 KB |
Output is correct |
42 |
Correct |
9 ms |
14684 KB |
Output is correct |
43 |
Correct |
9 ms |
14924 KB |
Output is correct |
44 |
Correct |
7 ms |
14028 KB |
Output is correct |
45 |
Correct |
9 ms |
14796 KB |
Output is correct |
46 |
Correct |
9 ms |
14668 KB |
Output is correct |
47 |
Correct |
7 ms |
14028 KB |
Output is correct |
48 |
Correct |
10 ms |
14628 KB |
Output is correct |
49 |
Correct |
9 ms |
14668 KB |
Output is correct |
50 |
Correct |
9 ms |
14796 KB |
Output is correct |
51 |
Correct |
10 ms |
14644 KB |
Output is correct |
52 |
Correct |
10 ms |
14588 KB |
Output is correct |
53 |
Correct |
10 ms |
14636 KB |
Output is correct |
54 |
Correct |
9 ms |
14508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14028 KB |
Output is correct |
2 |
Correct |
7 ms |
14028 KB |
Output is correct |
3 |
Correct |
7 ms |
14044 KB |
Output is correct |
4 |
Correct |
7 ms |
14048 KB |
Output is correct |
5 |
Correct |
7 ms |
14028 KB |
Output is correct |
6 |
Correct |
7 ms |
14008 KB |
Output is correct |
7 |
Correct |
7 ms |
14028 KB |
Output is correct |
8 |
Correct |
7 ms |
13984 KB |
Output is correct |
9 |
Correct |
8 ms |
14028 KB |
Output is correct |
10 |
Correct |
7 ms |
13984 KB |
Output is correct |
11 |
Correct |
7 ms |
14028 KB |
Output is correct |
12 |
Correct |
7 ms |
14028 KB |
Output is correct |
13 |
Correct |
7 ms |
14028 KB |
Output is correct |
14 |
Correct |
7 ms |
14028 KB |
Output is correct |
15 |
Correct |
8 ms |
14056 KB |
Output is correct |
16 |
Correct |
7 ms |
14028 KB |
Output is correct |
17 |
Correct |
8 ms |
14028 KB |
Output is correct |
18 |
Correct |
8 ms |
14028 KB |
Output is correct |
19 |
Correct |
7 ms |
13980 KB |
Output is correct |
20 |
Correct |
7 ms |
14028 KB |
Output is correct |
21 |
Correct |
7 ms |
14028 KB |
Output is correct |
22 |
Correct |
7 ms |
14028 KB |
Output is correct |
23 |
Correct |
7 ms |
14028 KB |
Output is correct |
24 |
Correct |
7 ms |
14028 KB |
Output is correct |
25 |
Correct |
7 ms |
14028 KB |
Output is correct |
26 |
Correct |
111 ms |
33716 KB |
Output is correct |
27 |
Correct |
136 ms |
32844 KB |
Output is correct |
28 |
Correct |
9 ms |
14540 KB |
Output is correct |
29 |
Correct |
7 ms |
14028 KB |
Output is correct |
30 |
Correct |
7 ms |
14028 KB |
Output is correct |
31 |
Correct |
142 ms |
32856 KB |
Output is correct |
32 |
Correct |
10 ms |
14572 KB |
Output is correct |
33 |
Correct |
137 ms |
45900 KB |
Output is correct |
34 |
Correct |
148 ms |
33064 KB |
Output is correct |
35 |
Correct |
10 ms |
14572 KB |
Output is correct |
36 |
Correct |
131 ms |
34376 KB |
Output is correct |
37 |
Correct |
9 ms |
14540 KB |
Output is correct |
38 |
Correct |
9 ms |
14540 KB |
Output is correct |
39 |
Correct |
105 ms |
33724 KB |
Output is correct |
40 |
Correct |
10 ms |
14924 KB |
Output is correct |
41 |
Correct |
133 ms |
32812 KB |
Output is correct |
42 |
Correct |
145 ms |
37156 KB |
Output is correct |
43 |
Correct |
7 ms |
14028 KB |
Output is correct |
44 |
Correct |
157 ms |
46264 KB |
Output is correct |
45 |
Correct |
164 ms |
39620 KB |
Output is correct |
46 |
Correct |
9 ms |
14636 KB |
Output is correct |
47 |
Correct |
9 ms |
14608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
32208 KB |
Output is correct |
2 |
Correct |
87 ms |
36668 KB |
Output is correct |
3 |
Correct |
9 ms |
14668 KB |
Output is correct |
4 |
Correct |
10 ms |
14548 KB |
Output is correct |
5 |
Correct |
7 ms |
14028 KB |
Output is correct |
6 |
Correct |
7 ms |
14028 KB |
Output is correct |
7 |
Correct |
10 ms |
14540 KB |
Output is correct |
8 |
Correct |
140 ms |
35428 KB |
Output is correct |
9 |
Correct |
9 ms |
14540 KB |
Output is correct |
10 |
Correct |
125 ms |
33124 KB |
Output is correct |
11 |
Correct |
8 ms |
13984 KB |
Output is correct |
12 |
Correct |
121 ms |
33104 KB |
Output is correct |
13 |
Correct |
154 ms |
35324 KB |
Output is correct |
14 |
Correct |
120 ms |
38192 KB |
Output is correct |
15 |
Correct |
103 ms |
33724 KB |
Output is correct |
16 |
Correct |
9 ms |
14668 KB |
Output is correct |
17 |
Correct |
7 ms |
14028 KB |
Output is correct |
18 |
Correct |
95 ms |
37972 KB |
Output is correct |
19 |
Correct |
147 ms |
45252 KB |
Output is correct |
20 |
Correct |
10 ms |
14668 KB |
Output is correct |
21 |
Correct |
7 ms |
14028 KB |
Output is correct |
22 |
Correct |
114 ms |
36772 KB |
Output is correct |
23 |
Correct |
10 ms |
14540 KB |
Output is correct |
24 |
Correct |
116 ms |
33572 KB |
Output is correct |
25 |
Correct |
167 ms |
42932 KB |
Output is correct |
26 |
Correct |
9 ms |
14668 KB |
Output is correct |
27 |
Correct |
10 ms |
14796 KB |
Output is correct |
28 |
Correct |
12 ms |
14540 KB |
Output is correct |
29 |
Correct |
10 ms |
14636 KB |
Output is correct |
30 |
Correct |
10 ms |
14668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14028 KB |
Output is correct |
2 |
Correct |
7 ms |
14028 KB |
Output is correct |
3 |
Correct |
7 ms |
14044 KB |
Output is correct |
4 |
Correct |
7 ms |
14048 KB |
Output is correct |
5 |
Correct |
7 ms |
14028 KB |
Output is correct |
6 |
Correct |
7 ms |
14008 KB |
Output is correct |
7 |
Correct |
7 ms |
14028 KB |
Output is correct |
8 |
Correct |
7 ms |
13984 KB |
Output is correct |
9 |
Correct |
8 ms |
14028 KB |
Output is correct |
10 |
Correct |
7 ms |
13984 KB |
Output is correct |
11 |
Correct |
7 ms |
14028 KB |
Output is correct |
12 |
Correct |
7 ms |
14028 KB |
Output is correct |
13 |
Correct |
7 ms |
14028 KB |
Output is correct |
14 |
Correct |
7 ms |
14028 KB |
Output is correct |
15 |
Correct |
8 ms |
14056 KB |
Output is correct |
16 |
Correct |
7 ms |
14028 KB |
Output is correct |
17 |
Correct |
8 ms |
14028 KB |
Output is correct |
18 |
Correct |
8 ms |
14028 KB |
Output is correct |
19 |
Correct |
7 ms |
13980 KB |
Output is correct |
20 |
Correct |
7 ms |
14028 KB |
Output is correct |
21 |
Correct |
7 ms |
14028 KB |
Output is correct |
22 |
Correct |
7 ms |
14028 KB |
Output is correct |
23 |
Correct |
7 ms |
14028 KB |
Output is correct |
24 |
Correct |
7 ms |
14028 KB |
Output is correct |
25 |
Correct |
7 ms |
13984 KB |
Output is correct |
26 |
Correct |
10 ms |
14608 KB |
Output is correct |
27 |
Correct |
9 ms |
14540 KB |
Output is correct |
28 |
Correct |
9 ms |
14960 KB |
Output is correct |
29 |
Correct |
9 ms |
14668 KB |
Output is correct |
30 |
Correct |
10 ms |
14540 KB |
Output is correct |
31 |
Correct |
8 ms |
13960 KB |
Output is correct |
32 |
Correct |
10 ms |
15052 KB |
Output is correct |
33 |
Correct |
7 ms |
14028 KB |
Output is correct |
34 |
Correct |
13 ms |
14540 KB |
Output is correct |
35 |
Correct |
10 ms |
14764 KB |
Output is correct |
36 |
Correct |
10 ms |
14620 KB |
Output is correct |
37 |
Correct |
10 ms |
14636 KB |
Output is correct |
38 |
Correct |
7 ms |
14028 KB |
Output is correct |
39 |
Correct |
10 ms |
14608 KB |
Output is correct |
40 |
Correct |
9 ms |
14540 KB |
Output is correct |
41 |
Correct |
9 ms |
14540 KB |
Output is correct |
42 |
Correct |
9 ms |
14684 KB |
Output is correct |
43 |
Correct |
9 ms |
14924 KB |
Output is correct |
44 |
Correct |
7 ms |
14028 KB |
Output is correct |
45 |
Correct |
9 ms |
14796 KB |
Output is correct |
46 |
Correct |
9 ms |
14668 KB |
Output is correct |
47 |
Correct |
7 ms |
14028 KB |
Output is correct |
48 |
Correct |
10 ms |
14628 KB |
Output is correct |
49 |
Correct |
9 ms |
14668 KB |
Output is correct |
50 |
Correct |
9 ms |
14796 KB |
Output is correct |
51 |
Correct |
10 ms |
14644 KB |
Output is correct |
52 |
Correct |
10 ms |
14588 KB |
Output is correct |
53 |
Correct |
10 ms |
14636 KB |
Output is correct |
54 |
Correct |
9 ms |
14508 KB |
Output is correct |
55 |
Correct |
7 ms |
14028 KB |
Output is correct |
56 |
Correct |
111 ms |
33716 KB |
Output is correct |
57 |
Correct |
136 ms |
32844 KB |
Output is correct |
58 |
Correct |
9 ms |
14540 KB |
Output is correct |
59 |
Correct |
7 ms |
14028 KB |
Output is correct |
60 |
Correct |
7 ms |
14028 KB |
Output is correct |
61 |
Correct |
142 ms |
32856 KB |
Output is correct |
62 |
Correct |
10 ms |
14572 KB |
Output is correct |
63 |
Correct |
137 ms |
45900 KB |
Output is correct |
64 |
Correct |
148 ms |
33064 KB |
Output is correct |
65 |
Correct |
10 ms |
14572 KB |
Output is correct |
66 |
Correct |
131 ms |
34376 KB |
Output is correct |
67 |
Correct |
9 ms |
14540 KB |
Output is correct |
68 |
Correct |
9 ms |
14540 KB |
Output is correct |
69 |
Correct |
105 ms |
33724 KB |
Output is correct |
70 |
Correct |
10 ms |
14924 KB |
Output is correct |
71 |
Correct |
133 ms |
32812 KB |
Output is correct |
72 |
Correct |
145 ms |
37156 KB |
Output is correct |
73 |
Correct |
7 ms |
14028 KB |
Output is correct |
74 |
Correct |
157 ms |
46264 KB |
Output is correct |
75 |
Correct |
164 ms |
39620 KB |
Output is correct |
76 |
Correct |
9 ms |
14636 KB |
Output is correct |
77 |
Correct |
9 ms |
14608 KB |
Output is correct |
78 |
Correct |
104 ms |
32208 KB |
Output is correct |
79 |
Correct |
87 ms |
36668 KB |
Output is correct |
80 |
Correct |
9 ms |
14668 KB |
Output is correct |
81 |
Correct |
10 ms |
14548 KB |
Output is correct |
82 |
Correct |
7 ms |
14028 KB |
Output is correct |
83 |
Correct |
7 ms |
14028 KB |
Output is correct |
84 |
Correct |
10 ms |
14540 KB |
Output is correct |
85 |
Correct |
140 ms |
35428 KB |
Output is correct |
86 |
Correct |
9 ms |
14540 KB |
Output is correct |
87 |
Correct |
125 ms |
33124 KB |
Output is correct |
88 |
Correct |
8 ms |
13984 KB |
Output is correct |
89 |
Correct |
121 ms |
33104 KB |
Output is correct |
90 |
Correct |
154 ms |
35324 KB |
Output is correct |
91 |
Correct |
120 ms |
38192 KB |
Output is correct |
92 |
Correct |
103 ms |
33724 KB |
Output is correct |
93 |
Correct |
9 ms |
14668 KB |
Output is correct |
94 |
Correct |
7 ms |
14028 KB |
Output is correct |
95 |
Correct |
95 ms |
37972 KB |
Output is correct |
96 |
Correct |
147 ms |
45252 KB |
Output is correct |
97 |
Correct |
10 ms |
14668 KB |
Output is correct |
98 |
Correct |
7 ms |
14028 KB |
Output is correct |
99 |
Correct |
114 ms |
36772 KB |
Output is correct |
100 |
Correct |
10 ms |
14540 KB |
Output is correct |
101 |
Correct |
116 ms |
33572 KB |
Output is correct |
102 |
Correct |
167 ms |
42932 KB |
Output is correct |
103 |
Correct |
9 ms |
14668 KB |
Output is correct |
104 |
Correct |
10 ms |
14796 KB |
Output is correct |
105 |
Correct |
12 ms |
14540 KB |
Output is correct |
106 |
Correct |
10 ms |
14636 KB |
Output is correct |
107 |
Correct |
10 ms |
14668 KB |
Output is correct |
108 |
Correct |
770 ms |
124216 KB |
Output is correct |
109 |
Correct |
1139 ms |
141148 KB |
Output is correct |
110 |
Correct |
1112 ms |
161332 KB |
Output is correct |
111 |
Correct |
1157 ms |
177940 KB |
Output is correct |
112 |
Correct |
1126 ms |
160504 KB |
Output is correct |
113 |
Correct |
737 ms |
135464 KB |
Output is correct |
114 |
Correct |
893 ms |
108536 KB |
Output is correct |
115 |
Correct |
919 ms |
108496 KB |
Output is correct |
116 |
Correct |
1095 ms |
115396 KB |
Output is correct |
117 |
Correct |
889 ms |
136036 KB |
Output is correct |
118 |
Correct |
919 ms |
108612 KB |
Output is correct |
119 |
Correct |
912 ms |
136104 KB |
Output is correct |
120 |
Correct |
1116 ms |
161604 KB |
Output is correct |
121 |
Correct |
889 ms |
136116 KB |
Output is correct |
122 |
Correct |
1020 ms |
125008 KB |
Output is correct |
123 |
Correct |
675 ms |
137652 KB |
Output is correct |
124 |
Correct |
1071 ms |
133716 KB |
Output is correct |