This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
using namespace std;
// 17.4.1.2 Headers
// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
int nn;
bool check(vector<pair<int,int>> vt){
int n = vt.size();
if(vt[0].first!=1){
return false;
}
for(int i = 1; i < n; i++){
if(vt[i].first>vt[i-1].second+1){
return false;
}
}
return true;
}
vector<vector<int>> adjlist;
vector<bool> visited;
int endp;
int finddia(){
int n = adjlist.size();
visited.resize(n);
for(int i = 0; i < n; i++){
visited[i] = false;
}
visited[0] = true;
int dist[n];
dist[0] = 0;
stack<int> stk;
stk.push(0);
while(!stk.empty()){
int node = stk.top();
stk.pop();
for(auto& e: adjlist[node]){
if(!visited[e]){
dist[e] = dist[node]+1;
visited[e] = true;
stk.push(e);
}
}
}
int Max = 0;
int val = 0;
for(int i = 0; i < n; i++){
if(dist[i]>Max){
Max = dist[i];
val = i;
}
}
return val;
}
int dd;
vector<int> dist;
const int INF = 300000;
vector<int> degree;
void fs(int node){
visited[node] = true;
set<pair<int,int>> adjnodes;
if(dist[node] == dd){
dist[node] = 0;
}
for(auto& e: adjlist[node]){
dist[e] = min(dist[e],dist[node]+1);
adjnodes.insert({degree[e],e});
}
for(const auto & adjnode : adjnodes){
if(!visited[adjnode.second]) {
fs(adjnode.second);
}
}
if(dist[node] == dd){
dist[node] = 0;
}
}
int dfs(){
int n = adjlist.size();
dist.resize(n);
for(int i = 0; i < n; i++){
dist[i] = INF;
visited[i] = false;
}
dist[endp] = 0;
visited[endp] = true;
fs(endp);
int count = 0;
for(int i = 0; i < n; i++){
if(dist[i]==0){
count++;
}
}
return count;
}
void solve(){
int n;
cin>>n>>dd;
adjlist.resize(n);
degree.resize(n);
for(int i = 0; i < n; i++){
degree[i] = 0;
}
for(int i = 1; i < n; i++){
int a;
cin>>a;
adjlist[i].push_back(a);
adjlist[a].push_back(i);
degree[i]++;
degree[a]++;
}
endp = finddia();
cout<<dfs();
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int tc = 1;
//read(tc);
while(tc--) {
//write("Case #", i+1, ": ");
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |