#pragma GCC optimize ("O3")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define F first
#define S second
using ll = long long;
using ii = pair<int, int>;
const int N = 5e5 + 5, mod = 1e9 + 7;
int n, k;
std::vector<int> adj[N];
int in[N], out[N], ord[N], lvl[N], sz[N], timer;
int cnt[N];
int cur[N];
int col[N];
int freq[N];
void dfs0(int u){
sz[u] = 1;
in[u] = timer;
for(auto v : adj[u]){
lvl[v] = lvl[u] + 1;
adj[v].erase(find(adj[v].begin(), adj[v].end(), u));
dfs0(v);
sz[u] += sz[v];
}
ord[timer] = u;
out[u] = timer++;
}
bool f;
int ans[N];
bool isFaulty[N];
int dfs1(int u, bool keep) {
// cout << u << endl;
int mx = -1, bigChild = -1;
for(auto v : adj[u])if(sz[v] > mx){
mx = sz[v], bigChild = v;
}
if(bigChild == -1 && u != 1){
cur[u] = k - 1;
cnt[col[u]]++;
cur[u] += (cnt[col[u]] == freq[col[u]]);
int ret = cur[u] == k;
// cout u << ' ' << ret << endl;
// cout << freq[col[u]] << ' ' << cnt[col[u]] << ' ' << cur[u] << ' ' << ret << '\n';
if(!keep){
cur[u] -= (cnt[col[u]] == freq[col[u]]);
cnt[col[u]]--;
cur[u] += (cnt[col[u]] == 0);
}
// cout << u << ' ' << ret << '\n';
isFaulty[u] = ret;
return ans[u] = ret;
}
int ret = 0;
for(auto v : adj[u])if(v != bigChild){
ret += dfs1(v, 0);
}
// cout << u << ' ' << ret << '\n';
ret += dfs1(bigChild, 1);
cur[u] = cur[bigChild];
for(auto v : adj[u])
if(v != bigChild){
for(int x = in[v]; x <= out[v]; x++){
cur[u] -= (cnt[col[ord[x]]] == 0);
cnt[col[ord[x]]]++;
cur[u] += (cnt[col[ord[x]]] == freq[col[ord[x]]]);
}
}
cur[u] -= (cnt[col[u]] == 0);
cnt[col[u]]++;
cur[u] += (cnt[col[u]] == freq[col[u]]);
if(!ret && u != 1){
ret = cur[u] == k;
}
isFaulty[u] = (cur[u] == k);
if(!keep){
for(int x = in[u]; x <= out[u]; x++){
cur[u] -= (cnt[col[ord[x]]] == freq[col[ord[x]]]);
cnt[col[ord[x]]]--;
cur[u] += (cnt[col[ord[x]]] == 0);
}
}
return ans[u] = ret;
}
int main(){
cin >> n >> k;
for(int i = 0, u, v; i < n - 1; i++){
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
for(int i = 1; i <= n; i++){
cin >> col[i];
freq[col[i]]++;
}
dfs0(1);
int out = dfs1(1, 0) + 1;
// cout << out << '\n';
if(min(n,k) == 1){
cout << 0 << endl;
return 0;
}
for(int i = 2; i <= n; i++)
if(isFaulty[i]){
int tot = 0;
for(auto v : adj[i])tot += ans[v];
if(tot == out - 1){
out++;
break;
}
}
cout << out / 2 << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
14 ms |
12072 KB |
Output is correct |
3 |
Correct |
14 ms |
12152 KB |
Output is correct |
4 |
Correct |
14 ms |
12152 KB |
Output is correct |
5 |
Correct |
14 ms |
12152 KB |
Output is correct |
6 |
Correct |
16 ms |
12152 KB |
Output is correct |
7 |
Correct |
16 ms |
12152 KB |
Output is correct |
8 |
Correct |
14 ms |
12152 KB |
Output is correct |
9 |
Correct |
14 ms |
12152 KB |
Output is correct |
10 |
Correct |
14 ms |
12152 KB |
Output is correct |
11 |
Correct |
14 ms |
12152 KB |
Output is correct |
12 |
Correct |
14 ms |
12152 KB |
Output is correct |
13 |
Correct |
14 ms |
12152 KB |
Output is correct |
14 |
Correct |
14 ms |
12148 KB |
Output is correct |
15 |
Correct |
14 ms |
12152 KB |
Output is correct |
16 |
Correct |
13 ms |
12152 KB |
Output is correct |
17 |
Correct |
16 ms |
12152 KB |
Output is correct |
18 |
Correct |
18 ms |
12152 KB |
Output is correct |
19 |
Correct |
14 ms |
12152 KB |
Output is correct |
20 |
Correct |
14 ms |
12152 KB |
Output is correct |
21 |
Correct |
14 ms |
12152 KB |
Output is correct |
22 |
Correct |
14 ms |
12280 KB |
Output is correct |
23 |
Correct |
14 ms |
12188 KB |
Output is correct |
24 |
Correct |
13 ms |
12152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
14 ms |
12072 KB |
Output is correct |
3 |
Correct |
14 ms |
12152 KB |
Output is correct |
4 |
Correct |
14 ms |
12152 KB |
Output is correct |
5 |
Correct |
14 ms |
12152 KB |
Output is correct |
6 |
Correct |
16 ms |
12152 KB |
Output is correct |
7 |
Correct |
16 ms |
12152 KB |
Output is correct |
8 |
Correct |
14 ms |
12152 KB |
Output is correct |
9 |
Correct |
14 ms |
12152 KB |
Output is correct |
10 |
Correct |
14 ms |
12152 KB |
Output is correct |
11 |
Correct |
14 ms |
12152 KB |
Output is correct |
12 |
Correct |
14 ms |
12152 KB |
Output is correct |
13 |
Correct |
14 ms |
12152 KB |
Output is correct |
14 |
Correct |
14 ms |
12148 KB |
Output is correct |
15 |
Correct |
14 ms |
12152 KB |
Output is correct |
16 |
Correct |
13 ms |
12152 KB |
Output is correct |
17 |
Correct |
16 ms |
12152 KB |
Output is correct |
18 |
Correct |
18 ms |
12152 KB |
Output is correct |
19 |
Correct |
14 ms |
12152 KB |
Output is correct |
20 |
Correct |
14 ms |
12152 KB |
Output is correct |
21 |
Correct |
14 ms |
12152 KB |
Output is correct |
22 |
Correct |
14 ms |
12280 KB |
Output is correct |
23 |
Correct |
14 ms |
12188 KB |
Output is correct |
24 |
Correct |
13 ms |
12152 KB |
Output is correct |
25 |
Correct |
14 ms |
12156 KB |
Output is correct |
26 |
Correct |
19 ms |
12408 KB |
Output is correct |
27 |
Correct |
18 ms |
12408 KB |
Output is correct |
28 |
Correct |
19 ms |
12536 KB |
Output is correct |
29 |
Correct |
19 ms |
12408 KB |
Output is correct |
30 |
Correct |
19 ms |
12408 KB |
Output is correct |
31 |
Correct |
14 ms |
12152 KB |
Output is correct |
32 |
Correct |
19 ms |
12664 KB |
Output is correct |
33 |
Correct |
14 ms |
12152 KB |
Output is correct |
34 |
Correct |
18 ms |
12408 KB |
Output is correct |
35 |
Correct |
19 ms |
12380 KB |
Output is correct |
36 |
Correct |
18 ms |
12408 KB |
Output is correct |
37 |
Correct |
19 ms |
12420 KB |
Output is correct |
38 |
Correct |
14 ms |
12152 KB |
Output is correct |
39 |
Correct |
19 ms |
12408 KB |
Output is correct |
40 |
Correct |
18 ms |
12408 KB |
Output is correct |
41 |
Correct |
18 ms |
12408 KB |
Output is correct |
42 |
Correct |
19 ms |
12536 KB |
Output is correct |
43 |
Correct |
18 ms |
12664 KB |
Output is correct |
44 |
Correct |
14 ms |
12152 KB |
Output is correct |
45 |
Correct |
19 ms |
12536 KB |
Output is correct |
46 |
Correct |
19 ms |
12452 KB |
Output is correct |
47 |
Correct |
14 ms |
12152 KB |
Output is correct |
48 |
Correct |
19 ms |
12408 KB |
Output is correct |
49 |
Correct |
18 ms |
12412 KB |
Output is correct |
50 |
Correct |
19 ms |
12536 KB |
Output is correct |
51 |
Correct |
19 ms |
12408 KB |
Output is correct |
52 |
Correct |
18 ms |
12408 KB |
Output is correct |
53 |
Correct |
19 ms |
12408 KB |
Output is correct |
54 |
Correct |
18 ms |
12380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
14 ms |
12072 KB |
Output is correct |
3 |
Correct |
14 ms |
12152 KB |
Output is correct |
4 |
Correct |
14 ms |
12152 KB |
Output is correct |
5 |
Correct |
14 ms |
12152 KB |
Output is correct |
6 |
Correct |
16 ms |
12152 KB |
Output is correct |
7 |
Correct |
16 ms |
12152 KB |
Output is correct |
8 |
Correct |
14 ms |
12152 KB |
Output is correct |
9 |
Correct |
14 ms |
12152 KB |
Output is correct |
10 |
Correct |
14 ms |
12152 KB |
Output is correct |
11 |
Correct |
14 ms |
12152 KB |
Output is correct |
12 |
Correct |
14 ms |
12152 KB |
Output is correct |
13 |
Correct |
14 ms |
12152 KB |
Output is correct |
14 |
Correct |
14 ms |
12148 KB |
Output is correct |
15 |
Correct |
14 ms |
12152 KB |
Output is correct |
16 |
Correct |
13 ms |
12152 KB |
Output is correct |
17 |
Correct |
16 ms |
12152 KB |
Output is correct |
18 |
Correct |
18 ms |
12152 KB |
Output is correct |
19 |
Correct |
14 ms |
12152 KB |
Output is correct |
20 |
Correct |
14 ms |
12152 KB |
Output is correct |
21 |
Correct |
14 ms |
12152 KB |
Output is correct |
22 |
Correct |
14 ms |
12280 KB |
Output is correct |
23 |
Correct |
14 ms |
12188 KB |
Output is correct |
24 |
Correct |
13 ms |
12152 KB |
Output is correct |
25 |
Correct |
14 ms |
12152 KB |
Output is correct |
26 |
Correct |
204 ms |
20368 KB |
Output is correct |
27 |
Correct |
225 ms |
20188 KB |
Output is correct |
28 |
Correct |
18 ms |
12408 KB |
Output is correct |
29 |
Correct |
16 ms |
12152 KB |
Output is correct |
30 |
Correct |
14 ms |
12152 KB |
Output is correct |
31 |
Correct |
232 ms |
20088 KB |
Output is correct |
32 |
Correct |
17 ms |
12304 KB |
Output is correct |
33 |
Correct |
235 ms |
28700 KB |
Output is correct |
34 |
Correct |
215 ms |
20168 KB |
Output is correct |
35 |
Correct |
19 ms |
12412 KB |
Output is correct |
36 |
Correct |
229 ms |
20988 KB |
Output is correct |
37 |
Correct |
18 ms |
12408 KB |
Output is correct |
38 |
Correct |
18 ms |
12408 KB |
Output is correct |
39 |
Correct |
186 ms |
20332 KB |
Output is correct |
40 |
Correct |
21 ms |
12668 KB |
Output is correct |
41 |
Correct |
237 ms |
20084 KB |
Output is correct |
42 |
Correct |
219 ms |
22904 KB |
Output is correct |
43 |
Correct |
14 ms |
12152 KB |
Output is correct |
44 |
Correct |
236 ms |
29020 KB |
Output is correct |
45 |
Correct |
222 ms |
24488 KB |
Output is correct |
46 |
Correct |
18 ms |
12408 KB |
Output is correct |
47 |
Correct |
18 ms |
12408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
186 ms |
18928 KB |
Output is correct |
2 |
Correct |
210 ms |
21460 KB |
Output is correct |
3 |
Correct |
19 ms |
12408 KB |
Output is correct |
4 |
Correct |
18 ms |
12408 KB |
Output is correct |
5 |
Correct |
13 ms |
12152 KB |
Output is correct |
6 |
Correct |
14 ms |
12152 KB |
Output is correct |
7 |
Correct |
18 ms |
12408 KB |
Output is correct |
8 |
Correct |
240 ms |
20728 KB |
Output is correct |
9 |
Correct |
18 ms |
12408 KB |
Output is correct |
10 |
Correct |
215 ms |
20216 KB |
Output is correct |
11 |
Correct |
14 ms |
12152 KB |
Output is correct |
12 |
Correct |
232 ms |
20352 KB |
Output is correct |
13 |
Correct |
242 ms |
20728 KB |
Output is correct |
14 |
Correct |
242 ms |
21244 KB |
Output is correct |
15 |
Correct |
183 ms |
20464 KB |
Output is correct |
16 |
Correct |
21 ms |
12408 KB |
Output is correct |
17 |
Correct |
16 ms |
12152 KB |
Output is correct |
18 |
Correct |
223 ms |
21300 KB |
Output is correct |
19 |
Correct |
257 ms |
25848 KB |
Output is correct |
20 |
Correct |
21 ms |
12408 KB |
Output is correct |
21 |
Correct |
26 ms |
12152 KB |
Output is correct |
22 |
Correct |
235 ms |
21008 KB |
Output is correct |
23 |
Correct |
18 ms |
12408 KB |
Output is correct |
24 |
Correct |
237 ms |
20216 KB |
Output is correct |
25 |
Correct |
240 ms |
24252 KB |
Output is correct |
26 |
Correct |
19 ms |
12408 KB |
Output is correct |
27 |
Correct |
19 ms |
12536 KB |
Output is correct |
28 |
Correct |
18 ms |
12408 KB |
Output is correct |
29 |
Correct |
18 ms |
12408 KB |
Output is correct |
30 |
Correct |
18 ms |
12408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
14 ms |
12072 KB |
Output is correct |
3 |
Correct |
14 ms |
12152 KB |
Output is correct |
4 |
Correct |
14 ms |
12152 KB |
Output is correct |
5 |
Correct |
14 ms |
12152 KB |
Output is correct |
6 |
Correct |
16 ms |
12152 KB |
Output is correct |
7 |
Correct |
16 ms |
12152 KB |
Output is correct |
8 |
Correct |
14 ms |
12152 KB |
Output is correct |
9 |
Correct |
14 ms |
12152 KB |
Output is correct |
10 |
Correct |
14 ms |
12152 KB |
Output is correct |
11 |
Correct |
14 ms |
12152 KB |
Output is correct |
12 |
Correct |
14 ms |
12152 KB |
Output is correct |
13 |
Correct |
14 ms |
12152 KB |
Output is correct |
14 |
Correct |
14 ms |
12148 KB |
Output is correct |
15 |
Correct |
14 ms |
12152 KB |
Output is correct |
16 |
Correct |
13 ms |
12152 KB |
Output is correct |
17 |
Correct |
16 ms |
12152 KB |
Output is correct |
18 |
Correct |
18 ms |
12152 KB |
Output is correct |
19 |
Correct |
14 ms |
12152 KB |
Output is correct |
20 |
Correct |
14 ms |
12152 KB |
Output is correct |
21 |
Correct |
14 ms |
12152 KB |
Output is correct |
22 |
Correct |
14 ms |
12280 KB |
Output is correct |
23 |
Correct |
14 ms |
12188 KB |
Output is correct |
24 |
Correct |
13 ms |
12152 KB |
Output is correct |
25 |
Correct |
14 ms |
12156 KB |
Output is correct |
26 |
Correct |
19 ms |
12408 KB |
Output is correct |
27 |
Correct |
18 ms |
12408 KB |
Output is correct |
28 |
Correct |
19 ms |
12536 KB |
Output is correct |
29 |
Correct |
19 ms |
12408 KB |
Output is correct |
30 |
Correct |
19 ms |
12408 KB |
Output is correct |
31 |
Correct |
14 ms |
12152 KB |
Output is correct |
32 |
Correct |
19 ms |
12664 KB |
Output is correct |
33 |
Correct |
14 ms |
12152 KB |
Output is correct |
34 |
Correct |
18 ms |
12408 KB |
Output is correct |
35 |
Correct |
19 ms |
12380 KB |
Output is correct |
36 |
Correct |
18 ms |
12408 KB |
Output is correct |
37 |
Correct |
19 ms |
12420 KB |
Output is correct |
38 |
Correct |
14 ms |
12152 KB |
Output is correct |
39 |
Correct |
19 ms |
12408 KB |
Output is correct |
40 |
Correct |
18 ms |
12408 KB |
Output is correct |
41 |
Correct |
18 ms |
12408 KB |
Output is correct |
42 |
Correct |
19 ms |
12536 KB |
Output is correct |
43 |
Correct |
18 ms |
12664 KB |
Output is correct |
44 |
Correct |
14 ms |
12152 KB |
Output is correct |
45 |
Correct |
19 ms |
12536 KB |
Output is correct |
46 |
Correct |
19 ms |
12452 KB |
Output is correct |
47 |
Correct |
14 ms |
12152 KB |
Output is correct |
48 |
Correct |
19 ms |
12408 KB |
Output is correct |
49 |
Correct |
18 ms |
12412 KB |
Output is correct |
50 |
Correct |
19 ms |
12536 KB |
Output is correct |
51 |
Correct |
19 ms |
12408 KB |
Output is correct |
52 |
Correct |
18 ms |
12408 KB |
Output is correct |
53 |
Correct |
19 ms |
12408 KB |
Output is correct |
54 |
Correct |
18 ms |
12380 KB |
Output is correct |
55 |
Correct |
14 ms |
12152 KB |
Output is correct |
56 |
Correct |
204 ms |
20368 KB |
Output is correct |
57 |
Correct |
225 ms |
20188 KB |
Output is correct |
58 |
Correct |
18 ms |
12408 KB |
Output is correct |
59 |
Correct |
16 ms |
12152 KB |
Output is correct |
60 |
Correct |
14 ms |
12152 KB |
Output is correct |
61 |
Correct |
232 ms |
20088 KB |
Output is correct |
62 |
Correct |
17 ms |
12304 KB |
Output is correct |
63 |
Correct |
235 ms |
28700 KB |
Output is correct |
64 |
Correct |
215 ms |
20168 KB |
Output is correct |
65 |
Correct |
19 ms |
12412 KB |
Output is correct |
66 |
Correct |
229 ms |
20988 KB |
Output is correct |
67 |
Correct |
18 ms |
12408 KB |
Output is correct |
68 |
Correct |
18 ms |
12408 KB |
Output is correct |
69 |
Correct |
186 ms |
20332 KB |
Output is correct |
70 |
Correct |
21 ms |
12668 KB |
Output is correct |
71 |
Correct |
237 ms |
20084 KB |
Output is correct |
72 |
Correct |
219 ms |
22904 KB |
Output is correct |
73 |
Correct |
14 ms |
12152 KB |
Output is correct |
74 |
Correct |
236 ms |
29020 KB |
Output is correct |
75 |
Correct |
222 ms |
24488 KB |
Output is correct |
76 |
Correct |
18 ms |
12408 KB |
Output is correct |
77 |
Correct |
18 ms |
12408 KB |
Output is correct |
78 |
Correct |
186 ms |
18928 KB |
Output is correct |
79 |
Correct |
210 ms |
21460 KB |
Output is correct |
80 |
Correct |
19 ms |
12408 KB |
Output is correct |
81 |
Correct |
18 ms |
12408 KB |
Output is correct |
82 |
Correct |
13 ms |
12152 KB |
Output is correct |
83 |
Correct |
14 ms |
12152 KB |
Output is correct |
84 |
Correct |
18 ms |
12408 KB |
Output is correct |
85 |
Correct |
240 ms |
20728 KB |
Output is correct |
86 |
Correct |
18 ms |
12408 KB |
Output is correct |
87 |
Correct |
215 ms |
20216 KB |
Output is correct |
88 |
Correct |
14 ms |
12152 KB |
Output is correct |
89 |
Correct |
232 ms |
20352 KB |
Output is correct |
90 |
Correct |
242 ms |
20728 KB |
Output is correct |
91 |
Correct |
242 ms |
21244 KB |
Output is correct |
92 |
Correct |
183 ms |
20464 KB |
Output is correct |
93 |
Correct |
21 ms |
12408 KB |
Output is correct |
94 |
Correct |
16 ms |
12152 KB |
Output is correct |
95 |
Correct |
223 ms |
21300 KB |
Output is correct |
96 |
Correct |
257 ms |
25848 KB |
Output is correct |
97 |
Correct |
21 ms |
12408 KB |
Output is correct |
98 |
Correct |
26 ms |
12152 KB |
Output is correct |
99 |
Correct |
235 ms |
21008 KB |
Output is correct |
100 |
Correct |
18 ms |
12408 KB |
Output is correct |
101 |
Correct |
237 ms |
20216 KB |
Output is correct |
102 |
Correct |
240 ms |
24252 KB |
Output is correct |
103 |
Correct |
19 ms |
12408 KB |
Output is correct |
104 |
Correct |
19 ms |
12536 KB |
Output is correct |
105 |
Correct |
18 ms |
12408 KB |
Output is correct |
106 |
Correct |
18 ms |
12408 KB |
Output is correct |
107 |
Correct |
18 ms |
12408 KB |
Output is correct |
108 |
Correct |
1222 ms |
49952 KB |
Output is correct |
109 |
Correct |
1409 ms |
68704 KB |
Output is correct |
110 |
Correct |
1359 ms |
82244 KB |
Output is correct |
111 |
Correct |
1642 ms |
78676 KB |
Output is correct |
112 |
Correct |
1857 ms |
66900 KB |
Output is correct |
113 |
Correct |
1297 ms |
51968 KB |
Output is correct |
114 |
Correct |
1333 ms |
47140 KB |
Output is correct |
115 |
Correct |
1412 ms |
47248 KB |
Output is correct |
116 |
Correct |
1484 ms |
47736 KB |
Output is correct |
117 |
Correct |
1566 ms |
50564 KB |
Output is correct |
118 |
Correct |
1342 ms |
46448 KB |
Output is correct |
119 |
Correct |
1540 ms |
50824 KB |
Output is correct |
120 |
Correct |
1574 ms |
67388 KB |
Output is correct |
121 |
Correct |
1532 ms |
50552 KB |
Output is correct |
122 |
Correct |
1493 ms |
49400 KB |
Output is correct |
123 |
Correct |
1232 ms |
51880 KB |
Output is correct |
124 |
Correct |
1553 ms |
50072 KB |
Output is correct |