Submission #132498

#TimeUsernameProblemLanguageResultExecution timeMemory
132498tlwpdusCat in a tree (BOI17_catinatree)C++11
100 / 100
139 ms19140 KiB
#include <bits/stdc++.h>

using namespace std;

const int INF = 0x3f3f3f3f;

int n, d;
vector<int> lis[200100];
int st[200100], en[200100], par[200100], dep[200100];
int tree[530000];
const int key = 262144;

void init() {
    for (int i=0;i<key*2;i++) tree[i] = INF;
}
void upd(int s, int e, int val) {
    s += key; e += key;
    while(s<=e) {
        if (s&1) tree[s] = min(tree[s],val);
        if (~e&1)tree[e] = min(tree[e],val);
        s = (s+1)/2;
        e = (e-1)/2;
    }
}
int getv(int idx) {
    int ans = INF;
    idx+=key;
    while(idx) {
        ans = min(ans,tree[idx]);
        idx/=2;
    }
    return ans;
}

int tc;
void idfs(int here, int p) {
    st[here] = tc++;
    par[here] = p;
    dep[here] = (~p?dep[p]:-1)+1;
    for (int &there : lis[here]) idfs(there,here);
    en[here] = tc-1;
}

int main() {
    scanf("%d%d",&n,&d);
    for (int i=1;i<n;i++) {
        int p;
        scanf("%d",&p);
        lis[p].push_back(i);
    }
    idfs(0,-1);
    vector<int> ord(n,0);
    iota(ord.begin(),ord.end(),0);
    sort(ord.begin(),ord.end(),[](int a, int b){return dep[a]>dep[b];});
    int ans = 0;
    init();
    for (int i=0;i<n;i++) {
        int idx = ord[i];
        if (getv(st[idx])+dep[idx]>=d) {
            ans++;
            int p = idx;
            for (int j=0;j<d&&~p;j++,p=par[p]) {
                upd(st[p],en[p],dep[idx]-2*dep[p]);
            }
        }
    }
    printf("%d\n",ans);

    return 0;
}

Compilation message (stderr)

catinatree.cpp: In function 'int main()':
catinatree.cpp:45:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&n,&d);
     ~~~~~^~~~~~~~~~~~~~
catinatree.cpp:48:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&p);
         ~~~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...