# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
148104 | WhipppedCream | Cat in a tree (BOI17_catinatree) | C++17 | 319 ms | 51732 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.
//Power Of Ninja Go
#include <bits/stdc++.h>
//#ifdef atom #else #endif
using namespace std;
typedef long long ll; typedef pair<int, int> ii; typedef vector<int> vi; typedef vector< ii > vii;
#define X first
#define Y second
#define pb push_back
int n, k;
const int maxn = 2e5+5;
vi adj[maxn];
int h[maxn];
multiset<int> bst[maxn];
void dfs(int u)
{
for(auto v : adj[u])
{
h[v] = 1+h[u];
dfs(v);
}
}
void solve(int u)
{
for(auto v : adj[u]) solve(v);
int big = -1;
for(auto v : adj[u])
{
if(big == -1 || bst[v].size()> bst[big].size()) big = v;
}
if(big == -1) return;
swap(bst[u], bst[big]);
for(auto v : adj[u])
{
while(!bst[v].empty() && !bst[u].empty() && *bst[u].begin() + *bst[v].begin() - 2*h[u]< k)
{
if(*bst[u].begin()< *bst[v].begin()) bst[u].erase(bst[u].begin());
else bst[v].erase(bst[v].begin());
}
for(auto x : bst[v]) bst[u].insert(x);
}
}
int main()
{
//#ifndef atom freopen(".in", "r", stdin); freopen(".out", "w", stdout); #endif
scanf("%d %d", &n, &k);
for(int i = 2; i<= n; i++)
{
int x; scanf("%d", &x);
x++;
adj[x].pb(i);
}
dfs(1);
for(int i = 1; i<= n; i++) bst[i].insert(h[i]);
solve(1); printf("%d\n", bst[1].size());
}
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... |