Submission #739417

#TimeUsernameProblemLanguageResultExecution timeMemory
739417nguyentunglamCat in a tree (BOI17_catinatree)C++17
100 / 100
83 ms23360 KiB
#include<bits/stdc++.h> #define fi first #define se second #define endl "\n" #define ii pair<int, int> using namespace std; const int N = 2e5 + 10; int f[N], g[N]; vector<int> adj[N]; int n, d; void dfs(int u) { f[u] = d; vector<int> lst; for(int &v : adj[u]) { dfs(v); lst.push_back(f[v] + 1); g[u] += g[v]; } sort(lst.begin(), lst.end(), greater<int>()); int cnt = 0; for(int &j : lst) if (j + f[u] >= d) { f[u] = j; cnt++; } g[u] -= adj[u].size() - cnt; if (f[u] == d) g[u]++, f[u] = 0; } int main() { #define task "" cin.tie(0) -> sync_with_stdio(0); if (fopen ("task.inp", "r")) { freopen ("task.inp", "r", stdin); freopen ("task.out", "w", stdout); } if (fopen (task".inp", "r")) { freopen (task".inp", "r", stdin); freopen (task".out", "w", stdout); } cin >> n >> d; for(int i = 1; i < n; i++) { int x; cin >> x; adj[x].push_back(i); } dfs(0); cout << g[0]; }

Compilation message (stderr)

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