# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1116032 | vjudge1 | Cat in a tree (BOI17_catinatree) | C++17 | 257 ms | 114788 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>
#define ll long long
#define ld long double
#define el cout << '\n'
#define f1(i,n) for(int i=1;i<=n;i++)
#define __file_name ""
using namespace std;
const ll maxn = 1e6+5, inf=1e18;
int n, d, depth[maxn];
vector<int> G[maxn];
//struct cmp{
// bool operator()(int x, int y) const{
// return make_pair(depth[x], x) > make_pair(depth[y], y);
// }
//};
set<pair<int,int>, less<pair<int,int>>> S[maxn];
void dfs(int u, int p){
S[u].insert({depth[u], u});
for(int c: G[u]){
if(c != p){
depth[c] = depth[u] + 1;
dfs(c, u);
if(S[c].size() > S[u].size()){
swap(S[c], S[u]);
}
// cout << u << ' ' << c << "::: ";
for(pair<int,int> item: S[c]) {
// cout << item << ' ';
S[u].insert(item);
}
// el;
}
// cout << u << ": ";
// for(int item: S[u]) cout << item << ' ';el;
}
while(S[u].size() > 1){
int v1 = (*S[u].begin()).second, v2 = (*next(S[u].begin())).second;
if(depth[v1] + depth[v2] - 2*depth[u] < d){
// cout << u << ' ' << v1 << "!!\n";
S[u].erase(S[u].begin());
}else break;
}
// cout << u << ": ";
// for(int item: S[u]) cout << item << ' ';el;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
if(fopen(__file_name ".inp", "r")){
freopen(__file_name ".inp","r",stdin);
freopen(__file_name ".out","w",stdout);
}
// code here
cin >> n >> d;
for(int i=2;i<=n;i++){
int u; cin >> u; u++;
G[u].push_back(i);
G[i].push_back(u);
}
dfs(1, 0);
cout << S[1].size();
return 0;
}
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... |