#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); er(tot);
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 |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23816 KB |
Output is correct |
3 |
Correct |
12 ms |
23852 KB |
Output is correct |
4 |
Correct |
12 ms |
23836 KB |
Output is correct |
5 |
Correct |
15 ms |
23836 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23752 KB |
Output is correct |
10 |
Correct |
12 ms |
23772 KB |
Output is correct |
11 |
Correct |
13 ms |
23812 KB |
Output is correct |
12 |
Correct |
14 ms |
23812 KB |
Output is correct |
13 |
Correct |
13 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23768 KB |
Output is correct |
15 |
Correct |
13 ms |
23764 KB |
Output is correct |
16 |
Correct |
13 ms |
23724 KB |
Output is correct |
17 |
Correct |
13 ms |
23892 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23744 KB |
Output is correct |
20 |
Correct |
13 ms |
23788 KB |
Output is correct |
21 |
Correct |
12 ms |
23812 KB |
Output is correct |
22 |
Correct |
12 ms |
23764 KB |
Output is correct |
23 |
Correct |
13 ms |
23816 KB |
Output is correct |
24 |
Correct |
15 ms |
23820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23816 KB |
Output is correct |
3 |
Correct |
12 ms |
23852 KB |
Output is correct |
4 |
Correct |
12 ms |
23836 KB |
Output is correct |
5 |
Correct |
15 ms |
23836 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23752 KB |
Output is correct |
10 |
Correct |
12 ms |
23772 KB |
Output is correct |
11 |
Correct |
13 ms |
23812 KB |
Output is correct |
12 |
Correct |
14 ms |
23812 KB |
Output is correct |
13 |
Correct |
13 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23768 KB |
Output is correct |
15 |
Correct |
13 ms |
23764 KB |
Output is correct |
16 |
Correct |
13 ms |
23724 KB |
Output is correct |
17 |
Correct |
13 ms |
23892 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23744 KB |
Output is correct |
20 |
Correct |
13 ms |
23788 KB |
Output is correct |
21 |
Correct |
12 ms |
23812 KB |
Output is correct |
22 |
Correct |
12 ms |
23764 KB |
Output is correct |
23 |
Correct |
13 ms |
23816 KB |
Output is correct |
24 |
Correct |
15 ms |
23820 KB |
Output is correct |
25 |
Correct |
12 ms |
23812 KB |
Output is correct |
26 |
Correct |
17 ms |
24296 KB |
Output is correct |
27 |
Correct |
14 ms |
24240 KB |
Output is correct |
28 |
Correct |
14 ms |
24340 KB |
Output is correct |
29 |
Correct |
15 ms |
24276 KB |
Output is correct |
30 |
Correct |
15 ms |
24276 KB |
Output is correct |
31 |
Correct |
14 ms |
23764 KB |
Output is correct |
32 |
Correct |
14 ms |
24404 KB |
Output is correct |
33 |
Correct |
14 ms |
23764 KB |
Output is correct |
34 |
Correct |
14 ms |
24276 KB |
Output is correct |
35 |
Correct |
14 ms |
24344 KB |
Output is correct |
36 |
Correct |
14 ms |
24212 KB |
Output is correct |
37 |
Correct |
15 ms |
24276 KB |
Output is correct |
38 |
Correct |
13 ms |
23816 KB |
Output is correct |
39 |
Correct |
17 ms |
24212 KB |
Output is correct |
40 |
Correct |
15 ms |
24216 KB |
Output is correct |
41 |
Correct |
17 ms |
24276 KB |
Output is correct |
42 |
Correct |
14 ms |
24276 KB |
Output is correct |
43 |
Correct |
15 ms |
24348 KB |
Output is correct |
44 |
Correct |
14 ms |
23764 KB |
Output is correct |
45 |
Correct |
15 ms |
24276 KB |
Output is correct |
46 |
Correct |
15 ms |
24168 KB |
Output is correct |
47 |
Correct |
13 ms |
24020 KB |
Output is correct |
48 |
Correct |
14 ms |
24148 KB |
Output is correct |
49 |
Correct |
15 ms |
24300 KB |
Output is correct |
50 |
Correct |
15 ms |
24276 KB |
Output is correct |
51 |
Correct |
16 ms |
24224 KB |
Output is correct |
52 |
Correct |
16 ms |
24276 KB |
Output is correct |
53 |
Correct |
15 ms |
24276 KB |
Output is correct |
54 |
Correct |
15 ms |
24208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23816 KB |
Output is correct |
3 |
Correct |
12 ms |
23852 KB |
Output is correct |
4 |
Correct |
12 ms |
23836 KB |
Output is correct |
5 |
Correct |
15 ms |
23836 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23752 KB |
Output is correct |
10 |
Correct |
12 ms |
23772 KB |
Output is correct |
11 |
Correct |
13 ms |
23812 KB |
Output is correct |
12 |
Correct |
14 ms |
23812 KB |
Output is correct |
13 |
Correct |
13 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23768 KB |
Output is correct |
15 |
Correct |
13 ms |
23764 KB |
Output is correct |
16 |
Correct |
13 ms |
23724 KB |
Output is correct |
17 |
Correct |
13 ms |
23892 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23744 KB |
Output is correct |
20 |
Correct |
13 ms |
23788 KB |
Output is correct |
21 |
Correct |
12 ms |
23812 KB |
Output is correct |
22 |
Correct |
12 ms |
23764 KB |
Output is correct |
23 |
Correct |
13 ms |
23816 KB |
Output is correct |
24 |
Correct |
15 ms |
23820 KB |
Output is correct |
25 |
Correct |
12 ms |
23764 KB |
Output is correct |
26 |
Correct |
72 ms |
38376 KB |
Output is correct |
27 |
Correct |
95 ms |
38020 KB |
Output is correct |
28 |
Correct |
15 ms |
24192 KB |
Output is correct |
29 |
Correct |
13 ms |
23840 KB |
Output is correct |
30 |
Correct |
13 ms |
23812 KB |
Output is correct |
31 |
Correct |
139 ms |
37980 KB |
Output is correct |
32 |
Correct |
14 ms |
24272 KB |
Output is correct |
33 |
Correct |
114 ms |
41420 KB |
Output is correct |
34 |
Correct |
92 ms |
37972 KB |
Output is correct |
35 |
Correct |
14 ms |
24152 KB |
Output is correct |
36 |
Correct |
115 ms |
38212 KB |
Output is correct |
37 |
Correct |
14 ms |
24212 KB |
Output is correct |
38 |
Correct |
17 ms |
24260 KB |
Output is correct |
39 |
Correct |
87 ms |
38276 KB |
Output is correct |
40 |
Correct |
15 ms |
24384 KB |
Output is correct |
41 |
Correct |
107 ms |
37944 KB |
Output is correct |
42 |
Correct |
119 ms |
39096 KB |
Output is correct |
43 |
Correct |
12 ms |
23764 KB |
Output is correct |
44 |
Correct |
111 ms |
41404 KB |
Output is correct |
45 |
Correct |
124 ms |
39784 KB |
Output is correct |
46 |
Correct |
14 ms |
24212 KB |
Output is correct |
47 |
Correct |
15 ms |
24204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
36928 KB |
Output is correct |
2 |
Correct |
108 ms |
39480 KB |
Output is correct |
3 |
Correct |
16 ms |
24148 KB |
Output is correct |
4 |
Correct |
16 ms |
24204 KB |
Output is correct |
5 |
Correct |
15 ms |
23740 KB |
Output is correct |
6 |
Correct |
12 ms |
23740 KB |
Output is correct |
7 |
Correct |
14 ms |
24276 KB |
Output is correct |
8 |
Correct |
116 ms |
39344 KB |
Output is correct |
9 |
Correct |
14 ms |
24148 KB |
Output is correct |
10 |
Correct |
105 ms |
38160 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
130 ms |
38088 KB |
Output is correct |
13 |
Correct |
114 ms |
39164 KB |
Output is correct |
14 |
Correct |
120 ms |
40852 KB |
Output is correct |
15 |
Correct |
75 ms |
38272 KB |
Output is correct |
16 |
Correct |
13 ms |
24276 KB |
Output is correct |
17 |
Correct |
14 ms |
23848 KB |
Output is correct |
18 |
Correct |
120 ms |
40836 KB |
Output is correct |
19 |
Correct |
162 ms |
42792 KB |
Output is correct |
20 |
Correct |
15 ms |
24204 KB |
Output is correct |
21 |
Correct |
13 ms |
23856 KB |
Output is correct |
22 |
Correct |
89 ms |
39556 KB |
Output is correct |
23 |
Correct |
14 ms |
24212 KB |
Output is correct |
24 |
Correct |
114 ms |
38508 KB |
Output is correct |
25 |
Correct |
154 ms |
42152 KB |
Output is correct |
26 |
Correct |
15 ms |
24280 KB |
Output is correct |
27 |
Correct |
14 ms |
24288 KB |
Output is correct |
28 |
Correct |
14 ms |
24208 KB |
Output is correct |
29 |
Correct |
14 ms |
24212 KB |
Output is correct |
30 |
Correct |
15 ms |
24276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23816 KB |
Output is correct |
3 |
Correct |
12 ms |
23852 KB |
Output is correct |
4 |
Correct |
12 ms |
23836 KB |
Output is correct |
5 |
Correct |
15 ms |
23836 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23752 KB |
Output is correct |
10 |
Correct |
12 ms |
23772 KB |
Output is correct |
11 |
Correct |
13 ms |
23812 KB |
Output is correct |
12 |
Correct |
14 ms |
23812 KB |
Output is correct |
13 |
Correct |
13 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23768 KB |
Output is correct |
15 |
Correct |
13 ms |
23764 KB |
Output is correct |
16 |
Correct |
13 ms |
23724 KB |
Output is correct |
17 |
Correct |
13 ms |
23892 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23744 KB |
Output is correct |
20 |
Correct |
13 ms |
23788 KB |
Output is correct |
21 |
Correct |
12 ms |
23812 KB |
Output is correct |
22 |
Correct |
12 ms |
23764 KB |
Output is correct |
23 |
Correct |
13 ms |
23816 KB |
Output is correct |
24 |
Correct |
15 ms |
23820 KB |
Output is correct |
25 |
Correct |
12 ms |
23812 KB |
Output is correct |
26 |
Correct |
17 ms |
24296 KB |
Output is correct |
27 |
Correct |
14 ms |
24240 KB |
Output is correct |
28 |
Correct |
14 ms |
24340 KB |
Output is correct |
29 |
Correct |
15 ms |
24276 KB |
Output is correct |
30 |
Correct |
15 ms |
24276 KB |
Output is correct |
31 |
Correct |
14 ms |
23764 KB |
Output is correct |
32 |
Correct |
14 ms |
24404 KB |
Output is correct |
33 |
Correct |
14 ms |
23764 KB |
Output is correct |
34 |
Correct |
14 ms |
24276 KB |
Output is correct |
35 |
Correct |
14 ms |
24344 KB |
Output is correct |
36 |
Correct |
14 ms |
24212 KB |
Output is correct |
37 |
Correct |
15 ms |
24276 KB |
Output is correct |
38 |
Correct |
13 ms |
23816 KB |
Output is correct |
39 |
Correct |
17 ms |
24212 KB |
Output is correct |
40 |
Correct |
15 ms |
24216 KB |
Output is correct |
41 |
Correct |
17 ms |
24276 KB |
Output is correct |
42 |
Correct |
14 ms |
24276 KB |
Output is correct |
43 |
Correct |
15 ms |
24348 KB |
Output is correct |
44 |
Correct |
14 ms |
23764 KB |
Output is correct |
45 |
Correct |
15 ms |
24276 KB |
Output is correct |
46 |
Correct |
15 ms |
24168 KB |
Output is correct |
47 |
Correct |
13 ms |
24020 KB |
Output is correct |
48 |
Correct |
14 ms |
24148 KB |
Output is correct |
49 |
Correct |
15 ms |
24300 KB |
Output is correct |
50 |
Correct |
15 ms |
24276 KB |
Output is correct |
51 |
Correct |
16 ms |
24224 KB |
Output is correct |
52 |
Correct |
16 ms |
24276 KB |
Output is correct |
53 |
Correct |
15 ms |
24276 KB |
Output is correct |
54 |
Correct |
15 ms |
24208 KB |
Output is correct |
55 |
Correct |
12 ms |
23764 KB |
Output is correct |
56 |
Correct |
72 ms |
38376 KB |
Output is correct |
57 |
Correct |
95 ms |
38020 KB |
Output is correct |
58 |
Correct |
15 ms |
24192 KB |
Output is correct |
59 |
Correct |
13 ms |
23840 KB |
Output is correct |
60 |
Correct |
13 ms |
23812 KB |
Output is correct |
61 |
Correct |
139 ms |
37980 KB |
Output is correct |
62 |
Correct |
14 ms |
24272 KB |
Output is correct |
63 |
Correct |
114 ms |
41420 KB |
Output is correct |
64 |
Correct |
92 ms |
37972 KB |
Output is correct |
65 |
Correct |
14 ms |
24152 KB |
Output is correct |
66 |
Correct |
115 ms |
38212 KB |
Output is correct |
67 |
Correct |
14 ms |
24212 KB |
Output is correct |
68 |
Correct |
17 ms |
24260 KB |
Output is correct |
69 |
Correct |
87 ms |
38276 KB |
Output is correct |
70 |
Correct |
15 ms |
24384 KB |
Output is correct |
71 |
Correct |
107 ms |
37944 KB |
Output is correct |
72 |
Correct |
119 ms |
39096 KB |
Output is correct |
73 |
Correct |
12 ms |
23764 KB |
Output is correct |
74 |
Correct |
111 ms |
41404 KB |
Output is correct |
75 |
Correct |
124 ms |
39784 KB |
Output is correct |
76 |
Correct |
14 ms |
24212 KB |
Output is correct |
77 |
Correct |
15 ms |
24204 KB |
Output is correct |
78 |
Correct |
74 ms |
36928 KB |
Output is correct |
79 |
Correct |
108 ms |
39480 KB |
Output is correct |
80 |
Correct |
16 ms |
24148 KB |
Output is correct |
81 |
Correct |
16 ms |
24204 KB |
Output is correct |
82 |
Correct |
15 ms |
23740 KB |
Output is correct |
83 |
Correct |
12 ms |
23740 KB |
Output is correct |
84 |
Correct |
14 ms |
24276 KB |
Output is correct |
85 |
Correct |
116 ms |
39344 KB |
Output is correct |
86 |
Correct |
14 ms |
24148 KB |
Output is correct |
87 |
Correct |
105 ms |
38160 KB |
Output is correct |
88 |
Correct |
12 ms |
23764 KB |
Output is correct |
89 |
Correct |
130 ms |
38088 KB |
Output is correct |
90 |
Correct |
114 ms |
39164 KB |
Output is correct |
91 |
Correct |
120 ms |
40852 KB |
Output is correct |
92 |
Correct |
75 ms |
38272 KB |
Output is correct |
93 |
Correct |
13 ms |
24276 KB |
Output is correct |
94 |
Correct |
14 ms |
23848 KB |
Output is correct |
95 |
Correct |
120 ms |
40836 KB |
Output is correct |
96 |
Correct |
162 ms |
42792 KB |
Output is correct |
97 |
Correct |
15 ms |
24204 KB |
Output is correct |
98 |
Correct |
13 ms |
23856 KB |
Output is correct |
99 |
Correct |
89 ms |
39556 KB |
Output is correct |
100 |
Correct |
14 ms |
24212 KB |
Output is correct |
101 |
Correct |
114 ms |
38508 KB |
Output is correct |
102 |
Correct |
154 ms |
42152 KB |
Output is correct |
103 |
Correct |
15 ms |
24280 KB |
Output is correct |
104 |
Correct |
14 ms |
24288 KB |
Output is correct |
105 |
Correct |
14 ms |
24208 KB |
Output is correct |
106 |
Correct |
14 ms |
24212 KB |
Output is correct |
107 |
Correct |
15 ms |
24276 KB |
Output is correct |
108 |
Correct |
574 ms |
102804 KB |
Output is correct |
109 |
Correct |
872 ms |
103332 KB |
Output is correct |
110 |
Correct |
802 ms |
108612 KB |
Output is correct |
111 |
Correct |
1104 ms |
121616 KB |
Output is correct |
112 |
Correct |
1094 ms |
116788 KB |
Output is correct |
113 |
Correct |
631 ms |
110572 KB |
Output is correct |
114 |
Correct |
616 ms |
94820 KB |
Output is correct |
115 |
Correct |
619 ms |
94836 KB |
Output is correct |
116 |
Correct |
760 ms |
99380 KB |
Output is correct |
117 |
Correct |
859 ms |
110584 KB |
Output is correct |
118 |
Correct |
625 ms |
95312 KB |
Output is correct |
119 |
Correct |
903 ms |
110612 KB |
Output is correct |
120 |
Correct |
1085 ms |
117256 KB |
Output is correct |
121 |
Correct |
903 ms |
110844 KB |
Output is correct |
122 |
Correct |
869 ms |
104436 KB |
Output is correct |
123 |
Correct |
626 ms |
112044 KB |
Output is correct |
124 |
Correct |
1047 ms |
108628 KB |
Output is correct |