답안 #972826

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
972826 2024-05-01T08:36:07 Z efedmrlr Chase (CEOI17_chase) C++17
100 / 100
2794 ms 175960 KB
// #pragma GCC optimize("O3,Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>

using namespace std;


#define int long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int i = 0; (i) < (n); (i)++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()


void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}


const double EPS = 0.00001;
const int INF = 1e9+500;
const int N = 1e5+5;
const int ALPH = 26;
const int LGN = 25;
constexpr int MOD = 1e9+7;
int n,m;
vector<int> p(N, 0), sm(N, 0);
array<vector<int>, 105> dpd, dpu;
vector<vector<int> > adj(N, vector<int>());
vector<int> pr(N, 0);
void dfs(int node, int par) {
    pr[node] = par;
    for(auto c : adj[node]) {
        if(c == par) continue;
        dfs(c, node);
    }
    for(int k = 0; k <= m; k++) {
        for(int c : adj[node]) {
            if(c == par) continue;
            dpd[k][node] = max(dpd[k][node], dpd[k][c]);
        }
    }
    for(int k = 1; k <= m; k++) {
        for(int c : adj[node]) {
            if(c == par) continue;
            dpd[k][node] = max(dpd[k][node], dpd[k - 1][c] + sm[node] - p[c]);
        }
    } 
    for(int k = 0; k <= m; k++) {
        for(int c : adj[node]) {
            if(c == par) continue;
            dpu[k][node] = max(dpu[k][node], dpu[k][c]);
        }
    }
    for(int k = 1; k <= m; k++) {
        for(int c : adj[node]) {
            if(c == par) continue;
            dpu[k][node] = max(dpu[k][node], dpu[k - 1][c] + sm[c] - p[node]);
        }
    }
}

inline void solve() {
    cin>>n>>m;
    for(int i = 1; i <= n; i++) {
        cin >> p[i];
    }
    REP(i, n - 1) {
        int u, v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }

    for(int i = 1; i<=n; i++) {
        for(auto c : adj[i]) {
            sm[i] += p[c];
        }
    }
    REP(i, m + 3) {
        dpd[i].assign(n + 2, 0);
        dpu[i].assign(n + 2, 0);
    }
    for(int i = 1; i <= n; i++) {
        dpd[1][i] = sm[i];
    }
    dfs(1, 0);
    for(int i = 1; i <= n; i++) {
        for(int k = 1; k <= m; k++) {
            dpd[k][i] = max(dpd[k][i], dpd[k - 1][i]);
            dpu[k][i] = max(dpu[k][i], dpu[k - 1][i]);
        }
    }
    // for(int i = 1; i<=n; i++) {
    //     cout << "i:" << i << " dpd:" << dpd[2][i] << "\n";
    // }
    int ans = 0;
    for(int i = 1; i <= n; i++) {
        for(int v = 0; v <= m; v++) {
            int v2 = m - v;
            vector<array<int, 2> > dmax, umax;
            int tmp = 0;
            if(v > 0) tmp = sm[i];
            dmax.pb({tmp, -1}); umax.pb({0, -2});
            dmax.pb({tmp, -4}); umax.pb({0, -3});
            for(auto c : adj[i]) {
                if(pr[i] == c) continue;
                int ret = dpd[v][c];
                if(v > 0) {
                    ret = max(ret, dpd[v - 1][c] + sm[i] - p[c]);
                }
                if(dmax[0][0] < ret) {
                    swap(dmax[0], dmax[1]);
                    dmax[0] = {ret, c};
                }
                else if(dmax[1][0] < ret) {
                    dmax[1] = {ret, c};
                }
                ret = dpu[v2][c];
                if(v2 > 0) {
                    ret = max(ret, dpu[v2 - 1][c] + sm[c] - p[i]);
                }
                if(umax[0][0] < ret) {
                    swap(umax[0], umax[1]);
                    umax[0] = {ret, c};
                }
                else if(umax[1][0] < ret) {
                    umax[1] = {ret, c};
                }
            }
            if(dmax[0][1] == umax[0][1]) swap(dmax[0], dmax[1]);
            ans = max(ans, dmax[0][0] + umax[0][0]);
            ans = max(ans, dmax[1][0] + umax[1][0]);
        }
    }
    cout << ans << "\n";
}
 
signed main() {

    fastio();
    int test = 1;
    //cin>>test;
    while(test--) {
        solve();
    }
    
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4952 KB Output is correct
7 Correct 20 ms 7004 KB Output is correct
8 Correct 4 ms 5208 KB Output is correct
9 Correct 3 ms 5212 KB Output is correct
10 Correct 13 ms 6748 KB Output is correct
11 Correct 6 ms 5724 KB Output is correct
12 Correct 5 ms 5288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2228 ms 173824 KB Output is correct
2 Correct 2303 ms 173812 KB Output is correct
3 Correct 1476 ms 170892 KB Output is correct
4 Correct 105 ms 15176 KB Output is correct
5 Correct 2672 ms 169384 KB Output is correct
6 Correct 2794 ms 172812 KB Output is correct
7 Correct 2778 ms 172812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4952 KB Output is correct
7 Correct 20 ms 7004 KB Output is correct
8 Correct 4 ms 5208 KB Output is correct
9 Correct 3 ms 5212 KB Output is correct
10 Correct 13 ms 6748 KB Output is correct
11 Correct 6 ms 5724 KB Output is correct
12 Correct 5 ms 5288 KB Output is correct
13 Correct 2228 ms 173824 KB Output is correct
14 Correct 2303 ms 173812 KB Output is correct
15 Correct 1476 ms 170892 KB Output is correct
16 Correct 105 ms 15176 KB Output is correct
17 Correct 2672 ms 169384 KB Output is correct
18 Correct 2794 ms 172812 KB Output is correct
19 Correct 2778 ms 172812 KB Output is correct
20 Correct 2733 ms 172808 KB Output is correct
21 Correct 48 ms 15696 KB Output is correct
22 Correct 2782 ms 172920 KB Output is correct
23 Correct 89 ms 17156 KB Output is correct
24 Correct 2735 ms 172880 KB Output is correct
25 Correct 1364 ms 172628 KB Output is correct
26 Correct 2352 ms 175960 KB Output is correct
27 Correct 2274 ms 175960 KB Output is correct