Submission #940922

# Submission time Handle Problem Language Result Execution time Memory
940922 2024-03-08T02:15:35 Z guagua0407 Capital City (JOI20_capital_city) C++17
0 / 100
235 ms 35864 KB
//#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=4e5+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 7 ms 24156 KB Output is correct
2 Correct 7 ms 24152 KB Output is correct
3 Correct 6 ms 24152 KB Output is correct
4 Correct 7 ms 24156 KB Output is correct
5 Correct 7 ms 24156 KB Output is correct
6 Correct 7 ms 24156 KB Output is correct
7 Incorrect 6 ms 24156 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 24156 KB Output is correct
2 Correct 7 ms 24152 KB Output is correct
3 Correct 6 ms 24152 KB Output is correct
4 Correct 7 ms 24156 KB Output is correct
5 Correct 7 ms 24156 KB Output is correct
6 Correct 7 ms 24156 KB Output is correct
7 Incorrect 6 ms 24156 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 192 ms 32824 KB Output is correct
2 Correct 184 ms 32852 KB Output is correct
3 Correct 235 ms 32824 KB Output is correct
4 Correct 171 ms 32708 KB Output is correct
5 Correct 185 ms 32768 KB Output is correct
6 Correct 170 ms 32708 KB Output is correct
7 Correct 186 ms 32712 KB Output is correct
8 Correct 167 ms 32432 KB Output is correct
9 Correct 200 ms 31988 KB Output is correct
10 Correct 188 ms 31940 KB Output is correct
11 Correct 208 ms 32044 KB Output is correct
12 Correct 189 ms 32044 KB Output is correct
13 Correct 215 ms 32064 KB Output is correct
14 Correct 187 ms 31940 KB Output is correct
15 Correct 198 ms 32100 KB Output is correct
16 Correct 201 ms 32040 KB Output is correct
17 Correct 208 ms 32116 KB Output is correct
18 Correct 190 ms 32040 KB Output is correct
19 Correct 211 ms 31944 KB Output is correct
20 Correct 195 ms 32044 KB Output is correct
21 Runtime error 19 ms 35864 KB Execution killed with signal 11
22 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 24156 KB Output is correct
2 Correct 7 ms 24152 KB Output is correct
3 Correct 6 ms 24152 KB Output is correct
4 Correct 7 ms 24156 KB Output is correct
5 Correct 7 ms 24156 KB Output is correct
6 Correct 7 ms 24156 KB Output is correct
7 Incorrect 6 ms 24156 KB Output isn't correct
8 Halted 0 ms 0 KB -