Submission #853430

# Submission time Handle Problem Language Result Execution time Memory
853430 2023-09-24T10:46:52 Z manizare Chase (CEOI17_chase) C++14
50 / 100
194 ms 131940 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]) ;
    }
  }
}


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 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 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 4696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 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 4696 KB Output is correct
7 Correct 2 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Incorrect 2 ms 4700 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 148 ms 94288 KB Output is correct
2 Correct 159 ms 94192 KB Output is correct
3 Correct 106 ms 9572 KB Output is correct
4 Correct 100 ms 121004 KB Output is correct
5 Correct 182 ms 131924 KB Output is correct
6 Correct 194 ms 131940 KB Output is correct
7 Correct 181 ms 131348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 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 4696 KB Output is correct
7 Correct 2 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Incorrect 2 ms 4700 KB Output isn't correct
10 Halted 0 ms 0 KB -