Submission #61851

#TimeUsernameProblemLanguageResultExecution timeMemory
61851IvanCChase (CEOI17_chase)C++17
100 / 100
805 ms358920 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 1e5 + 10; const int MAXC = 1e2 + 5; vector<int> grafo[MAXN]; ll dp[MAXN][MAXC][2],P[MAXN],somatorio[MAXN],best; ll copia[MAXN][MAXC],copia_sigma[MAXN],atual[MAXN][MAXC]; int N,C; void adicona(int v,int u){ for(int i = 1;i<=C;i++){ if(atual[u][i] > dp[v][i][0]){ dp[v][i][1] = dp[v][i][0]; dp[v][i][0] = atual[u][i]; } else if(atual[u][i] > dp[v][i][1]){ dp[v][i][1] = atual[u][i]; } } somatorio[v] += P[u]; } void tira(int v,int u){ for(int i = 1;i<=C;i++){ if(atual[u][i] == dp[v][i][0]){ dp[v][i][0] = dp[v][i][1]; } } somatorio[v] -= P[u]; } void faz_copia(int v){ for(int i = 0;i<=C;i++){ copia[v][i] = dp[v][i][0]; } copia_sigma[v] = somatorio[v]; } void restaura(int v){ for(int i = 0;i<=C;i++){ dp[v][i][0] = copia[v][i]; } somatorio[v] = copia_sigma[v]; } void calcula(int v){ for(int i = 1;i<=C;i++){ atual[v][i] = max(dp[v][i][0], dp[v][i-1][0] + somatorio[v] ); } } void dfs1(int v,int p){ for(int u : grafo[v]){ if(u == p) continue; dfs1(u,v); adicona(v,u); } calcula(v); } void dfs2(int v,int p){ calcula(v); faz_copia(v); for(int i = 0;i<=C;i++){ best = max(best,atual[v][i]); } for(int u : grafo[v]){ if(u == p) continue; tira(v,u); calcula(v); adicona(u,v); dfs2(u,v); restaura(v); } } int main(){ scanf("%d %d",&N,&C); for(int i = 1;i<=N;i++){ scanf("%lld",&P[i]); } for(int i = 1;i<N;i++){ int u,v; scanf("%d %d",&u,&v); grafo[u].push_back(v); grafo[v].push_back(u); } dfs1(1,-1); dfs2(1,-1); printf("%lld\n",best); return 0; }

Compilation message (stderr)

chase.cpp: In function 'int main()':
chase.cpp:90:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&N,&C);
  ~~~~~^~~~~~~~~~~~~~~
chase.cpp:92:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld",&P[i]);
   ~~~~~^~~~~~~~~~~~~~
chase.cpp:96:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&u,&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...