# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
853414 |
2023-09-24T09:45:57 Z |
manizare |
Chase (CEOI17_chase) |
C++14 |
|
1257 ms |
454680 KB |
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#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] ;
void dfs(int v, int p =0){
sm[v] += w[p] ;
vector <pii> vec[102] , vec2[102] , vec3[102];
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++){
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 i =0 ; i <= k ; i++){
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]});
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:22: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]
22 | for(int i = 0 ; i < G[v].size() ; i++){
| ~~^~~~~~~~~~~~~
chase.cpp:28: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]
28 | for(int i =0 ; i < G[v].size() ; i++){
| ~~^~~~~~~~~~~~~
chase.cpp:48: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]
48 | for(int i =0 ; i < G[v].size() ; i++){
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 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 |
4696 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 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 |
4696 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Incorrect |
13 ms |
8536 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1257 ms |
454680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 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 |
4696 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Incorrect |
13 ms |
8536 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |