Submission #1254923

#TimeUsernameProblemLanguageResultExecution timeMemory
1254923minggaCat in a tree (BOI17_catinatree)C++20
100 / 100
31 ms15940 KiB
// Author: caption_mingle
#include "bits/stdc++.h"

using namespace std;

#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define ll long long
const int mod = 1e9 + 7;
const int inf = 2e9;
const int N = 2e5 + 7;
vector<int> g[N];
int n, f[N], f1[N], d;

void dfs(int u) {
	f1[u] = inf;
	for(int v : g[u]) {
		dfs(v);
		if(f1[u] + f1[v] + 1 >= d) {
			f[u] += f[v];
			f1[u] = min(f1[u], f1[v] + 1);
		} else {
			f[u] += f[v] - 1;
			f1[u] = max(f1[u], f1[v] + 1);
		}
	}
	if(f1[u] >= d) f[u]++, f1[u] = 0;
}

signed main() {
	cin.tie(0) -> sync_with_stdio(0);
	#define task ""
	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 p; cin >> p;
		g[p].pb(i);
	}
	dfs(0);
	cout << f[0];
    cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}

Compilation message (stderr)

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