Submission #1099558

#TimeUsernameProblemLanguageResultExecution timeMemory
1099558ASN49KCat in a tree (BOI17_catinatree)C++14
11 / 100
1044 ms3156 KiB
#include <bits/stdc++.h> using namespace std; using i64=long long; #define UNUSED -1 #define all(x) x.begin(),x.end() #define pb push_back const int mod=1e9+7,inf=1e9+1; const i64 INF=1e18; mt19937 rng(69); namespace lmao { int main(int n,int dist,vector<int>&p) { vector<deque<int>>dp(n); for(auto &c:dp) { c.push_back(1); } for(int x=n-1;x>0;x--) { dp[x].push_front(dp[x].front()); if((int)dp[x].size()==dist+2) { dp[x].pop_back(); } deque<int>&b=dp[x],&a=dp[p[x]]; while(a.size()<b.size()) { a.push_back(0); } map<int,int>new_dp; for(int i=0;i<(int)a.size();i++) { for(int j=0;j<(int)b.size();j++) { if(i+j>=dist) new_dp[min(i,j)]=max(new_dp[min(i,j)],a[i]+b[j]); } } for(int i=(int)b.size()-1;i>=0;i--) { a[i]=max(a[i] , b[i]); } for(auto &c:new_dp) { a[c.first]=max(a[c.first] , c.second); } for(int i=(int)a.size()-2;i>=0;i--) { a[i]=max(a[i],a[i+1]); } } return dp[0][0]; } } namespace good { int main(int n,int dist,vector<int>&p) { vector<int>h(n); for(int i=1;i<n;i++) { h[i]=h[p[i]]+1; } auto get_dist=[&](int x,int y)->int { if(h[x]<h[y]) { swap(x,y); } int rez=0; while(h[x]>h[y]) { x=p[x]; rez++; } while(x!=y) { rez+=2; x=p[x]; y=p[y]; } return rez; }; int sol=0; for(int i=1;i<(1<<n);i++) { vector<int>poz; for(int j=0;j<n;j++) { if((i>>j)&1) { poz.pb(j); } } bool ok=true; int k=(int)poz.size(); for(int i=0;i<k;i++) { for(int j=0;j<i;j++) { if(get_dist(poz[i],poz[j]) < dist) { ok=false; } } } if(ok) { sol=max(sol , k); } } return sol; } }; main() { int n,dist; cin>>n>>dist; vector<int>p(n,0); for(int i=1;i<n;i++) { cin>>p[i]; } cout<<lmao::main(n,dist,p); /*const int n=4; while(true) { const int dist=rng()%n+1; vector<int>p(n); for(int i=1;i<n;i++) { p[i]=rng()%i; } if(good::main(n,dist,p)!=lmao::main(n,dist,p)) { cout<<n<<' '<<dist<<'\n'; for(int i=1;i<n;i++) { cout<<p[i]<<'\n'; } break; } }*/ }

Compilation message (stderr)

catinatree.cpp:122:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  122 | main()
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...