//#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++){
int C=c[ord[i]];
mx[C]=max(mx[C],i);
mn[C]=min(mn[C],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=0;i<k;i++){
assert(mnn[i]>=1 and mnn[i]<=n and mxx[i]>=1 and 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:105:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for(int i=1;i<ord.size();i++){
| ~^~~~~~~~~~~
capital_city.cpp:117:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | for(int i=1;i<ord.size();i++){
| ~^~~~~~~~~~~
capital_city.cpp:125:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
125 | 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 |
5 ms |
12124 KB |
Output is correct |
2 |
Correct |
4 ms |
12124 KB |
Output is correct |
3 |
Correct |
4 ms |
12004 KB |
Output is correct |
4 |
Correct |
4 ms |
12124 KB |
Output is correct |
5 |
Correct |
4 ms |
12124 KB |
Output is correct |
6 |
Correct |
4 ms |
12124 KB |
Output is correct |
7 |
Incorrect |
4 ms |
12012 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12124 KB |
Output is correct |
2 |
Correct |
4 ms |
12124 KB |
Output is correct |
3 |
Correct |
4 ms |
12004 KB |
Output is correct |
4 |
Correct |
4 ms |
12124 KB |
Output is correct |
5 |
Correct |
4 ms |
12124 KB |
Output is correct |
6 |
Correct |
4 ms |
12124 KB |
Output is correct |
7 |
Incorrect |
4 ms |
12012 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
197 ms |
20968 KB |
Output is correct |
2 |
Correct |
169 ms |
20912 KB |
Output is correct |
3 |
Correct |
185 ms |
20932 KB |
Output is correct |
4 |
Correct |
169 ms |
20936 KB |
Output is correct |
5 |
Correct |
200 ms |
20732 KB |
Output is correct |
6 |
Correct |
168 ms |
20936 KB |
Output is correct |
7 |
Correct |
184 ms |
20736 KB |
Output is correct |
8 |
Correct |
183 ms |
20704 KB |
Output is correct |
9 |
Correct |
186 ms |
20164 KB |
Output is correct |
10 |
Correct |
185 ms |
20056 KB |
Output is correct |
11 |
Correct |
209 ms |
20168 KB |
Output is correct |
12 |
Correct |
185 ms |
20164 KB |
Output is correct |
13 |
Correct |
187 ms |
20216 KB |
Output is correct |
14 |
Correct |
236 ms |
20156 KB |
Output is correct |
15 |
Correct |
191 ms |
20164 KB |
Output is correct |
16 |
Correct |
186 ms |
20164 KB |
Output is correct |
17 |
Correct |
200 ms |
20248 KB |
Output is correct |
18 |
Correct |
187 ms |
20164 KB |
Output is correct |
19 |
Correct |
194 ms |
20164 KB |
Output is correct |
20 |
Correct |
194 ms |
20272 KB |
Output is correct |
21 |
Correct |
4 ms |
12124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12124 KB |
Output is correct |
2 |
Correct |
4 ms |
12124 KB |
Output is correct |
3 |
Correct |
4 ms |
12004 KB |
Output is correct |
4 |
Correct |
4 ms |
12124 KB |
Output is correct |
5 |
Correct |
4 ms |
12124 KB |
Output is correct |
6 |
Correct |
4 ms |
12124 KB |
Output is correct |
7 |
Incorrect |
4 ms |
12012 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |