Submission #172694

#TimeUsernameProblemLanguageResultExecution timeMemory
172694rzbtCat in a tree (BOI17_catinatree)C++14
100 / 100
116 ms15712 KiB
#include <bits/stdc++.h> #define mp make_pair #define pb push_back #define F first #define S second #define all(x) x.begin(),x.end() #define MAXN 200005 typedef long long ll; using namespace std; int dp[MAXN],udalj[MAXN]; int n,k; vector<int> graf[MAXN]; void dfs(int t){ if(graf[t].empty()){ dp[t]=1; udalj[t]=0; return; } dfs(graf[t][0]); udalj[t]=udalj[graf[t][0]]+1; dp[t]=dp[graf[t][0]]; if(udalj[t]==k){ udalj[t]=0; dp[t]++; } bool prvi=true; for(auto x:graf[t]){ if(prvi){ prvi=false; continue; } dfs(x); if(udalj[t]+udalj[x]+1>=k){ dp[t]+=dp[x]; udalj[t]=min(udalj[t],udalj[x]+1); }else{ dp[t]+=dp[x]-1; udalj[t]=max(udalj[t],udalj[x]+1); } } //printf("%d %d %d",t,udalj[t],dp[t]); } int main() { scanf("%d %d", &n, &k); for(int i=1;i<n;i++){ int t; scanf("%d", &t); graf[t].pb(i); } dfs(0); printf("%d",dp[0]); return 0; }

Compilation message (stderr)

catinatree.cpp: In function 'int main()':
catinatree.cpp:53:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &k);
     ~~~~~^~~~~~~~~~~~~~~~~
catinatree.cpp:56:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &t);
         ~~~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...