제출 #503739

#제출 시각아이디문제언어결과실행 시간메모리
503739Newtech66Cat in a tree (BOI17_catinatree)C++17
51 / 100
310 ms524292 KiB
#include<bits/stdc++.h> using namespace std; using lol=long long int; #define endl "\n" const lol mod1=1e9+7,mod2=998244353; const lol inf=1e18+8; const double eps=1e-12; const double PI=acos(-1.0); const int N=2e5+5; #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update using namespace __gnu_pbds; typedef tree<pair<int,int>,null_type,less<pair<int,int> >,rb_tree_tag,tree_order_statistics_node_update> ordered_set; //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); vector<vector<int> > g(N); vector<vector<int> > dp; void dfs(int u,int d,int p=-1) { dp[u][0]=1; for(auto v:g[u]) { if(v==p) continue; dfs(v,d,u); dp[u][0]+=dp[v][d-1]; } for(int x=1;x<=d/2;x++) { int mx=-1e9; int S=0; for(auto v:g[u]) { if(v==p) continue; S+=dp[v][d-x-1]; mx=max(mx,dp[v][x-1]-dp[v][d-x-1]); } dp[u][x]=S+mx; } for(int x=d/2+1;x<d;x++) { for(auto v:g[u]) { if(v==p) continue; dp[u][x]+=dp[v][x-1]; } } for(auto v:g[u]) { if(v==p) continue; dp[u][d]+=dp[v][d-1]; } int mx=dp[u][d]; for(int x=d-1;x>=0;x--) { mx=max(dp[u][x],mx); dp[u][x]=mx; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int _=1; //cin>>_; while(_--) { int n,d; cin>>n>>d; dp.resize(n); for(int i=0;i<n;i++) dp[i].resize(d+1,0); for(int i=1;i<n;i++) { int x; cin>>x; g[x].push_back(i); g[i].push_back(x); } dfs(0,d); cout<<dp[0][0]; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...