Submission #853433

# Submission time Handle Problem Language Result Execution time Memory
853433 2023-09-24T10:49:04 Z manizare Chase (CEOI17_chase) C++14
50 / 100
191 ms 131924 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 = 1e5 + 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] ; 
int mx[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 < G[v].size() ; i++){
    int u= G[v][i] ;
    if(u==p)continue ;
    for(int i =0 ; i <= k ; i++){
      if(i!=0)ans = max(ans , dp[u][i-1][0] + sm[v]) ;
      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 i =0 ; i <= k ; i++){
      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)});
    }
  }
  ans = max(ans , dp[v][k][1]) ;
  ans = max(ans , sm[v]) ;
  mx[0] =-1;
  for(int i = 0 ; i < G[v].size() ; i++){
    int u = G[v][i] ;
    if(u == p)continue ;
    if(mx[0]!=-1){
      for(int i = 1 ; i <k ; i++){
        ans = max(ans , mx[k-i] + dp[u][i][1]) ;
      }
      if(k > 2){
        for(int i = 2;  i < k ; i++){
          ans = max(ans,mx[k-i] + dp[u][i-1][1] + sm[v] - w[u]) ;
        }
      }
    }
    for(int i =0 ; i <= k ; i++){
      mx[i] = max(mx[i] , dp[u][i][0]) ;
    }
    for(int i = 1; i <= k  ;i++){
      mx[i] = max(mx[i] , mx[i-1]) ;
    }
  }
}


signed main(){
  ios::sync_with_stdio(false); cin.tie(0) ;
//  freopen("chase.03.03.in" , "r" , stdin) ;

  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) ; 
  }
  if(k ==0 ){
    cout << 0 ;
    return 0 ;
  }
  dfs(1 , 0) ;
  cout << ans ; 
}

Compilation message

chase.cpp: In function 'void dfs(long long int, long long int)':
chase.cpp:20: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]
   20 |   for(int i =  0 ; i < G[v].size() ; i++){
      |                    ~~^~~~~~~~~~~~~
chase.cpp:26: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]
   26 |   for(int i =0 ; i < G[v].size() ; i++){
      |                  ~~^~~~~~~~~~~~~
chase.cpp:41:21: 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]
   41 |   for(int i = 0 ; i < G[v].size() ; i++){
      |                   ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4696 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4696 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 2 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Incorrect 1 ms 4700 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 164 ms 94180 KB Output is correct
2 Correct 164 ms 94104 KB Output is correct
3 Correct 112 ms 9676 KB Output is correct
4 Correct 98 ms 121060 KB Output is correct
5 Correct 191 ms 131924 KB Output is correct
6 Correct 190 ms 131668 KB Output is correct
7 Correct 191 ms 131504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4696 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 2 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Incorrect 1 ms 4700 KB Output isn't correct
10 Halted 0 ms 0 KB -