# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
451974 | fadi57 | Cat in a tree (BOI17_catinatree) | C++14 | 13 ms | 23756 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>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
const int mx=1e6+10;
typedef long long ll;
const int mod=1e9+7;
const int MXm=22;
#define F first
#define S second
const int inf=1e9+10;
vector<int>adj[mx];
int n,d,ans;
void dfs(int node,int depth,int par){
if(depth==0){
ans++;
depth=d-1;
}
// cout<<node<<" "<<depth<<" "<<endl;
depth--;
for(auto it:adj[node]){
if(it==par){
continue;
}
dfs(it,depth,node);
}
return;
}
int main(){
cin>>n>>d;
for(int i=1;i<n;i++){
int x;
cin>>x;
adj[i].push_back(x);
adj[x].push_back(i);
}int s;
for(int i=0;i<n;i++){
if(adj[i].size()==1){
s=i;
}
}
dfs(s,d,-1);
if(ans==0){
ans=-1;
}
cout<<ans+1;
}
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... |