//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int mxn=2e5+5;
const int inf=1e9;
vector<int> adj[mxn];
bool visited[mxn];
int c[mxn];
vector<int> segtree(mxn*4,inf);
int n,k;
void update(int pos,int val,int l=1,int r=n,int v=1){
if(l==r){
segtree[v]=min(segtree[v],val);
return;
}
int mid=(l+r)/2;
if(pos<=mid) update(pos,val,l,mid,v*2);
else update(pos,val,mid+1,r,v*2+1);
segtree[v]=min(segtree[v*2],segtree[v*2+1]);
}
int query(int tl,int tr,int l=1,int r=n,int v=1){
if(r<tl or tr<l){
return inf;
}
if(tl<=l and r<=tr){
return segtree[v];
}
int mid=(l+r)/2;
return min(query(tl,min(mid,tr),l,mid,v*2),query(max(mid+1,tl),tr,mid+1,r,v*2+1));
}
int main() {_
cin>>n>>k;
for(int i=0;i<n-1;i++){
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for(int i=1;i<=n;i++){
cin>>c[i];
c[i]--;
}
int st;
for(int i=1;i<=n;i++){
if((int)adj[i].size()==1) st=i;
}
vector<int> ord;
ord.push_back(-1);
queue<int> q;
q.push(st);
while(!q.empty()){
int v=q.front();
q.pop();
visited[v]=true;
ord.push_back(v);
for(auto u:adj[v]){
if(visited[u]) continue;
q.push(u);
}
}
vector<int> mnn(k),mxx(k);
{
vector<int> mn(k,inf),mx(k,-1);
for(int i=1;i<ord.size();i++){
mx[c[ord[i]]]=max(mx[c[ord[i]]],i);
mn[c[ord[i]]]=min(mn[c[ord[i]]],i);
}
for(int i=1;i<ord.size();i++){
int C=c[ord[i]];
int val=query(mn[C],i);
val=min(val,mn[C]);
if(i==mx[C]){
mnn[C]=val;
}
update(i,val);
}
}
reverse(ord.begin()+1,ord.end());
segtree=vector<int>(mxn*4,inf);
{
vector<int> mn(k,inf),mx(k,-1);
for(int i=1;i<ord.size();i++){
mx[c[ord[i]]]=max(mx[c[ord[i]]],i);
mn[c[ord[i]]]=min(mn[c[ord[i]]],i);
}
for(int i=1;i<ord.size();i++){
int C=c[ord[i]];
int val=query(mn[C],i);
val=min(val,mn[C]);
if(i==mx[C]){
mxx[C]=val;
}
update(i,val);
}
}
reverse(ord.begin()+1,ord.end());
vector<int> mn(k,inf),mx(k,-1);
for(int i=1;i<ord.size();i++){
mx[c[ord[i]]]=max(mx[c[ord[i]]],i);
mn[c[ord[i]]]=min(mn[c[ord[i]]],i);
}
for(int i=0;i<k;i++){
mxx[i]=n-mxx[i]+1;
}
vector<int> pre(ord.size());
for(int i=1;i<ord.size();i++){
int C=c[ord[i]];
pre[i]=pre[i-1];
if(i==mx[C]){
pre[i]++;
}
}
int ans=inf;
/*for(int i=1;i<ord.size();i++){
cout<<c[ord[i]]<<' ';
}
cout<<'\n';*/
for(int i=0;i<k;i++){
//cout<<mnn[i]<<' '<<mxx[i]<<'\n';
ans=min(ans,pre[mxx[i]]-pre[mnn[i]-1]-1);
}
cout<<ans<<'\n';
return 0;
}
//maybe its multiset not set
//yeeorz
//laborz
Compilation message
capital_city.cpp: In function 'int main()':
capital_city.cpp:82:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for(int i=1;i<ord.size();i++){
| ~^~~~~~~~~~~
capital_city.cpp:86:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for(int i=1;i<ord.size();i++){
| ~^~~~~~~~~~~
capital_city.cpp:100:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for(int i=1;i<ord.size();i++){
| ~^~~~~~~~~~~
capital_city.cpp:104:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for(int i=1;i<ord.size();i++){
| ~^~~~~~~~~~~
capital_city.cpp:116:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
116 | for(int i=1;i<ord.size();i++){
| ~^~~~~~~~~~~
capital_city.cpp:124:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
124 | for(int i=1;i<ord.size();i++){
| ~^~~~~~~~~~~
capital_city.cpp: In function 'void setIO(std::string)':
capital_city.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
12124 KB |
Output is correct |
2 |
Correct |
4 ms |
12124 KB |
Output is correct |
3 |
Correct |
7 ms |
12216 KB |
Output is correct |
4 |
Correct |
4 ms |
12124 KB |
Output is correct |
5 |
Correct |
6 ms |
12124 KB |
Output is correct |
6 |
Correct |
5 ms |
12124 KB |
Output is correct |
7 |
Incorrect |
4 ms |
12124 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
12124 KB |
Output is correct |
2 |
Correct |
4 ms |
12124 KB |
Output is correct |
3 |
Correct |
7 ms |
12216 KB |
Output is correct |
4 |
Correct |
4 ms |
12124 KB |
Output is correct |
5 |
Correct |
6 ms |
12124 KB |
Output is correct |
6 |
Correct |
5 ms |
12124 KB |
Output is correct |
7 |
Incorrect |
4 ms |
12124 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
184 ms |
24580 KB |
Output is correct |
2 |
Correct |
172 ms |
24608 KB |
Output is correct |
3 |
Correct |
184 ms |
24456 KB |
Output is correct |
4 |
Correct |
169 ms |
24520 KB |
Output is correct |
5 |
Correct |
216 ms |
24312 KB |
Output is correct |
6 |
Correct |
169 ms |
24452 KB |
Output is correct |
7 |
Correct |
185 ms |
24260 KB |
Output is correct |
8 |
Correct |
165 ms |
24236 KB |
Output is correct |
9 |
Correct |
211 ms |
23780 KB |
Output is correct |
10 |
Correct |
195 ms |
23796 KB |
Output is correct |
11 |
Correct |
189 ms |
23796 KB |
Output is correct |
12 |
Correct |
207 ms |
23808 KB |
Output is correct |
13 |
Correct |
187 ms |
23824 KB |
Output is correct |
14 |
Correct |
187 ms |
23828 KB |
Output is correct |
15 |
Correct |
193 ms |
23692 KB |
Output is correct |
16 |
Correct |
200 ms |
23888 KB |
Output is correct |
17 |
Correct |
204 ms |
23836 KB |
Output is correct |
18 |
Correct |
234 ms |
23624 KB |
Output is correct |
19 |
Correct |
209 ms |
23736 KB |
Output is correct |
20 |
Correct |
188 ms |
23724 KB |
Output is correct |
21 |
Runtime error |
11 ms |
18008 KB |
Execution killed with signal 11 |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
12124 KB |
Output is correct |
2 |
Correct |
4 ms |
12124 KB |
Output is correct |
3 |
Correct |
7 ms |
12216 KB |
Output is correct |
4 |
Correct |
4 ms |
12124 KB |
Output is correct |
5 |
Correct |
6 ms |
12124 KB |
Output is correct |
6 |
Correct |
5 ms |
12124 KB |
Output is correct |
7 |
Incorrect |
4 ms |
12124 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |