Submission #853419

# Submission time Handle Problem Language Result Execution time Memory
853419 2023-09-24T10:14:17 Z manizare Chase (CEOI17_chase) C++14
0 / 100
592 ms 101944 KB
#include <bits/stdc++.h> 
 
#define pb push_back
#define F first
#define S second 
#define all(a) a.begin(),a.end()
#define pii pair <int,int>
#define Pii pair< pii , pii >
#define int long long
 
using namespace std ;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
const int maxn = 2e5 + 10 , maxq = 1e7 + 1 ,  inf = 1e18 + 10 , mod= 998244353 ,lg = 20 ;
int sm[maxn] , dp[maxn][102][2] , w[maxn] , ans = 0, n , k;
vector <int> G[maxn] ; 
vector <pii> vec[102] , vec2[102] , vec3[102];  

void dfs(int v, int p =0){
  sm[v] += w[p] ;
  for(int i =  0 ; i < G[v].size() ; i++){
    int u = G[v][i] ;
    if(u == p)continue ;
    sm[v] += w[u];
    dfs(u , v) ;
  }
  for(int i = 0 ; i <= k ; i++){
    vec[i].clear();vec2[i].clear();vec3[i].clear();
  }
  for(int i =0 ; i < G[v].size() ; i++){
    int u= G[v][i] ;
    if(u==p)continue ;
    for(int i =0 ; i <= k ; i++){
      vec[i].pb({dp[u][i][0] , u}) ;
      dp[v][i][0] = max({dp[v][i][0] , dp[u][i][0] , (i!=0 ?dp[u][i-1][0] + sm[v] - w[p] : -inf)});
    }
    for(int j =0 ; j <= k ; j++){
      vec2[i].pb({dp[u][i][1] , u}) ;
      vec3[i].pb({dp[u][i][1]-w[u]+sm[v] , u}) ;
      if(i!=0)dp[v][i][1] = max(dp[v][i][1] , sm[v]); 
      dp[v][i][1] = max({dp[v][i][1] , dp[u][i][1] , (i!=0 ?dp[u][i-1][1] + sm[v] - w[u]: -inf)});
    }
  }
  for(int i =0 ; i <= k ; i++){
    ans = max({ans , dp[v][i][1] , dp[v][i][0] });
    sort(all(vec[i]));reverse(all(vec[i]));
    sort(all(vec2[i]));reverse(all(vec2[i]));
    sort(all(vec3[i]));reverse(all(vec3[i]));
  }
  for(int i =0 ; i < G[v].size() ; i++){
    int u = G[v][i] ;
    if(u == p)continue ;
    for(int j = 0 ; j <= k ; j++){
      for(int a = 0 ; a < min(2ll , (int)vec[j].size()) ; a++){
        for(int b =0  ; b < min(2ll , (int)vec2[k-j].size()) ; b++){
          if(vec[j][a].S != vec2[k-j][b].S){
            ans = max(ans , vec[j][a].F + vec2[k-j][b].F) ;
          }
        }
      }
      if(j == k || k==j+1 )continue ;
      for(int a = 0 ; a < min(2ll , (int)vec[j].size()) ; a++){
        for(int b =0  ; b < min(2ll , (int)vec3[k-j-1].size()) ; b++){
          if(vec[j][a].S != vec3[k-j-1][b].S){
            ans = max(ans , vec[j][a].F + vec3[k-j-1][b].F) ;
          }
        }
      }
    }
  }
}


signed main(){
  ios::sync_with_stdio(false); cin.tie(0) ;
  cin >> n >> k ;
  for(int i = 1; i <= n ; i++){
    cin >> w[i] ;
  }
  for(int i = 1; i < n; i++){
    int v, u ;
    cin >> v>> u ;
    G[v].pb(u);G[u].pb(v) ; 
  }
  dfs(1 , 0) ;
  cout << ans ; 
}

Compilation message

chase.cpp: In function 'void dfs(long long int, long long int)':
chase.cpp:21:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |   for(int i =  0 ; i < G[v].size() ; i++){
      |                    ~~^~~~~~~~~~~~~
chase.cpp:30:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |   for(int i =0 ; i < G[v].size() ; i++){
      |                  ~~^~~~~~~~~~~~~
chase.cpp:50:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |   for(int i =0 ; i < G[v].size() ; i++){
      |                  ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 9048 KB Output is correct
3 Correct 1 ms 8796 KB Output is correct
4 Incorrect 2 ms 8796 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 9048 KB Output is correct
3 Correct 1 ms 8796 KB Output is correct
4 Incorrect 2 ms 8796 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 592 ms 101944 KB Output is correct
2 Correct 586 ms 101744 KB Output is correct
3 Runtime error 100 ms 27088 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 9048 KB Output is correct
3 Correct 1 ms 8796 KB Output is correct
4 Incorrect 2 ms 8796 KB Output isn't correct
5 Halted 0 ms 0 KB -