#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)
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 |
8280 KB |
Output is correct |
3 |
Correct |
3 ms |
8220 KB |
Output is correct |
4 |
Correct |
3 ms |
8028 KB |
Output is correct |
5 |
Correct |
4 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8028 KB |
Output is correct |
7 |
Correct |
2 ms |
8028 KB |
Output is correct |
8 |
Correct |
2 ms |
8028 KB |
Output is correct |
9 |
Correct |
2 ms |
8024 KB |
Output is correct |
10 |
Correct |
2 ms |
10324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8024 KB |
Output is correct |
2 |
Correct |
2 ms |
8280 KB |
Output is correct |
3 |
Correct |
3 ms |
8220 KB |
Output is correct |
4 |
Correct |
3 ms |
8028 KB |
Output is correct |
5 |
Correct |
4 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8028 KB |
Output is correct |
7 |
Correct |
2 ms |
8028 KB |
Output is correct |
8 |
Correct |
2 ms |
8028 KB |
Output is correct |
9 |
Correct |
2 ms |
8024 KB |
Output is correct |
10 |
Correct |
2 ms |
10324 KB |
Output is correct |
11 |
Correct |
72 ms |
8284 KB |
Output is correct |
12 |
Correct |
70 ms |
8500 KB |
Output is correct |
13 |
Correct |
68 ms |
8280 KB |
Output is correct |
14 |
Correct |
67 ms |
8280 KB |
Output is correct |
15 |
Correct |
29 ms |
8540 KB |
Output is correct |
16 |
Correct |
34 ms |
8284 KB |
Output is correct |
17 |
Correct |
40 ms |
8544 KB |
Output is correct |
18 |
Correct |
35 ms |
8536 KB |
Output is correct |
19 |
Correct |
34 ms |
8536 KB |
Output is correct |
20 |
Correct |
33 ms |
8556 KB |
Output is correct |
21 |
Correct |
34 ms |
8540 KB |
Output is correct |
22 |
Correct |
37 ms |
8788 KB |
Output is correct |
23 |
Correct |
56 ms |
8536 KB |
Output is correct |
24 |
Correct |
37 ms |
8548 KB |
Output is correct |
25 |
Correct |
60 ms |
8540 KB |
Output is correct |
26 |
Correct |
60 ms |
8536 KB |
Output is correct |
27 |
Correct |
63 ms |
8540 KB |
Output is correct |
28 |
Correct |
52 ms |
8536 KB |
Output is correct |
29 |
Correct |
55 ms |
8540 KB |
Output is correct |
30 |
Correct |
48 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3077 ms |
46380 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8024 KB |
Output is correct |
2 |
Correct |
2 ms |
8280 KB |
Output is correct |
3 |
Correct |
3 ms |
8220 KB |
Output is correct |
4 |
Correct |
3 ms |
8028 KB |
Output is correct |
5 |
Correct |
4 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8028 KB |
Output is correct |
7 |
Correct |
2 ms |
8028 KB |
Output is correct |
8 |
Correct |
2 ms |
8028 KB |
Output is correct |
9 |
Correct |
2 ms |
8024 KB |
Output is correct |
10 |
Correct |
2 ms |
10324 KB |
Output is correct |
11 |
Correct |
72 ms |
8284 KB |
Output is correct |
12 |
Correct |
70 ms |
8500 KB |
Output is correct |
13 |
Correct |
68 ms |
8280 KB |
Output is correct |
14 |
Correct |
67 ms |
8280 KB |
Output is correct |
15 |
Correct |
29 ms |
8540 KB |
Output is correct |
16 |
Correct |
34 ms |
8284 KB |
Output is correct |
17 |
Correct |
40 ms |
8544 KB |
Output is correct |
18 |
Correct |
35 ms |
8536 KB |
Output is correct |
19 |
Correct |
34 ms |
8536 KB |
Output is correct |
20 |
Correct |
33 ms |
8556 KB |
Output is correct |
21 |
Correct |
34 ms |
8540 KB |
Output is correct |
22 |
Correct |
37 ms |
8788 KB |
Output is correct |
23 |
Correct |
56 ms |
8536 KB |
Output is correct |
24 |
Correct |
37 ms |
8548 KB |
Output is correct |
25 |
Correct |
60 ms |
8540 KB |
Output is correct |
26 |
Correct |
60 ms |
8536 KB |
Output is correct |
27 |
Correct |
63 ms |
8540 KB |
Output is correct |
28 |
Correct |
52 ms |
8536 KB |
Output is correct |
29 |
Correct |
55 ms |
8540 KB |
Output is correct |
30 |
Correct |
48 ms |
8540 KB |
Output is correct |
31 |
Execution timed out |
3077 ms |
46380 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |