Submission #242281

#TimeUsernameProblemLanguageResultExecution timeMemory
242281jamielimMagic Tree (CEOI19_magictree)C++14
3 / 100
119 ms44284 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> ii; typedef long double ld; #define mp make_pair #define pb push_back #define fi first #define se second #define ALL(x) x.begin(),x.end() #define SZ(x) (int)x.size() const int INF=2012345678; const ll LLINF=4012345678012345678LL; const ll MOD=1000000007; //998244353; // const ld PI=3.1415926535898; const ld EPS=1e-9; ll gcd(ll a,ll b){if(a<b)swap(a,b);if(b==0)return a;return gcd(b,a%b);} inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;} inline ll expo(ll b,ll p,ll m){ll res=1; while(p){if(p&1)res=(res*b)%m; b=(b*b)%m; p>>=1;} return res;} inline ll modinv(ll a,ll m){return expo(a,m-2,m);} int n,m,k; vector<int> adj[100005]; pair<int,int> r[100005]; ll memo[100005][25][2]; ll dfs(int x,int t,int f){ if(memo[x][t][f]!=-1)return memo[x][t][f]; if(f){ ll ans=(r[x].fi<=t?r[x].se:0); for(int i=0;i<SZ(adj[x]);i++){ ans+=max(dfs(adj[x][i],r[x].fi,0),dfs(adj[x][i],r[x].fi,1)); } //printf("%d %d %d %lld\n",x,t,f,ans); return memo[x][t][f]=ans; }else{ ll ans=0; for(int i=0;i<SZ(adj[x]);i++){ ans+=max(dfs(adj[x][i],t,0),dfs(adj[x][i],t,1)); } //printf("%d %d %d %lld\n",x,t,f,ans); return memo[x][t][f]=ans; } } int main(){ scanf("%d%d%d",&n,&m,&k); int p,v,d,w; for(int i=1;i<n;i++){ scanf("%d",&p);p--; adj[p].pb(i); } bool st2=1; for(int i=0;i<m;i++){ scanf("%d%d%d",&v,&d,&w);v--; r[v]=mp(d,w); if(SZ(adj[v])!=0)st2=0; } if(st2){ ll ans=0; for(int i=0;i<n;i++)ans+=r[i].se; printf("%lld",ans); }else{ memset(memo,-1,sizeof(memo)); printf("%lld",dfs(0,k,0)); } }

Compilation message (stderr)

magictree.cpp: In function 'int main()':
magictree.cpp:48:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d",&n,&m,&k);
  ~~~~~^~~~~~~~~~~~~~~~~~~
magictree.cpp:51:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&p);p--;
   ~~~~~^~~~~~~~~
magictree.cpp:56:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d",&v,&d,&w);v--;
   ~~~~~^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...