Submission #624261

#TimeUsernameProblemLanguageResultExecution timeMemory
624261jeroenodbCat in a tree (BOI17_catinatree)C++14
100 / 100
71 ms21120 KiB
#include "bits/stdc++.h" using namespace std; #define all(x) begin(x),end(x) template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; } #define debug(a) cerr << "(" << #a << ": " << a << ")\n"; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pi; const int mxN = 2e5+3, oo = 1e9; int choice[mxN],d[mxN],N,D, minput[mxN]; vi adj[mxN], ans; void dfs(int at, int from) { choice[at]=at; minput[at]=oo; for(int to : adj[at]) if(to!=from) { d[to] = d[at]+1; dfs(to,at); minput[at] = min(minput[at],minput[to]+1); if(choice[to]) { if((d[choice[to]]-d[at])*2>=D) { ans.push_back(choice[to]); minput[at] = min(minput[at], d[choice[to]]-d[at]); } else if(d[choice[to]]>d[choice[at]]) { choice[at] = choice[to]; } } } int got = d[choice[at]]-d[at]; if(got+minput[at]<D) { choice[at]=0; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> N >> D; for(int i=2;i<=N;++i) { int x; cin >> x; adj[x+1].push_back(i); } dfs(1,0); if(choice[1]) ans.push_back(choice[1]); cout << ans.size() << '\n'; //<< ans << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...