Submission #789987

# Submission time Handle Problem Language Result Execution time Memory
789987 2023-07-22T08:44:30 Z 1075508020060209tc Cat in a tree (BOI17_catinatree) C++14
0 / 100
8 ms 9940 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
int n;int D;
vector<int>e[200005];
int dph[200005];
int tak[200005];
vector<int>dfn;
int rdfn[1000006];
 
void dfs(int nw,int pa){
dfn.push_back(nw);
rdfn[nw]=dfn.size()-1;
dph[nw]=dph[pa]+1;
for(int i=0;i<e[nw].size();i++){
    int v=e[nw][i];
    if(v==pa){continue;}
    dfs(v,nw);
    dfn.push_back(nw);
}
}
int dis[200005];
void bfs(vector<int>st){
queue<int>q;
for(int i=1;i<=n;i++){
    dis[i]=1e9;
}
for(int i=0;i<st.size();i++){
    q.push(st[i]);
    dis[st[i]]=0;
}
while(q.size()){
    int nw=q.front();
    q.pop();
    for(int i=0;i<e[nw].size();i++){
        int v=e[nw][i];
        if(dis[v]<=n){continue;}
        dis[v]=dis[nw]+1;
        q.push(v);
    }
}
}
int rmq[22][500005];
int lca(int a,int b){
int l=rdfn[a];int r=rdfn[b];
if(r<l){swap(l,r);}
int lg=__lg(r-l+1);
int ret=rmq[lg][l];
if(dph[ret]>dph[rmq[lg][r-(1<<lg)+1]]){
    ret=rmq[lg][r-(1<<lg)+1];
}
return ret;
}
int dst(int a,int b){
int lc=lca(a,b);
return dph[a]+dph[b]-dph[lc]*2;
}
 
signed main()
{
    dfn.push_back(0);
    cin>>n>>D;
    for(int i=2;i<=n;i++){
        int v;
        cin>>v;
        v++;
        e[v].push_back(i);
        e[i].push_back(v);
    }
    dfs(1,0);
    vector<pair<int,int>>seq;
    for(int i=1;i<=n;i++){
        seq.push_back({dph[i],i});
    }
    sort(seq.begin(),seq.end());
    reverse(seq.begin(),seq.end());
    for(int i=1;i<dfn.size();i++){
        rmq[0][i]=dfn[i];
    }
    for(int i=1;i<=20;i++){
        for(int j=1;j<dfn.size();j++){
            if( (j+(1<<i)-1)>=dfn.size()){break;}
            rmq[i][j]=rmq[i-1][j];
            if(dph[rmq[i][j]]>dph[rmq[i-1][j+(1<<(i-1))]]){
                rmq[i][j]=rmq[i-1][j+(1<<(i-1))];
            }
        }
    }
    int B=450;
 
 
    vector<int>ans;
    ans.push_back(seq[0].second);
    tak[seq[0].second]++;
    bfs(ans);int lst=0;
    for(int i=1;i<seq.size();i++){
        int nw=seq[i].second;
        if(dis[nw]>=D){
        int ok=1;
            for(int j=i-1;j>lst;j++){
                int v=seq[j].second;
                if(tak[v]){
                    if(dst(nw,v)<D){ok=0;break;}
                }
            }
            if(ok){ans.push_back(nw);tak[nw]=1;}
        }
        if(i%450==0){
            bfs(ans);
            lst=i;
        }
    }
    cout<<ans.size();
 
}

Compilation message

catinatree.cpp: In function 'void dfs(int, int)':
catinatree.cpp:16:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 | for(int i=0;i<e[nw].size();i++){
      |             ~^~~~~~~~~~~~~
catinatree.cpp: In function 'void bfs(std::vector<int>)':
catinatree.cpp:29:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 | for(int i=0;i<st.size();i++){
      |             ~^~~~~~~~~~
catinatree.cpp:36:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |     for(int i=0;i<e[nw].size();i++){
      |                 ~^~~~~~~~~~~~~
catinatree.cpp: In function 'int main()':
catinatree.cpp:78:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |     for(int i=1;i<dfn.size();i++){
      |                 ~^~~~~~~~~~~
catinatree.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 j=1;j<dfn.size();j++){
      |                     ~^~~~~~~~~~~
catinatree.cpp:83:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |             if( (j+(1<<i)-1)>=dfn.size()){break;}
      |                 ~~~~~~~~~~~~^~~~~~~~~~~~
catinatree.cpp:97:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |     for(int i=1;i<seq.size();i++){
      |                 ~^~~~~~~~~~~
catinatree.cpp:90:9: warning: unused variable 'B' [-Wunused-variable]
   90 |     int B=450;
      |         ^
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 9940 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 9940 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 8 ms 9940 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -