답안 #598507

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
598507 2022-07-18T12:26:17 Z OttoTheDino Chase (CEOI17_chase) C++17
20 / 100
253 ms 349044 KB
#include <bits/stdc++.h>
using namespace std;
 
#define rep(i,s,e)                  for (ll i = s; i <= e; ++i)
#define rrep(i,s,e)                 for (ll i = s; i >= e; --i)
#define pb                          push_back
#define pf                          push_front
#define fi                          first
#define se                          second
#define all(a)                      a.begin(), a.end()
#define len(a)                      (ll)a.size()
typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<ll, ll> pll;
typedef vector<ii> vii;
typedef vector<ll> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
 
const ll mx = 1e5, mxv = 100;
vi adj[mx+1];
vector<pll> qs;
 
void dfs (int u, int par) {
    for (int v : adj[u]) {
        if (v==par) continue;
        dfs (v, u);
    }
    qs.pb({u,par});
}
 
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    ll p[mx+1], s[mx+1], dpd[mx+1][mxv+1], dpu[mx+1][mxv+1], ans, l;
 
    ll n; cin >> n >> l;
 
    rep (i,1,n) cin >> p[i];
 
    rep (i,1,n-1) {
        ll u, v; cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
 
    rep (i,1,n) for (int v : adj[i]) s[i] += p[v];

    dfs (1,0);

    for (auto [u, par] : qs) {

        set<pll,greater<pll>> dpd_nxt[mxv+1], dpu_here[mxv+1];

        rep (i,0,mxv) {
            if (i) {
                dpd[u][i] = s[u]-p[par];
                dpu[u][i] = s[u];
            }
            dpd_nxt[i].insert({0,0});
            dpu_here[i].insert({dpu[u][i], u});
        }

        for (int v : adj[u]) {
            if (v==par) continue;
            dfs (v, u);

            rep (i,0,mxv) {
                dpd_nxt[i].insert({dpd[v][i], v});
                if (len(dpd_nxt[i])>2) dpd_nxt[i].erase(--dpd_nxt[i].end());
                dpd[u][i] = max(dpd[u][i], dpd[v][i]);
                if (i) {
                    dpd[u][i] = max(dpd[u][i], s[u]-p[par]+dpd[v][i-1]);
                    dpd[u][i] = max(dpd[u][i], dpd[u][i-1]);
                }
            }

            rep (i,0,mxv) {
                dpu_here[i].insert({i==0?dpu[v][i]:max(dpu[v][i], dpu[v][i-1]+s[u]-p[v]), v});
                if (len(dpu_here[i])>2) dpu_here[i].erase(--dpu_here[i].end());
                dpu[u][i] = max(dpu[u][i], dpu[v][i]);
                if (i) {
                    dpu[u][i] = max(dpu[u][i], dpu[v][i-1]+s[u]-p[v]);
                    dpu[u][i] = max(dpu[u][i], dpu[u][i-1]);
                }
            }
        }

        rep (i,0,l) {
            auto it1 = dpu_here[i].begin();
            auto it2 = dpd_nxt[l-i].begin();
            if ((*it1).se==(*it2).se) {
                ++it2;
                ans = max(ans, (*it1).fi+(*it2).fi);
                --it2;
                ++it1;
            }
            ans = max(ans, (*it1).fi+(*it2).fi);
        }
    }

    cout << ans << "\n";
 
    return 0;
}

Compilation message

chase.cpp: In function 'int main()':
chase.cpp:37:62: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
   37 |     ll p[mx+1], s[mx+1], dpd[mx+1][mxv+1], dpu[mx+1][mxv+1], ans, l;
      |                                                              ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 162256 KB Output is correct
2 Correct 62 ms 162308 KB Output is correct
3 Correct 68 ms 162248 KB Output is correct
4 Correct 76 ms 162364 KB Output is correct
5 Correct 63 ms 162252 KB Output is correct
6 Correct 65 ms 162252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 162256 KB Output is correct
2 Correct 62 ms 162308 KB Output is correct
3 Correct 68 ms 162248 KB Output is correct
4 Correct 76 ms 162364 KB Output is correct
5 Correct 63 ms 162252 KB Output is correct
6 Correct 65 ms 162252 KB Output is correct
7 Runtime error 201 ms 329196 KB Execution killed with signal 7
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 253 ms 349044 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 162256 KB Output is correct
2 Correct 62 ms 162308 KB Output is correct
3 Correct 68 ms 162248 KB Output is correct
4 Correct 76 ms 162364 KB Output is correct
5 Correct 63 ms 162252 KB Output is correct
6 Correct 65 ms 162252 KB Output is correct
7 Runtime error 201 ms 329196 KB Execution killed with signal 7
8 Halted 0 ms 0 KB -