# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
61851 | IvanC | Chase (CEOI17_chase) | C++17 | 805 ms | 358920 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |