이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <quadmath.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#define sz(x) (int)x.size()
//#define sqr(x) x*x
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,tune=native")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("no-stack-protector")
//#pragma GCC optimize("fast-math")
using namespace std;
//using namespace __gnu_pbds;
//
//#define int long long
////#define ld long double
//template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
vector<vector<int> > g;
int d;
pair<int, int> dfs(int v, int p)
{
vector<pair<int, int> > cur;
cur.push_back({0, 1});
int total = 1;
for (auto& u : g[v])
{
if (u != p)
{
auto[res, mn_dep] = dfs(u, v);
total += res;
cur.push_back({mn_dep, res});
}
}
sort(cur.rbegin(), cur.rend());
while (cur.size() > 1 && cur[cur.size() - 1].first + cur[cur.size() - 2].first < d)
{
total--;
cur.pop_back();
}
// if (cur[0].first == d)
// return {total + 1, 1};
return {total, cur.back().first + 1};
}
void solve()
{
int n;
cin >> n >> d;
g.resize(n);
for (int i = 1; i < n; i++)
{
int p;
cin >> p;
g[p].push_back(i);
}
auto [ans, mn] = dfs(0, 0);
cout << ans;
}
//
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
//cin >> t;
while (t--)
{
solve();
}
cerr << endl << fixed << setprecision(3) << "Time execution: " << (double)clock() / CLOCKS_PER_SEC << endl;
return 0;
}
//
//
/*
17 13
1 3 1 2 1 3 1 2 1 1 2 1 2 1 2 1 3
3 1 2 3 3 2 1 2 1 3 2 2 1
*/
/*
6 9
3 2 1 6 7 4
3 2 6 10 8 9 7 1 4
*/
/*
9 8
2 1 4 6 7 8 9 10 11
8 9 10 11 1 4 6 7
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |