# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1115814 | vjudge1 | Cat in a tree (BOI17_catinatree) | C++17 | 55 ms | 23368 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define name "TENBAI"
#define fi first
#define se second
#define int long long
#define endl '\n'
#define setmin(x, y) x = min((x), (y))
#define setmax(x, y) x = max((x), (y))
#define sqr(x) ((x) * (x))
mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);}
const int NM = 2e5 + 5;
vector<int> g[NM];
int n, d;
pair<int, int> dp[NM];
void dfs(int u)
{
vector<int> V;
dp[u] = {0, 0};
for (int v : g[u])
{
dfs(v);
pair<int, int> t = {dp[v].fi, dp[v].se + 1};
dp[u].fi += t.fi;
V.push_back(t.se);
}
sort(V.begin(), V.end(), greater<>());
while (V.size() >= 2 && V[V.size() - 1] + V[V.size() - 2] < d)
{
dp[u].fi--;
V.pop_back();
}
dp[u].se = V.empty() ? d : V.back();
if (dp[u].se >= d)
dp[u].fi++, dp[u].se = 0;
}
void solve()
{
cin >> n >> d;
for (int i = 2; i <= n; i++)
{
int t;
cin >> t;
t++;
g[t].push_back(i);
}
dfs(1);
cout << dp[1].fi << endl;
}
signed main()
{
if (fopen("in.txt", "r"))
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
}
else if (fopen(name".inp", "r"))
{
freopen(name".inp", "r", stdin);
freopen(name".out", "w", stdout);
}
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int tc = 1;
// cin >> tc;
while (tc--)
solve();
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |