#include <bits/stdc++.h>
// #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
#define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define pb push_back
#define ss second
#define ff first
void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, maxn = 5e5+5, lg = 20, inf = ll(1e9) + 5;
ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll);return k*k%md*(b&1ll?a:1)%md;}
int st[maxn], par[maxn][lg], h[maxn], vl[maxn];
vector<int> adj[maxn], ct[maxn];
void dfs(int r, int p){
par[r][0] = p;
rep(i,1,lg) par[r][i] = par[par[r][i-1]][i-1];
for(int c: adj[r]) if(c - p) h[c] = h[r] + 1, dfs(c, r);
}
int lca(int u, int v){
if(h[u] > h[v]) swap(u, v);
per(i,lg-1,0) if(h[par[v][i]] >= h[u]) v = par[v][i];
if(u == v) return u;
per(i,lg-1,0) if(par[u][i] - par[v][i]) u = par[u][i], v = par[v][i];
return par[u][0];
}
int dfs2(int r, int p){
int res = 0;
for(int c: adj[r]) if(c - p){
res += dfs2(c, r);
st[r] += st[c];
}
if(st[r] || !r) return vl[r] = -1, res;
vl[r] = res;
return res + 1;
}
int main(){
cin.tie(0) -> sync_with_stdio(0);
int n, k; cin >> n >> k;
rep(i,1,n){
int u, v; cin >> u >> v; u--, v--;
adj[u].pb(v), adj[v].pb(u);
} dfs(0, 0);
rep(i,0,n){
int t; cin >> t; t--;
ct[t].pb(i);
}
rep(i,0,k){
int lc = ct[i].back();
for(int c: ct[i]) lc = lca(lc, c);
for(int c: ct[i]) st[c]++, st[lc]--;
}
int ans = 1, tot = dfs2(0, 0);
rep(i,0,n) if(vl[i] + 1 && (vl[i] == tot-1 || vl[i] == 0)) ans++;
cout << ans/2 << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
14 ms |
23764 KB |
Output is correct |
7 |
Correct |
14 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23780 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
15 ms |
23756 KB |
Output is correct |
11 |
Correct |
16 ms |
23820 KB |
Output is correct |
12 |
Correct |
13 ms |
23768 KB |
Output is correct |
13 |
Correct |
17 ms |
23824 KB |
Output is correct |
14 |
Correct |
19 ms |
23764 KB |
Output is correct |
15 |
Correct |
15 ms |
23820 KB |
Output is correct |
16 |
Correct |
14 ms |
23812 KB |
Output is correct |
17 |
Correct |
14 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23812 KB |
Output is correct |
19 |
Correct |
12 ms |
23732 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
14 ms |
23764 KB |
Output is correct |
22 |
Correct |
19 ms |
23792 KB |
Output is correct |
23 |
Correct |
13 ms |
23764 KB |
Output is correct |
24 |
Correct |
14 ms |
23820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
14 ms |
23764 KB |
Output is correct |
7 |
Correct |
14 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23780 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
15 ms |
23756 KB |
Output is correct |
11 |
Correct |
16 ms |
23820 KB |
Output is correct |
12 |
Correct |
13 ms |
23768 KB |
Output is correct |
13 |
Correct |
17 ms |
23824 KB |
Output is correct |
14 |
Correct |
19 ms |
23764 KB |
Output is correct |
15 |
Correct |
15 ms |
23820 KB |
Output is correct |
16 |
Correct |
14 ms |
23812 KB |
Output is correct |
17 |
Correct |
14 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23812 KB |
Output is correct |
19 |
Correct |
12 ms |
23732 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
14 ms |
23764 KB |
Output is correct |
22 |
Correct |
19 ms |
23792 KB |
Output is correct |
23 |
Correct |
13 ms |
23764 KB |
Output is correct |
24 |
Correct |
14 ms |
23820 KB |
Output is correct |
25 |
Correct |
13 ms |
23740 KB |
Output is correct |
26 |
Correct |
15 ms |
24276 KB |
Output is correct |
27 |
Correct |
18 ms |
24212 KB |
Output is correct |
28 |
Correct |
21 ms |
24340 KB |
Output is correct |
29 |
Correct |
17 ms |
24276 KB |
Output is correct |
30 |
Correct |
16 ms |
24200 KB |
Output is correct |
31 |
Correct |
19 ms |
23836 KB |
Output is correct |
32 |
Correct |
15 ms |
24404 KB |
Output is correct |
33 |
Correct |
13 ms |
23816 KB |
Output is correct |
34 |
Correct |
15 ms |
24244 KB |
Output is correct |
35 |
Correct |
19 ms |
24244 KB |
Output is correct |
36 |
Correct |
17 ms |
24140 KB |
Output is correct |
37 |
Correct |
16 ms |
24276 KB |
Output is correct |
38 |
Correct |
18 ms |
23764 KB |
Output is correct |
39 |
Correct |
16 ms |
24276 KB |
Output is correct |
40 |
Correct |
17 ms |
24148 KB |
Output is correct |
41 |
Correct |
14 ms |
24212 KB |
Output is correct |
42 |
Correct |
14 ms |
24276 KB |
Output is correct |
43 |
Correct |
16 ms |
24432 KB |
Output is correct |
44 |
Correct |
16 ms |
23816 KB |
Output is correct |
45 |
Correct |
17 ms |
24340 KB |
Output is correct |
46 |
Correct |
17 ms |
24212 KB |
Output is correct |
47 |
Correct |
15 ms |
23816 KB |
Output is correct |
48 |
Correct |
19 ms |
24204 KB |
Output is correct |
49 |
Correct |
19 ms |
24336 KB |
Output is correct |
50 |
Correct |
16 ms |
24328 KB |
Output is correct |
51 |
Correct |
18 ms |
24148 KB |
Output is correct |
52 |
Correct |
16 ms |
24212 KB |
Output is correct |
53 |
Correct |
19 ms |
24212 KB |
Output is correct |
54 |
Correct |
17 ms |
24156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
14 ms |
23764 KB |
Output is correct |
7 |
Correct |
14 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23780 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
15 ms |
23756 KB |
Output is correct |
11 |
Correct |
16 ms |
23820 KB |
Output is correct |
12 |
Correct |
13 ms |
23768 KB |
Output is correct |
13 |
Correct |
17 ms |
23824 KB |
Output is correct |
14 |
Correct |
19 ms |
23764 KB |
Output is correct |
15 |
Correct |
15 ms |
23820 KB |
Output is correct |
16 |
Correct |
14 ms |
23812 KB |
Output is correct |
17 |
Correct |
14 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23812 KB |
Output is correct |
19 |
Correct |
12 ms |
23732 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
14 ms |
23764 KB |
Output is correct |
22 |
Correct |
19 ms |
23792 KB |
Output is correct |
23 |
Correct |
13 ms |
23764 KB |
Output is correct |
24 |
Correct |
14 ms |
23820 KB |
Output is correct |
25 |
Correct |
16 ms |
23764 KB |
Output is correct |
26 |
Correct |
104 ms |
37692 KB |
Output is correct |
27 |
Correct |
147 ms |
37344 KB |
Output is correct |
28 |
Correct |
19 ms |
24236 KB |
Output is correct |
29 |
Correct |
15 ms |
23816 KB |
Output is correct |
30 |
Correct |
16 ms |
23796 KB |
Output is correct |
31 |
Correct |
139 ms |
37316 KB |
Output is correct |
32 |
Correct |
16 ms |
24220 KB |
Output is correct |
33 |
Correct |
156 ms |
44448 KB |
Output is correct |
34 |
Correct |
143 ms |
37516 KB |
Output is correct |
35 |
Correct |
18 ms |
24256 KB |
Output is correct |
36 |
Correct |
148 ms |
37980 KB |
Output is correct |
37 |
Correct |
18 ms |
24232 KB |
Output is correct |
38 |
Correct |
15 ms |
24176 KB |
Output is correct |
39 |
Correct |
117 ms |
37652 KB |
Output is correct |
40 |
Correct |
16 ms |
24404 KB |
Output is correct |
41 |
Correct |
123 ms |
37284 KB |
Output is correct |
42 |
Correct |
140 ms |
39692 KB |
Output is correct |
43 |
Correct |
14 ms |
23824 KB |
Output is correct |
44 |
Correct |
146 ms |
44792 KB |
Output is correct |
45 |
Correct |
139 ms |
41052 KB |
Output is correct |
46 |
Correct |
16 ms |
24260 KB |
Output is correct |
47 |
Correct |
15 ms |
24160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
37184 KB |
Output is correct |
2 |
Correct |
110 ms |
39668 KB |
Output is correct |
3 |
Correct |
16 ms |
24296 KB |
Output is correct |
4 |
Correct |
15 ms |
24160 KB |
Output is correct |
5 |
Correct |
18 ms |
23768 KB |
Output is correct |
6 |
Correct |
15 ms |
23768 KB |
Output is correct |
7 |
Correct |
18 ms |
24228 KB |
Output is correct |
8 |
Correct |
138 ms |
38400 KB |
Output is correct |
9 |
Correct |
14 ms |
24148 KB |
Output is correct |
10 |
Correct |
137 ms |
37408 KB |
Output is correct |
11 |
Correct |
15 ms |
23808 KB |
Output is correct |
12 |
Correct |
147 ms |
37348 KB |
Output is correct |
13 |
Correct |
135 ms |
38244 KB |
Output is correct |
14 |
Correct |
137 ms |
39960 KB |
Output is correct |
15 |
Correct |
95 ms |
37628 KB |
Output is correct |
16 |
Correct |
20 ms |
24268 KB |
Output is correct |
17 |
Correct |
17 ms |
23764 KB |
Output is correct |
18 |
Correct |
131 ms |
39928 KB |
Output is correct |
19 |
Correct |
176 ms |
43816 KB |
Output is correct |
20 |
Correct |
19 ms |
24180 KB |
Output is correct |
21 |
Correct |
17 ms |
23812 KB |
Output is correct |
22 |
Correct |
117 ms |
38756 KB |
Output is correct |
23 |
Correct |
16 ms |
24148 KB |
Output is correct |
24 |
Correct |
146 ms |
37652 KB |
Output is correct |
25 |
Correct |
172 ms |
42524 KB |
Output is correct |
26 |
Correct |
18 ms |
24332 KB |
Output is correct |
27 |
Correct |
15 ms |
24276 KB |
Output is correct |
28 |
Correct |
19 ms |
24260 KB |
Output is correct |
29 |
Correct |
18 ms |
24212 KB |
Output is correct |
30 |
Correct |
19 ms |
24276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
14 ms |
23764 KB |
Output is correct |
7 |
Correct |
14 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23780 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
15 ms |
23756 KB |
Output is correct |
11 |
Correct |
16 ms |
23820 KB |
Output is correct |
12 |
Correct |
13 ms |
23768 KB |
Output is correct |
13 |
Correct |
17 ms |
23824 KB |
Output is correct |
14 |
Correct |
19 ms |
23764 KB |
Output is correct |
15 |
Correct |
15 ms |
23820 KB |
Output is correct |
16 |
Correct |
14 ms |
23812 KB |
Output is correct |
17 |
Correct |
14 ms |
23764 KB |
Output is correct |
18 |
Correct |
12 ms |
23812 KB |
Output is correct |
19 |
Correct |
12 ms |
23732 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
14 ms |
23764 KB |
Output is correct |
22 |
Correct |
19 ms |
23792 KB |
Output is correct |
23 |
Correct |
13 ms |
23764 KB |
Output is correct |
24 |
Correct |
14 ms |
23820 KB |
Output is correct |
25 |
Correct |
13 ms |
23740 KB |
Output is correct |
26 |
Correct |
15 ms |
24276 KB |
Output is correct |
27 |
Correct |
18 ms |
24212 KB |
Output is correct |
28 |
Correct |
21 ms |
24340 KB |
Output is correct |
29 |
Correct |
17 ms |
24276 KB |
Output is correct |
30 |
Correct |
16 ms |
24200 KB |
Output is correct |
31 |
Correct |
19 ms |
23836 KB |
Output is correct |
32 |
Correct |
15 ms |
24404 KB |
Output is correct |
33 |
Correct |
13 ms |
23816 KB |
Output is correct |
34 |
Correct |
15 ms |
24244 KB |
Output is correct |
35 |
Correct |
19 ms |
24244 KB |
Output is correct |
36 |
Correct |
17 ms |
24140 KB |
Output is correct |
37 |
Correct |
16 ms |
24276 KB |
Output is correct |
38 |
Correct |
18 ms |
23764 KB |
Output is correct |
39 |
Correct |
16 ms |
24276 KB |
Output is correct |
40 |
Correct |
17 ms |
24148 KB |
Output is correct |
41 |
Correct |
14 ms |
24212 KB |
Output is correct |
42 |
Correct |
14 ms |
24276 KB |
Output is correct |
43 |
Correct |
16 ms |
24432 KB |
Output is correct |
44 |
Correct |
16 ms |
23816 KB |
Output is correct |
45 |
Correct |
17 ms |
24340 KB |
Output is correct |
46 |
Correct |
17 ms |
24212 KB |
Output is correct |
47 |
Correct |
15 ms |
23816 KB |
Output is correct |
48 |
Correct |
19 ms |
24204 KB |
Output is correct |
49 |
Correct |
19 ms |
24336 KB |
Output is correct |
50 |
Correct |
16 ms |
24328 KB |
Output is correct |
51 |
Correct |
18 ms |
24148 KB |
Output is correct |
52 |
Correct |
16 ms |
24212 KB |
Output is correct |
53 |
Correct |
19 ms |
24212 KB |
Output is correct |
54 |
Correct |
17 ms |
24156 KB |
Output is correct |
55 |
Correct |
16 ms |
23764 KB |
Output is correct |
56 |
Correct |
104 ms |
37692 KB |
Output is correct |
57 |
Correct |
147 ms |
37344 KB |
Output is correct |
58 |
Correct |
19 ms |
24236 KB |
Output is correct |
59 |
Correct |
15 ms |
23816 KB |
Output is correct |
60 |
Correct |
16 ms |
23796 KB |
Output is correct |
61 |
Correct |
139 ms |
37316 KB |
Output is correct |
62 |
Correct |
16 ms |
24220 KB |
Output is correct |
63 |
Correct |
156 ms |
44448 KB |
Output is correct |
64 |
Correct |
143 ms |
37516 KB |
Output is correct |
65 |
Correct |
18 ms |
24256 KB |
Output is correct |
66 |
Correct |
148 ms |
37980 KB |
Output is correct |
67 |
Correct |
18 ms |
24232 KB |
Output is correct |
68 |
Correct |
15 ms |
24176 KB |
Output is correct |
69 |
Correct |
117 ms |
37652 KB |
Output is correct |
70 |
Correct |
16 ms |
24404 KB |
Output is correct |
71 |
Correct |
123 ms |
37284 KB |
Output is correct |
72 |
Correct |
140 ms |
39692 KB |
Output is correct |
73 |
Correct |
14 ms |
23824 KB |
Output is correct |
74 |
Correct |
146 ms |
44792 KB |
Output is correct |
75 |
Correct |
139 ms |
41052 KB |
Output is correct |
76 |
Correct |
16 ms |
24260 KB |
Output is correct |
77 |
Correct |
15 ms |
24160 KB |
Output is correct |
78 |
Correct |
92 ms |
37184 KB |
Output is correct |
79 |
Correct |
110 ms |
39668 KB |
Output is correct |
80 |
Correct |
16 ms |
24296 KB |
Output is correct |
81 |
Correct |
15 ms |
24160 KB |
Output is correct |
82 |
Correct |
18 ms |
23768 KB |
Output is correct |
83 |
Correct |
15 ms |
23768 KB |
Output is correct |
84 |
Correct |
18 ms |
24228 KB |
Output is correct |
85 |
Correct |
138 ms |
38400 KB |
Output is correct |
86 |
Correct |
14 ms |
24148 KB |
Output is correct |
87 |
Correct |
137 ms |
37408 KB |
Output is correct |
88 |
Correct |
15 ms |
23808 KB |
Output is correct |
89 |
Correct |
147 ms |
37348 KB |
Output is correct |
90 |
Correct |
135 ms |
38244 KB |
Output is correct |
91 |
Correct |
137 ms |
39960 KB |
Output is correct |
92 |
Correct |
95 ms |
37628 KB |
Output is correct |
93 |
Correct |
20 ms |
24268 KB |
Output is correct |
94 |
Correct |
17 ms |
23764 KB |
Output is correct |
95 |
Correct |
131 ms |
39928 KB |
Output is correct |
96 |
Correct |
176 ms |
43816 KB |
Output is correct |
97 |
Correct |
19 ms |
24180 KB |
Output is correct |
98 |
Correct |
17 ms |
23812 KB |
Output is correct |
99 |
Correct |
117 ms |
38756 KB |
Output is correct |
100 |
Correct |
16 ms |
24148 KB |
Output is correct |
101 |
Correct |
146 ms |
37652 KB |
Output is correct |
102 |
Correct |
172 ms |
42524 KB |
Output is correct |
103 |
Correct |
18 ms |
24332 KB |
Output is correct |
104 |
Correct |
15 ms |
24276 KB |
Output is correct |
105 |
Correct |
19 ms |
24260 KB |
Output is correct |
106 |
Correct |
18 ms |
24212 KB |
Output is correct |
107 |
Correct |
19 ms |
24276 KB |
Output is correct |
108 |
Correct |
602 ms |
93624 KB |
Output is correct |
109 |
Correct |
981 ms |
105980 KB |
Output is correct |
110 |
Correct |
890 ms |
117044 KB |
Output is correct |
111 |
Correct |
1167 ms |
124504 KB |
Output is correct |
112 |
Correct |
1131 ms |
114752 KB |
Output is correct |
113 |
Correct |
683 ms |
101332 KB |
Output is correct |
114 |
Correct |
671 ms |
87844 KB |
Output is correct |
115 |
Correct |
677 ms |
87860 KB |
Output is correct |
116 |
Correct |
916 ms |
90576 KB |
Output is correct |
117 |
Correct |
910 ms |
101444 KB |
Output is correct |
118 |
Correct |
706 ms |
87756 KB |
Output is correct |
119 |
Correct |
883 ms |
101308 KB |
Output is correct |
120 |
Correct |
1098 ms |
115352 KB |
Output is correct |
121 |
Correct |
932 ms |
101412 KB |
Output is correct |
122 |
Correct |
905 ms |
95132 KB |
Output is correct |
123 |
Correct |
688 ms |
102520 KB |
Output is correct |
124 |
Correct |
1076 ms |
99436 KB |
Output is correct |