#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int n,k,rez=INF;
vector<int> con[200005];
int color[200005];
int fr[200005];
int siz[200005];
bool iss[200005];
bool hascol[200005];
bool bagat[200005];
vector<int> ofcol[200005];
vector<int> active;
int parent[200005];
void get_sizes(int nod, int par)
{
if(ofcol[color[nod]].empty()) active.push_back(color[nod]);
ofcol[color[nod]].push_back(nod);
siz[nod]=1;
bagat[color[nod]]=0;
hascol[nod]=0;
for(auto adj:con[nod])
{
if(adj!=par && !iss[adj])
{
get_sizes(adj,nod);
siz[nod]+=siz[adj];
}
}
}
int get_centroid(int nod, int par, int tot)
{
for(auto adj:con[nod])
if(adj!=par && !iss[adj] && 2*siz[adj]>tot)
return get_centroid(adj,nod,tot);
return nod;
}
void dfs_parent(int nod)
{
for(auto adj:con[nod])
{
if(!iss[adj] && adj!=parent[nod])
{
parent[adj]=nod;
dfs_parent(adj);
}
}
}
int cate;
void baga_color(int col);
void baga_nod(int nod)
{
if(hascol[nod])
return;
hascol[nod]=1;
baga_color(color[nod]);
if(parent[nod]!=-1 && !hascol[parent[nod]])
baga_nod(parent[nod]);
}
void baga_color(int col)
{
if(bagat[col])
return;
cate++;
if((int)ofcol[col].size() != fr[col])
cate=INF;
bagat[col]=1;
for(auto x:ofcol[col])
{
baga_nod(x);
}
}
void centroid_decomposition(int c)
{
get_sizes(c,-1);
c = get_centroid(c,-1,siz[c]);
if((int)ofcol[color[c]].size()==fr[color[c]])
{
parent[c]=-1;
dfs_parent(c);
cate=0;
baga_color(color[c]);
rez = min(rez, cate-1);
}
for(auto x:active)
ofcol[x].clear();
active.clear();
iss[c]=1;
for(auto adj:con[c])
if(!iss[adj])
centroid_decomposition(adj);
}
signed main()
{
cin>>n>>k;
int a,b;
for(int i=1;i<n;i++)
{
cin>>a>>b;
con[a].push_back(b);
con[b].push_back(a);
}
for(int i=1;i<=n;i++)
{
cin>>color[i];
fr[color[i]]++;
}
centroid_decomposition(1);
cout<<rez;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13144 KB |
Output is correct |
2 |
Correct |
2 ms |
13148 KB |
Output is correct |
3 |
Correct |
2 ms |
13148 KB |
Output is correct |
4 |
Correct |
2 ms |
13148 KB |
Output is correct |
5 |
Correct |
2 ms |
13148 KB |
Output is correct |
6 |
Correct |
2 ms |
13148 KB |
Output is correct |
7 |
Correct |
2 ms |
13148 KB |
Output is correct |
8 |
Correct |
2 ms |
13148 KB |
Output is correct |
9 |
Correct |
2 ms |
13148 KB |
Output is correct |
10 |
Correct |
2 ms |
13148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13144 KB |
Output is correct |
2 |
Correct |
2 ms |
13148 KB |
Output is correct |
3 |
Correct |
2 ms |
13148 KB |
Output is correct |
4 |
Correct |
2 ms |
13148 KB |
Output is correct |
5 |
Correct |
2 ms |
13148 KB |
Output is correct |
6 |
Correct |
2 ms |
13148 KB |
Output is correct |
7 |
Correct |
2 ms |
13148 KB |
Output is correct |
8 |
Correct |
2 ms |
13148 KB |
Output is correct |
9 |
Correct |
2 ms |
13148 KB |
Output is correct |
10 |
Correct |
2 ms |
13148 KB |
Output is correct |
11 |
Correct |
4 ms |
13404 KB |
Output is correct |
12 |
Correct |
4 ms |
13352 KB |
Output is correct |
13 |
Correct |
4 ms |
13404 KB |
Output is correct |
14 |
Correct |
4 ms |
13404 KB |
Output is correct |
15 |
Correct |
3 ms |
13404 KB |
Output is correct |
16 |
Correct |
4 ms |
13464 KB |
Output is correct |
17 |
Correct |
3 ms |
13404 KB |
Output is correct |
18 |
Correct |
4 ms |
13404 KB |
Output is correct |
19 |
Correct |
3 ms |
13400 KB |
Output is correct |
20 |
Correct |
4 ms |
13404 KB |
Output is correct |
21 |
Correct |
3 ms |
13404 KB |
Output is correct |
22 |
Correct |
3 ms |
13660 KB |
Output is correct |
23 |
Correct |
3 ms |
13388 KB |
Output is correct |
24 |
Correct |
4 ms |
13404 KB |
Output is correct |
25 |
Correct |
5 ms |
13356 KB |
Output is correct |
26 |
Correct |
4 ms |
13640 KB |
Output is correct |
27 |
Correct |
4 ms |
13404 KB |
Output is correct |
28 |
Correct |
3 ms |
13404 KB |
Output is correct |
29 |
Correct |
4 ms |
13404 KB |
Output is correct |
30 |
Correct |
6 ms |
13608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
249 ms |
42072 KB |
Output is correct |
2 |
Correct |
174 ms |
42620 KB |
Output is correct |
3 |
Correct |
262 ms |
42012 KB |
Output is correct |
4 |
Correct |
184 ms |
42440 KB |
Output is correct |
5 |
Correct |
246 ms |
38604 KB |
Output is correct |
6 |
Correct |
186 ms |
42444 KB |
Output is correct |
7 |
Correct |
253 ms |
39372 KB |
Output is correct |
8 |
Correct |
178 ms |
42148 KB |
Output is correct |
9 |
Correct |
336 ms |
37072 KB |
Output is correct |
10 |
Correct |
312 ms |
34508 KB |
Output is correct |
11 |
Correct |
314 ms |
37596 KB |
Output is correct |
12 |
Correct |
317 ms |
40140 KB |
Output is correct |
13 |
Correct |
321 ms |
34304 KB |
Output is correct |
14 |
Correct |
306 ms |
40400 KB |
Output is correct |
15 |
Correct |
335 ms |
40124 KB |
Output is correct |
16 |
Correct |
344 ms |
35128 KB |
Output is correct |
17 |
Correct |
330 ms |
35792 KB |
Output is correct |
18 |
Correct |
289 ms |
35788 KB |
Output is correct |
19 |
Correct |
316 ms |
39120 KB |
Output is correct |
20 |
Correct |
338 ms |
41552 KB |
Output is correct |
21 |
Correct |
2 ms |
13148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
13144 KB |
Output is correct |
2 |
Correct |
2 ms |
13148 KB |
Output is correct |
3 |
Correct |
2 ms |
13148 KB |
Output is correct |
4 |
Correct |
2 ms |
13148 KB |
Output is correct |
5 |
Correct |
2 ms |
13148 KB |
Output is correct |
6 |
Correct |
2 ms |
13148 KB |
Output is correct |
7 |
Correct |
2 ms |
13148 KB |
Output is correct |
8 |
Correct |
2 ms |
13148 KB |
Output is correct |
9 |
Correct |
2 ms |
13148 KB |
Output is correct |
10 |
Correct |
2 ms |
13148 KB |
Output is correct |
11 |
Correct |
4 ms |
13404 KB |
Output is correct |
12 |
Correct |
4 ms |
13352 KB |
Output is correct |
13 |
Correct |
4 ms |
13404 KB |
Output is correct |
14 |
Correct |
4 ms |
13404 KB |
Output is correct |
15 |
Correct |
3 ms |
13404 KB |
Output is correct |
16 |
Correct |
4 ms |
13464 KB |
Output is correct |
17 |
Correct |
3 ms |
13404 KB |
Output is correct |
18 |
Correct |
4 ms |
13404 KB |
Output is correct |
19 |
Correct |
3 ms |
13400 KB |
Output is correct |
20 |
Correct |
4 ms |
13404 KB |
Output is correct |
21 |
Correct |
3 ms |
13404 KB |
Output is correct |
22 |
Correct |
3 ms |
13660 KB |
Output is correct |
23 |
Correct |
3 ms |
13388 KB |
Output is correct |
24 |
Correct |
4 ms |
13404 KB |
Output is correct |
25 |
Correct |
5 ms |
13356 KB |
Output is correct |
26 |
Correct |
4 ms |
13640 KB |
Output is correct |
27 |
Correct |
4 ms |
13404 KB |
Output is correct |
28 |
Correct |
3 ms |
13404 KB |
Output is correct |
29 |
Correct |
4 ms |
13404 KB |
Output is correct |
30 |
Correct |
6 ms |
13608 KB |
Output is correct |
31 |
Correct |
249 ms |
42072 KB |
Output is correct |
32 |
Correct |
174 ms |
42620 KB |
Output is correct |
33 |
Correct |
262 ms |
42012 KB |
Output is correct |
34 |
Correct |
184 ms |
42440 KB |
Output is correct |
35 |
Correct |
246 ms |
38604 KB |
Output is correct |
36 |
Correct |
186 ms |
42444 KB |
Output is correct |
37 |
Correct |
253 ms |
39372 KB |
Output is correct |
38 |
Correct |
178 ms |
42148 KB |
Output is correct |
39 |
Correct |
336 ms |
37072 KB |
Output is correct |
40 |
Correct |
312 ms |
34508 KB |
Output is correct |
41 |
Correct |
314 ms |
37596 KB |
Output is correct |
42 |
Correct |
317 ms |
40140 KB |
Output is correct |
43 |
Correct |
321 ms |
34304 KB |
Output is correct |
44 |
Correct |
306 ms |
40400 KB |
Output is correct |
45 |
Correct |
335 ms |
40124 KB |
Output is correct |
46 |
Correct |
344 ms |
35128 KB |
Output is correct |
47 |
Correct |
330 ms |
35792 KB |
Output is correct |
48 |
Correct |
289 ms |
35788 KB |
Output is correct |
49 |
Correct |
316 ms |
39120 KB |
Output is correct |
50 |
Correct |
338 ms |
41552 KB |
Output is correct |
51 |
Correct |
2 ms |
13148 KB |
Output is correct |
52 |
Correct |
192 ms |
26132 KB |
Output is correct |
53 |
Correct |
225 ms |
26192 KB |
Output is correct |
54 |
Correct |
199 ms |
26192 KB |
Output is correct |
55 |
Correct |
199 ms |
26144 KB |
Output is correct |
56 |
Correct |
202 ms |
26196 KB |
Output is correct |
57 |
Correct |
208 ms |
26240 KB |
Output is correct |
58 |
Correct |
232 ms |
28872 KB |
Output is correct |
59 |
Correct |
221 ms |
29388 KB |
Output is correct |
60 |
Correct |
248 ms |
28620 KB |
Output is correct |
61 |
Correct |
240 ms |
28084 KB |
Output is correct |
62 |
Correct |
188 ms |
42508 KB |
Output is correct |
63 |
Correct |
178 ms |
42440 KB |
Output is correct |
64 |
Correct |
175 ms |
42188 KB |
Output is correct |
65 |
Correct |
186 ms |
42388 KB |
Output is correct |
66 |
Correct |
185 ms |
33864 KB |
Output is correct |
67 |
Correct |
175 ms |
33880 KB |
Output is correct |
68 |
Correct |
199 ms |
33872 KB |
Output is correct |
69 |
Correct |
176 ms |
33864 KB |
Output is correct |
70 |
Correct |
177 ms |
33608 KB |
Output is correct |
71 |
Correct |
184 ms |
33764 KB |
Output is correct |
72 |
Correct |
203 ms |
33604 KB |
Output is correct |
73 |
Correct |
172 ms |
32840 KB |
Output is correct |
74 |
Correct |
187 ms |
33896 KB |
Output is correct |
75 |
Correct |
172 ms |
33740 KB |
Output is correct |
76 |
Correct |
262 ms |
32456 KB |
Output is correct |
77 |
Correct |
258 ms |
30804 KB |
Output is correct |
78 |
Correct |
300 ms |
35536 KB |
Output is correct |
79 |
Correct |
326 ms |
33996 KB |
Output is correct |
80 |
Correct |
338 ms |
40656 KB |
Output is correct |
81 |
Correct |
327 ms |
37328 KB |
Output is correct |
82 |
Correct |
324 ms |
37328 KB |
Output is correct |
83 |
Correct |
290 ms |
33996 KB |
Output is correct |
84 |
Correct |
316 ms |
39868 KB |
Output is correct |
85 |
Correct |
336 ms |
38368 KB |
Output is correct |
86 |
Correct |
337 ms |
33728 KB |
Output is correct |
87 |
Correct |
316 ms |
35276 KB |
Output is correct |
88 |
Correct |
304 ms |
36560 KB |
Output is correct |
89 |
Correct |
303 ms |
32368 KB |
Output is correct |
90 |
Correct |
330 ms |
32216 KB |
Output is correct |
91 |
Correct |
319 ms |
34764 KB |
Output is correct |
92 |
Correct |
419 ms |
32596 KB |
Output is correct |
93 |
Correct |
276 ms |
32436 KB |
Output is correct |
94 |
Correct |
303 ms |
31692 KB |
Output is correct |
95 |
Correct |
290 ms |
33996 KB |
Output is correct |
96 |
Correct |
302 ms |
32716 KB |
Output is correct |
97 |
Correct |
307 ms |
34504 KB |
Output is correct |