#include<bits/stdc++.h>
#define int long long
using namespace std;
struct wi{
vector<int> Q;
int wiel=0;
int kto=0;
int ojc;
}*w;
vector<int> lista[300000];
int wynik=1e9;
int byl[300000];
int odw[300000];
int kolor[300000];
int licz_wiel(int x,int ojc=-1)
{
w[x].wiel=1;
for(int child:w[x].Q)
{
if(child==ojc || byl[child])
continue;
w[x].wiel+=licz_wiel(child,x);
}
return w[x].wiel;
}
int licz_centr(int x,int wiel,int ojc=-1)
{
for(int child:w[x].Q)
{
if(child==ojc || byl[child])
continue;
if(w[child].wiel*2>wiel)
return licz_centr(child,wiel,x);
}
return x;
}
void dfs(int x,int ojc,int kto)
{
w[x].kto=kto;
w[x].ojc=ojc;
for(int i:w[x].Q)
{
if(i==ojc || byl[i])
continue;
dfs(i,x,kto);
}
}
void centroid(int x)
{
int c=licz_centr(x,licz_wiel(x));
byl[c]=1;
dfs(c,c,c);
queue<int> kolejka;
kolejka.push(kolor[c]);
int wynikk=0;
while(!kolejka.empty())
{
int y=kolejka.front();
kolejka.pop();
if(odw[y]==c)
continue;
odw[y]=c;
for(int i=0;i<lista[y].size();i++)
{
int pom=lista[y][i];
if(w[pom].kto!=c)
{
wynikk=1e9;
break;
}
kolejka.push(kolor[w[pom].ojc]);
}
wynikk++;
}
wynik=min(wynik,wynikk);
for(int child:w[c].Q)
if(byl[child]==0)
centroid(child);
}
int32_t main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
int n,k,x,y;
cin>>n>>k;
w=new wi[n+3];
for(int i=1;i<n;i++)
{
cin>>x>>y;
w[x].Q.push_back(y);
w[y].Q.push_back(x);
}
for(int i=1;i<=n;i++)
{
int c;
cin>>c;
kolor[i]=c;
lista[c].push_back(i);
}
centroid(1);
cout<<wynik-1;
return 0;
}
Compilation message
capital_city.cpp: In function 'void centroid(long long int)':
capital_city.cpp:63:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(int i=0;i<lista[y].size();i++)
| ~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8024 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
2 ms |
8120 KB |
Output is correct |
4 |
Correct |
2 ms |
8024 KB |
Output is correct |
5 |
Correct |
3 ms |
8028 KB |
Output is correct |
6 |
Correct |
3 ms |
8028 KB |
Output is correct |
7 |
Correct |
2 ms |
8028 KB |
Output is correct |
8 |
Correct |
2 ms |
8124 KB |
Output is correct |
9 |
Correct |
2 ms |
8024 KB |
Output is correct |
10 |
Correct |
2 ms |
10076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8024 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
2 ms |
8120 KB |
Output is correct |
4 |
Correct |
2 ms |
8024 KB |
Output is correct |
5 |
Correct |
3 ms |
8028 KB |
Output is correct |
6 |
Correct |
3 ms |
8028 KB |
Output is correct |
7 |
Correct |
2 ms |
8028 KB |
Output is correct |
8 |
Correct |
2 ms |
8124 KB |
Output is correct |
9 |
Correct |
2 ms |
8024 KB |
Output is correct |
10 |
Correct |
2 ms |
10076 KB |
Output is correct |
11 |
Correct |
3 ms |
8280 KB |
Output is correct |
12 |
Correct |
3 ms |
8284 KB |
Output is correct |
13 |
Correct |
3 ms |
8508 KB |
Output is correct |
14 |
Correct |
3 ms |
8284 KB |
Output is correct |
15 |
Correct |
3 ms |
8536 KB |
Output is correct |
16 |
Correct |
3 ms |
8284 KB |
Output is correct |
17 |
Correct |
3 ms |
8540 KB |
Output is correct |
18 |
Correct |
3 ms |
8540 KB |
Output is correct |
19 |
Correct |
3 ms |
8368 KB |
Output is correct |
20 |
Correct |
3 ms |
8536 KB |
Output is correct |
21 |
Correct |
3 ms |
8540 KB |
Output is correct |
22 |
Correct |
3 ms |
8540 KB |
Output is correct |
23 |
Correct |
3 ms |
8536 KB |
Output is correct |
24 |
Correct |
5 ms |
8548 KB |
Output is correct |
25 |
Correct |
5 ms |
8540 KB |
Output is correct |
26 |
Correct |
4 ms |
8376 KB |
Output is correct |
27 |
Correct |
4 ms |
8536 KB |
Output is correct |
28 |
Correct |
4 ms |
8540 KB |
Output is correct |
29 |
Correct |
4 ms |
8352 KB |
Output is correct |
30 |
Correct |
3 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
304 ms |
47292 KB |
Output is correct |
2 |
Correct |
142 ms |
47672 KB |
Output is correct |
3 |
Correct |
318 ms |
46936 KB |
Output is correct |
4 |
Correct |
149 ms |
47584 KB |
Output is correct |
5 |
Correct |
322 ms |
44716 KB |
Output is correct |
6 |
Correct |
149 ms |
47760 KB |
Output is correct |
7 |
Correct |
367 ms |
45028 KB |
Output is correct |
8 |
Correct |
137 ms |
47508 KB |
Output is correct |
9 |
Correct |
351 ms |
43604 KB |
Output is correct |
10 |
Correct |
370 ms |
41796 KB |
Output is correct |
11 |
Correct |
315 ms |
44140 KB |
Output is correct |
12 |
Correct |
397 ms |
46444 KB |
Output is correct |
13 |
Correct |
339 ms |
41236 KB |
Output is correct |
14 |
Correct |
360 ms |
46364 KB |
Output is correct |
15 |
Correct |
367 ms |
46092 KB |
Output is correct |
16 |
Correct |
321 ms |
42216 KB |
Output is correct |
17 |
Correct |
377 ms |
42624 KB |
Output is correct |
18 |
Correct |
358 ms |
42844 KB |
Output is correct |
19 |
Correct |
355 ms |
45172 KB |
Output is correct |
20 |
Correct |
357 ms |
46892 KB |
Output is correct |
21 |
Correct |
2 ms |
10324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8024 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
2 ms |
8120 KB |
Output is correct |
4 |
Correct |
2 ms |
8024 KB |
Output is correct |
5 |
Correct |
3 ms |
8028 KB |
Output is correct |
6 |
Correct |
3 ms |
8028 KB |
Output is correct |
7 |
Correct |
2 ms |
8028 KB |
Output is correct |
8 |
Correct |
2 ms |
8124 KB |
Output is correct |
9 |
Correct |
2 ms |
8024 KB |
Output is correct |
10 |
Correct |
2 ms |
10076 KB |
Output is correct |
11 |
Correct |
3 ms |
8280 KB |
Output is correct |
12 |
Correct |
3 ms |
8284 KB |
Output is correct |
13 |
Correct |
3 ms |
8508 KB |
Output is correct |
14 |
Correct |
3 ms |
8284 KB |
Output is correct |
15 |
Correct |
3 ms |
8536 KB |
Output is correct |
16 |
Correct |
3 ms |
8284 KB |
Output is correct |
17 |
Correct |
3 ms |
8540 KB |
Output is correct |
18 |
Correct |
3 ms |
8540 KB |
Output is correct |
19 |
Correct |
3 ms |
8368 KB |
Output is correct |
20 |
Correct |
3 ms |
8536 KB |
Output is correct |
21 |
Correct |
3 ms |
8540 KB |
Output is correct |
22 |
Correct |
3 ms |
8540 KB |
Output is correct |
23 |
Correct |
3 ms |
8536 KB |
Output is correct |
24 |
Correct |
5 ms |
8548 KB |
Output is correct |
25 |
Correct |
5 ms |
8540 KB |
Output is correct |
26 |
Correct |
4 ms |
8376 KB |
Output is correct |
27 |
Correct |
4 ms |
8536 KB |
Output is correct |
28 |
Correct |
4 ms |
8540 KB |
Output is correct |
29 |
Correct |
4 ms |
8352 KB |
Output is correct |
30 |
Correct |
3 ms |
8540 KB |
Output is correct |
31 |
Correct |
304 ms |
47292 KB |
Output is correct |
32 |
Correct |
142 ms |
47672 KB |
Output is correct |
33 |
Correct |
318 ms |
46936 KB |
Output is correct |
34 |
Correct |
149 ms |
47584 KB |
Output is correct |
35 |
Correct |
322 ms |
44716 KB |
Output is correct |
36 |
Correct |
149 ms |
47760 KB |
Output is correct |
37 |
Correct |
367 ms |
45028 KB |
Output is correct |
38 |
Correct |
137 ms |
47508 KB |
Output is correct |
39 |
Correct |
351 ms |
43604 KB |
Output is correct |
40 |
Correct |
370 ms |
41796 KB |
Output is correct |
41 |
Correct |
315 ms |
44140 KB |
Output is correct |
42 |
Correct |
397 ms |
46444 KB |
Output is correct |
43 |
Correct |
339 ms |
41236 KB |
Output is correct |
44 |
Correct |
360 ms |
46364 KB |
Output is correct |
45 |
Correct |
367 ms |
46092 KB |
Output is correct |
46 |
Correct |
321 ms |
42216 KB |
Output is correct |
47 |
Correct |
377 ms |
42624 KB |
Output is correct |
48 |
Correct |
358 ms |
42844 KB |
Output is correct |
49 |
Correct |
355 ms |
45172 KB |
Output is correct |
50 |
Correct |
357 ms |
46892 KB |
Output is correct |
51 |
Correct |
2 ms |
10324 KB |
Output is correct |
52 |
Correct |
261 ms |
36016 KB |
Output is correct |
53 |
Correct |
272 ms |
36232 KB |
Output is correct |
54 |
Correct |
326 ms |
36484 KB |
Output is correct |
55 |
Correct |
273 ms |
36356 KB |
Output is correct |
56 |
Correct |
294 ms |
36004 KB |
Output is correct |
57 |
Correct |
294 ms |
36256 KB |
Output is correct |
58 |
Correct |
253 ms |
38484 KB |
Output is correct |
59 |
Correct |
209 ms |
38884 KB |
Output is correct |
60 |
Correct |
312 ms |
37716 KB |
Output is correct |
61 |
Correct |
273 ms |
37716 KB |
Output is correct |
62 |
Correct |
140 ms |
47640 KB |
Output is correct |
63 |
Correct |
141 ms |
47696 KB |
Output is correct |
64 |
Correct |
135 ms |
47356 KB |
Output is correct |
65 |
Correct |
154 ms |
47952 KB |
Output is correct |
66 |
Correct |
191 ms |
41912 KB |
Output is correct |
67 |
Correct |
221 ms |
41888 KB |
Output is correct |
68 |
Correct |
206 ms |
41880 KB |
Output is correct |
69 |
Correct |
242 ms |
41768 KB |
Output is correct |
70 |
Correct |
197 ms |
41668 KB |
Output is correct |
71 |
Correct |
223 ms |
42044 KB |
Output is correct |
72 |
Correct |
179 ms |
41920 KB |
Output is correct |
73 |
Correct |
193 ms |
41120 KB |
Output is correct |
74 |
Correct |
211 ms |
41924 KB |
Output is correct |
75 |
Correct |
205 ms |
41924 KB |
Output is correct |
76 |
Correct |
280 ms |
41556 KB |
Output is correct |
77 |
Correct |
262 ms |
39872 KB |
Output is correct |
78 |
Correct |
350 ms |
42724 KB |
Output is correct |
79 |
Correct |
329 ms |
40784 KB |
Output is correct |
80 |
Correct |
401 ms |
46732 KB |
Output is correct |
81 |
Correct |
325 ms |
43884 KB |
Output is correct |
82 |
Correct |
378 ms |
44088 KB |
Output is correct |
83 |
Correct |
363 ms |
41044 KB |
Output is correct |
84 |
Correct |
371 ms |
45652 KB |
Output is correct |
85 |
Correct |
357 ms |
44724 KB |
Output is correct |
86 |
Correct |
350 ms |
40852 KB |
Output is correct |
87 |
Correct |
340 ms |
42444 KB |
Output is correct |
88 |
Correct |
283 ms |
43604 KB |
Output is correct |
89 |
Correct |
301 ms |
40652 KB |
Output is correct |
90 |
Correct |
345 ms |
40560 KB |
Output is correct |
91 |
Correct |
294 ms |
42372 KB |
Output is correct |
92 |
Correct |
310 ms |
40980 KB |
Output is correct |
93 |
Correct |
308 ms |
41112 KB |
Output is correct |
94 |
Correct |
283 ms |
40532 KB |
Output is correct |
95 |
Correct |
299 ms |
41788 KB |
Output is correct |
96 |
Correct |
303 ms |
40436 KB |
Output is correct |
97 |
Correct |
276 ms |
42236 KB |
Output is correct |