Submission #1116032

#TimeUsernameProblemLanguageResultExecution timeMemory
1116032vjudge1Cat in a tree (BOI17_catinatree)C++17
100 / 100
257 ms114788 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define el cout << '\n'
#define f1(i,n) for(int i=1;i<=n;i++)
#define __file_name ""
using namespace std;
const ll maxn = 1e6+5, inf=1e18;

int n, d, depth[maxn];
vector<int> G[maxn];

//struct cmp{
//    bool operator()(int x, int y) const{
//        return make_pair(depth[x], x) > make_pair(depth[y], y);
//    }
//};

set<pair<int,int>, less<pair<int,int>>> S[maxn];

void dfs(int u, int p){
    S[u].insert({depth[u], u});
    for(int c: G[u]){
        if(c != p){
            depth[c] = depth[u] + 1;
            dfs(c, u);
            if(S[c].size() > S[u].size()){
                swap(S[c], S[u]);
            }
//            cout << u << ' ' << c << "::: ";
            for(pair<int,int> item: S[c]) {
//                cout << item << ' ';
                S[u].insert(item);
            }
//            el;
        }
//        cout << u << ": ";
//        for(int item: S[u]) cout << item << ' ';el;
    }
    while(S[u].size() > 1){
        int v1 = (*S[u].begin()).second, v2 = (*next(S[u].begin())).second;
        if(depth[v1] + depth[v2] - 2*depth[u] < d){
//            cout << u << ' ' << v1 << "!!\n";
            S[u].erase(S[u].begin());
        }else break;
    }
//    cout << u << ": ";
//    for(int item: S[u]) cout << item << ' ';el;
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    if(fopen(__file_name ".inp", "r")){
        freopen(__file_name ".inp","r",stdin);
        freopen(__file_name ".out","w",stdout);
    }
    // code here
    cin >> n >> d;
    for(int i=2;i<=n;i++){
        int u; cin >> u; u++;
        G[u].push_back(i);
        G[i].push_back(u);
    }
    dfs(1, 0);
    cout << S[1].size();
    return 0;
}

Compilation message (stderr)

catinatree.cpp: In function 'int main()':
catinatree.cpp:55:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |         freopen(__file_name ".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:56:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |         freopen(__file_name ".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...