# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1026173 |
2024-07-17T16:39:44 Z |
ShaShi |
Chase (CEOI17_chase) |
C++17 |
|
741 ms |
485236 KB |
#include <bits/stdc++.h>
#define int long long
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define kill(x) cout << x << "\n", exit(0)
#define pii pair<int, int>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef long double ld;
const int MAXN = (int)1e5 + 7;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9 + 7;
const int SQ = 107;
int n, m, t, k, tmp, tmp2, tmp3, tmp4, flag, u, v, w, ans;
int arr[MAXN], dp[MAXN][SQ], dp2[MAXN][SQ];
int ps[SQ][2][MAXN], pd[SQ][MAXN], ps2[SQ][2][MAXN], pd2[SQ][MAXN];
vector<int> adj[MAXN], vec[MAXN];
void DFSsz(int v, int p=-1) {
int sum = 0;
for (int u:adj[v]) {
if (u == p) continue;
DFSsz(u, v);
sum += arr[u]; vec[v].pb(u);
}
for (int j=1; j<=k; j++) {
for (int u:vec[v]) {
dp[v][j] = max({dp[v][j], max(dp[u][j], dp2[u][j])});
dp2[v][j] = max({dp2[v][j], max(dp[u][j-1], dp2[u][j-1])+sum});
}
}
for (int j=1; j<=k; j++) {
ps[j][0][0] = ps[j][1][vec[v].size()+1] = ps2[j][0][0] = ps2[j][1][vec[v].size()+1] = 0;
for (int i=1; i<=vec[v].size(); i++) {
int u = vec[v][i-1];
ps[j][0][i] = max(ps[j][0][i-1], max(dp[u][j], dp2[u][j]));
ps2[j][0][i] = max(ps2[j][0][i-1], max(dp[u][j-1], dp2[u][j-1]));
}
for (int i=vec[v].size(); i>=1; i--) {
int u = vec[v][i-1];
ps[j][1][i] = max(ps[j][1][i+1], max(dp[u][j], dp2[u][j]));
ps2[j][1][i] = max(ps2[j][1][i+1], max(dp[u][j-1], dp2[u][j-1]));
}
for (int i=1; i<=vec[v].size(); i++) {
int u = vec[v][i-1];
pd[j][u] = max({ps[j][0][i-1], ps[j][1][i+1]});
pd2[j][u] = max(ps2[j][0][i-1], ps2[j][1][i+1])+sum-arr[u];
}
}
}
void Walk(int v, int p, vector<int> mul, int add=0) {
for (int j=0; j<=k; j++) ans = max({ans, max(dp[v][j], dp2[v][j]+add), mul[j]});
// cout << "! " << v << " " << merge(dp[v], mul).F << " " << merge(dp[v], mul).S << endl;
for (int u:adj[v]) {
if (u == p) continue;
vector<int> mul2(k+1);
for (int j=0; j<=k; j++) mul2[j] = max({mul[j], pd[j][u]});
Walk(u, v, mul2);
}
}
int32_t main() {
#ifdef LOCAL
freopen("inp.in", "r", stdin);
freopen("res.out", "w", stdout);
#else
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
cin >> n >> k;
for (int i=1; i<=n; i++) cin >> arr[i];
for (int i=1; i<n; i++) {
cin >> u >> v;
adj[u].pb(v); adj[v].pb(u);
}
DFSsz(1);
Walk(1, 0, vector<int>(k+1, 0));
cout << ans << endl;
return 0;
}
Compilation message
chase.cpp: In function 'void DFSsz(long long int, long long int)':
chase.cpp:62:24: 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]
62 | for (int i=1; i<=vec[v].size(); i++) {
| ~^~~~~~~~~~~~~~~
chase.cpp:76:24: 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]
76 | for (int i=1; i<=vec[v].size(); i++) {
| ~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
25180 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
25180 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
605 ms |
398016 KB |
Output is correct |
2 |
Correct |
626 ms |
399440 KB |
Output is correct |
3 |
Correct |
411 ms |
485236 KB |
Output is correct |
4 |
Correct |
122 ms |
161064 KB |
Output is correct |
5 |
Correct |
741 ms |
366676 KB |
Output is correct |
6 |
Correct |
700 ms |
377608 KB |
Output is correct |
7 |
Correct |
724 ms |
379152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
25180 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |