답안 #925979

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
925979 2024-02-12T12:14:39 Z Whisper Dostavljač (COCI18_dostavljac) C++17
140 / 140
290 ms 4512 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using T = tuple<ll, ll, ll>;

#define int long long
#define Base 41
#define sz(a) (int)a.size()
#define FOR(i, a, b) for ( int i = a ; i <= b ; i++ )
#define FORD(i, a, b) for (int i = b; i >= a; i --)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPD(i, n) for (int i = n - 1; i >= 0; --i)
#define all(x) x.begin() , x.end()
#define pii pair<int , int>
#define fi first
#define se second
#define Lg(x) 31 - __builtin_clz(x)

#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)

constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int MAX = 5e2 + 5;
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void setupIO(){
    #define name "Whisper"
    //Phu Trong from Nguyen Tat Thanh High School for gifted student
    srand(time(NULL));
    cin.tie(nullptr)->sync_with_stdio(false); cout.tie(nullptr);
    //freopen(name".inp", "r", stdin);
    //freopen(name".out", "w", stdout);
    cout << fixed << setprecision(10);
}

template <class X, class Y>
    bool minimize(X &x, const Y &y){
        X eps = 1e-9;
        if (x > y + eps) {x = y; return 1;}
        return 0;
    }

template <class X, class Y>
    bool maximize(X &x, const Y &y){
        X eps = 1e-9;
        if (x + eps < y) {x = y; return 1;}
        return 0;
    }
int numNode, numTime;
int a[MAX];
vector<int> adj[MAX];
int dp[MAX][MAX][2];

void dfs(int u, int p){
    for (int i = 1; i <= numTime; ++i) dp[u][i][1] = dp[u][i][0] = a[u];
    for (auto&v : adj[u]) if(v ^ p){
        dfs(v, u);
        REPD(i, numTime + 1) REPD(j, numTime + 1){
            if (i >= j + 1){
                maximize(dp[u][i][0], dp[u][i - j - 1][1] + max(dp[v][j][0], dp[v][j][1]));
            }
            if (i >= j + 2){
                maximize(dp[u][i][0], dp[v][j][1] + dp[u][i - j - 2][0]);
                maximize(dp[u][i][1], dp[v][j][1] + dp[u][i - j - 2][1]);
            }
        }
    }
}
void Whisper(){
    cin >> numNode >> numTime;
    for (int i = 1; i <= numNode; ++i) cin >> a[i];
    for (int i = 1; i < numNode; ++i){
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(1, 0);
    int ans = 0;
    for (int i = 1; i <= numTime; ++i){
        maximize(ans, dp[1][i][0]);
        maximize(ans, dp[1][i][1]);
    }
    cout << ans;

    /*
        let dp[u][k][0] is the maximum pepper if we start the path at u and end in u's subtree
        let dp[u][k][1] is the maximum pepper if we start the path at u and also end at u

        1. dp[u][k][0] = dp[u][k][1] = a[u] with all k <= numTime
        2.
            maximize(dp[u][i][0], dp[u][i - j - 1][1] + max(dp[v][j][0], dp[v][j][1]))
            (we end our path in the subtree v)
        3.
            maximize(dp[u][i][0], dp[v][j][1] + dp[u][i - j - 2][0])
            (we end our path inside another subtree)
            maximize(dp[u][i][1], dp[v][j][1] + dp[u][i - j - 2][1])
            (we can go through v's subtree)

        4. the answer is maximum dp[1][0][i], dp[1][1][i] (0 <= i <= numTime)
    */
}


signed main(){
    setupIO();
    int Test = 1;
//    cin >> Test;
    for ( int i = 1 ; i <= Test ; i++ ){
        Whisper();
        if (i < Test) cout << '\n';
    }
}

# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 2652 KB Output is correct
2 Correct 3 ms 2648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2652 KB Output is correct
2 Correct 41 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 2640 KB Output is correct
2 Correct 23 ms 2704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 2952 KB Output is correct
2 Correct 152 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 134 ms 3512 KB Output is correct
2 Correct 53 ms 3412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 290 ms 4512 KB Output is correct
2 Correct 26 ms 3932 KB Output is correct