Submission #736614

#TimeUsernameProblemLanguageResultExecution timeMemory
736614josanneo22Cat in a tree (BOI17_catinatree)C++17
100 / 100
13 ms6300 KiB
#include<bits/stdc++.h> using namespace std; #define pb push_back #define pii pair<int,int> const int inf=1000000010; const int MAXN=200010, LOG=20; char buf[1<<23],*p1=buf,*p2=buf; #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) inline int rd() { int s=0; char ch=getchar(),last; while(ch<'0'||ch>'9') last=ch,ch=getchar(); while(ch>='0'&&ch<='9') s=(s<<1)+(s<<3)+(ch^48),ch=getchar(); return last=='-'?-s:s; } int num[100]; inline void rt(int x) { if(x<0) putchar('-'),x=-x;; int len=0; do num[len++]=x%10;while(x/=10); while(len--) putchar(num[len]+'0'); } int n, D; int par[MAXN]; pii dp[MAXN][2], dpt[2]; inline void Add(int v, int u){ for (int i:{0, 1}) dpt[i]=dp[v][i]; for (int i:{0, 1}) for (int j:{0, 1}) if (dpt[i].second+dp[u][j].second>=D){ pii p={dpt[i].first+dp[u][j].first, min(dpt[i].second, dp[u][j].second)}; if (p>dp[v][0]) swap(dp[v][0], p); if (p.first!=dp[v][0].first && p>dp[v][1]) swap(dp[v][1], p); } } void solve(){ n=rd(),D=rd(); for (int i=1; i<n; i++)par[i]=rd(); for (int i=0; i<n; i++) dp[i][0]={1, 0}, dp[i][1]={0, inf}; for (int i=n-1; i; i--){ dp[i][0].second++; dp[i][1].second++; Add(par[i], i); } rt(dp[0][0].first); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); solve(); }

Compilation message (stderr)

catinatree.cpp: In function 'int rd()':
catinatree.cpp:16:18: warning: 'last' may be used uninitialized in this function [-Wmaybe-uninitialized]
   16 |  return last=='-'?-s:s;
      |         ~~~~~~~~~^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...