Submission #1110987

#TimeUsernameProblemLanguageResultExecution timeMemory
1110987anmattroiCat in a tree (BOI17_catinatree)C++14
100 / 100
58 ms25968 KiB
#include <bits/stdc++.h>

#define maxn 200005

using namespace std;

int n, d;
vector<int> adj[maxn];
int f[maxn];
int ds = 0;

void dfs(int u, int dad) {
    f[u] = INT_MAX/3;
    vector<int> nho;
    for (int v : adj[u]) {
        if (v == dad) continue;
        dfs(v, u);
        nho.emplace_back(f[v]+1);
    }
    sort(nho.begin(), nho.end());
    reverse(nho.begin(), nho.end());
    while (nho.size() >= 2 && nho[int(nho.size())-1] + nho[int(nho.size())-2] < d) {--ds; nho.pop_back();}
    if (!nho.empty()) f[u] = nho.back();
    if (f[u] >= d) {
        ++ds;
        f[u] = 0;
    }
}
int main() {
    if (fopen("FARcatNOUGH.INP", "r")) {
        freopen("FARENOUGH.INP", "r", stdin);
        freopen("FARENOUGH.OUT", "w", stdout);
    }
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> d;
    for (int u = 2; u <= n; u++) {
        int v; cin >> v;
        ++v;
        adj[u].emplace_back(v);
        adj[v].emplace_back(u);
    }
    dfs(1, 0);
    cout << ds;
}

Compilation message (stderr)

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