답안 #794902

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
794902 2023-07-27T02:16:00 Z PurpleCrayon Designated Cities (JOI19_designated_cities) C++17
100 / 100
1866 ms 44560 KB
#include <bits/stdc++.h>
using namespace std;
 
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 2e5+10, MOD = 1e9+7;
const ll INF = 1e18+10;
 
int n;
vector<ar<int, 3>> adj[N];
ll ans[N];
 
ll calc_one(int c, int p) {
    ll sum = 0;
    for (auto [nxt, w, _] : adj[c]) if (nxt != p) {
        sum += w + calc_one(nxt, c);
    }
    return sum;
}
 
void dfs1(int c, int p, ll x) {
    ans[1] = min(ans[1], x);
    for (auto [nxt, a, b] : adj[c]) if (nxt != p) {
        dfs1(nxt, c, x - a + b);
    }
}
 
int sub[N];
bool blocked[N];
 
int dfs_sub(int c, int p) {
    sub[c] = 1;
    for (auto [nxt, x, y] : adj[c]) if (nxt != p && !blocked[nxt]) {
        sub[c] += dfs_sub(nxt, c);
    }
 
    return sub[c];
}
 
int dfs_cent(int c, int p, int cnt) {
    for (auto [nxt, x, y] : adj[c]) if (nxt != p && !blocked[nxt] && sub[nxt] > cnt / 2)
        return dfs_cent(nxt, c, cnt);
 
    return c;
}
 
pair<ll, int> dfs_best(int c, int p, ll x) {
    pair<ll, int> best{x, c};
    for (auto [nxt, w, _] : adj[c]) if (!blocked[nxt] && nxt != p) {
        best = max(best, dfs_best(nxt, c, x + w));
    }
 
    return best;
}
 
vector<int> roots;
int process(int c) {
    if (sz(adj[c]) == 1) roots.push_back(c); // can't hurt, and probably useful
 
    ll best_w = -1;
    int best_nxt = -1, best_c = -1;
 
    for (auto [nxt, w, _] : adj[c]) if (!blocked[nxt]) {
        auto cur = dfs_best(nxt, c, w);
        if (cur.first > best_w) {
            best_w = cur.first;
            best_nxt = nxt;
            best_c = cur.second;
        }
    }
 
    if (best_c != -1) {
        roots.push_back(best_c);
    }
    return best_nxt;
}
 
void build(int c) {
    if (c == -1) return;
    int cnt = dfs_sub(c, -1);
    int cent = dfs_cent(c, -1, cnt);
 
    int nxt = process(cent);
    blocked[cent] = 1;
    build(nxt);
}
 
 
int par[N], par_w[N], st[N], en[N], et[N], tt;
ll sum[N];
bool marked[N];
 
ll lzy[4 * N];
pair<ll, int> t[4 * N];
 
void build(int v, int tl, int tr) {
    lzy[v] = 0;
    if (tl == tr) t[v] = {sum[tl], tl};
    else {
        int tm = (tl + tr) / 2;
        build(2*v, tl, tm), build(2*v+1, tm+1, tr);
        t[v] = max(t[2*v], t[2*v+1]);
    }
}
 
void push(int v, int tl, int tr, ll x) {
    if (!x) return;
    t[v].first += x;
    if (tl != tr)
        lzy[2*v] += x, lzy[2*v+1] += x;
}
 
void app(int v, int tl, int tr) {
    push(v, tl, tr, lzy[v]);
    lzy[v] = 0;
}
 
void upd(int v, int tl, int tr, int l, int r, ll x) {
    app(v, tl, tr);
    if (r < tl || l > tr) return;
    if (l <= tl && tr <= r) {
        push(v, tl, tr, x);
        return;
    }
 
    int tm = (tl + tr) / 2;
    upd(2*v, tl, tm, l, r, x), upd(2*v+1, tm+1, tr, l, r, x);
    t[v] = max(t[2*v], t[2*v+1]);
}
 
pair<ll, int> qry(int v, int tl, int tr, int l, int r) {
    app(v, tl, tr);
    if (r < tl || l > tr) return {-INF, -1};
    if (l <= tl && tr <= r) return t[v];
    int tm = (tl + tr) / 2;
    return max(qry(2*v, tl, tm, l, r), qry(2*v+1, tm+1, tr, l, r));
}
 
 
void dfs_root(int c, int p) {
    par[c] = p;
    et[st[c] = tt++] = c;
    for (auto [nxt, w, _] : adj[c]) if (nxt != p) {
        par_w[nxt] = w;
        dfs_root(nxt, c);
    }
    en[c] = tt-1;
}
 
void add(int root) {
    tt = 0;
    memset(par, -1, sizeof(par));
    memset(sum, 0, sizeof(sum));
    memset(par_w, 0, sizeof(par_w));
 
    dfs_root(root, -1);
    for (int i = 1; i < n; i++) {
        int c = et[i];
        sum[i] = sum[st[par[c]]] + par_w[c];
    }
 
    memset(marked, 0, sizeof(marked));
    ll cur = accumulate(par_w, par_w + n, 0LL);
    build(1, 0, n-1);
    for (int i = 1; i <= n; i++) {
        /*
        pair<ll, int> best{-1, -1};
        for (int c = 0; c < n; c++) best = max(best, {sum[c], c});
        */
        pair<ll, int> best = qry(1, 0, n-1, 0, n-1);
 
        int c = et[best.second];
        while (c != -1 && !marked[c]) {
            marked[c] = 1;
            upd(1, 0, n-1, st[c], en[c],  -par_w[c]);
            // for (int j = st[c]; j <= en[c]; j++) sum[j] -= par_w[c];
            cur -= par_w[c];
            c = par[c];
        }
 
        ans[i+1] = min(ans[i+1], cur);
    }
}
 
void solve() {
    cin >> n;
    for (int i = 0; i < n-1; i++) {
        int a, b; cin >> a >> b, --a, --b;
        int x, y; cin >> x >> y;
        adj[a].push_back({b, x, y});
        adj[b].push_back({a, y, x});
    }
 
    fill(ans, ans + n, INF);
    dfs1(0, -1, calc_one(0, -1));
 
    build(0);
    sort(roots.begin(), roots.end());
    roots.resize(unique(roots.begin(), roots.end()) - roots.begin());
    for (int c : roots) add(c);
 
    int q; cin >> q;
    while (q--) {
        int x; cin >> x;
        cout << ans[x] << '\n';
    }
}
 
int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8276 KB Output is correct
2 Correct 3 ms 8276 KB Output is correct
3 Correct 3 ms 8276 KB Output is correct
4 Correct 4 ms 8388 KB Output is correct
5 Correct 3 ms 8276 KB Output is correct
6 Correct 3 ms 8276 KB Output is correct
7 Correct 4 ms 8276 KB Output is correct
8 Correct 4 ms 8276 KB Output is correct
9 Correct 3 ms 8276 KB Output is correct
10 Correct 4 ms 8276 KB Output is correct
11 Correct 3 ms 8276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8276 KB Output is correct
2 Correct 655 ms 33648 KB Output is correct
3 Correct 1036 ms 44236 KB Output is correct
4 Correct 244 ms 33612 KB Output is correct
5 Correct 247 ms 33432 KB Output is correct
6 Correct 941 ms 35228 KB Output is correct
7 Correct 238 ms 33400 KB Output is correct
8 Correct 1093 ms 43836 KB Output is correct
9 Correct 201 ms 33876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8276 KB Output is correct
2 Correct 1030 ms 33664 KB Output is correct
3 Correct 1040 ms 44196 KB Output is correct
4 Correct 302 ms 33588 KB Output is correct
5 Correct 477 ms 33476 KB Output is correct
6 Correct 1006 ms 35668 KB Output is correct
7 Correct 343 ms 33804 KB Output is correct
8 Correct 1866 ms 41020 KB Output is correct
9 Correct 272 ms 33912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8276 KB Output is correct
2 Correct 3 ms 8276 KB Output is correct
3 Correct 3 ms 8276 KB Output is correct
4 Correct 4 ms 8388 KB Output is correct
5 Correct 3 ms 8276 KB Output is correct
6 Correct 3 ms 8276 KB Output is correct
7 Correct 4 ms 8276 KB Output is correct
8 Correct 4 ms 8276 KB Output is correct
9 Correct 3 ms 8276 KB Output is correct
10 Correct 4 ms 8276 KB Output is correct
11 Correct 3 ms 8276 KB Output is correct
12 Correct 5 ms 8276 KB Output is correct
13 Correct 6 ms 8532 KB Output is correct
14 Correct 7 ms 8660 KB Output is correct
15 Correct 6 ms 8532 KB Output is correct
16 Correct 6 ms 8532 KB Output is correct
17 Correct 5 ms 8620 KB Output is correct
18 Correct 9 ms 8580 KB Output is correct
19 Correct 5 ms 8532 KB Output is correct
20 Correct 6 ms 8532 KB Output is correct
21 Correct 7 ms 8532 KB Output is correct
22 Correct 6 ms 8532 KB Output is correct
23 Correct 5 ms 8532 KB Output is correct
24 Correct 5 ms 8532 KB Output is correct
25 Correct 8 ms 8660 KB Output is correct
26 Correct 5 ms 8652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8276 KB Output is correct
2 Correct 655 ms 33648 KB Output is correct
3 Correct 1036 ms 44236 KB Output is correct
4 Correct 244 ms 33612 KB Output is correct
5 Correct 247 ms 33432 KB Output is correct
6 Correct 941 ms 35228 KB Output is correct
7 Correct 238 ms 33400 KB Output is correct
8 Correct 1093 ms 43836 KB Output is correct
9 Correct 201 ms 33876 KB Output is correct
10 Correct 4 ms 8276 KB Output is correct
11 Correct 1030 ms 33664 KB Output is correct
12 Correct 1040 ms 44196 KB Output is correct
13 Correct 302 ms 33588 KB Output is correct
14 Correct 477 ms 33476 KB Output is correct
15 Correct 1006 ms 35668 KB Output is correct
16 Correct 343 ms 33804 KB Output is correct
17 Correct 1866 ms 41020 KB Output is correct
18 Correct 272 ms 33912 KB Output is correct
19 Correct 3 ms 8276 KB Output is correct
20 Correct 319 ms 33648 KB Output is correct
21 Correct 1085 ms 44104 KB Output is correct
22 Correct 513 ms 33612 KB Output is correct
23 Correct 1004 ms 33832 KB Output is correct
24 Correct 268 ms 33660 KB Output is correct
25 Correct 950 ms 33552 KB Output is correct
26 Correct 311 ms 33664 KB Output is correct
27 Correct 519 ms 33404 KB Output is correct
28 Correct 983 ms 35332 KB Output is correct
29 Correct 533 ms 33568 KB Output is correct
30 Correct 273 ms 33608 KB Output is correct
31 Correct 367 ms 33484 KB Output is correct
32 Correct 1081 ms 41356 KB Output is correct
33 Correct 212 ms 33912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8276 KB Output is correct
2 Correct 3 ms 8276 KB Output is correct
3 Correct 3 ms 8276 KB Output is correct
4 Correct 4 ms 8388 KB Output is correct
5 Correct 3 ms 8276 KB Output is correct
6 Correct 3 ms 8276 KB Output is correct
7 Correct 4 ms 8276 KB Output is correct
8 Correct 4 ms 8276 KB Output is correct
9 Correct 3 ms 8276 KB Output is correct
10 Correct 4 ms 8276 KB Output is correct
11 Correct 3 ms 8276 KB Output is correct
12 Correct 4 ms 8276 KB Output is correct
13 Correct 655 ms 33648 KB Output is correct
14 Correct 1036 ms 44236 KB Output is correct
15 Correct 244 ms 33612 KB Output is correct
16 Correct 247 ms 33432 KB Output is correct
17 Correct 941 ms 35228 KB Output is correct
18 Correct 238 ms 33400 KB Output is correct
19 Correct 1093 ms 43836 KB Output is correct
20 Correct 201 ms 33876 KB Output is correct
21 Correct 4 ms 8276 KB Output is correct
22 Correct 1030 ms 33664 KB Output is correct
23 Correct 1040 ms 44196 KB Output is correct
24 Correct 302 ms 33588 KB Output is correct
25 Correct 477 ms 33476 KB Output is correct
26 Correct 1006 ms 35668 KB Output is correct
27 Correct 343 ms 33804 KB Output is correct
28 Correct 1866 ms 41020 KB Output is correct
29 Correct 272 ms 33912 KB Output is correct
30 Correct 5 ms 8276 KB Output is correct
31 Correct 6 ms 8532 KB Output is correct
32 Correct 7 ms 8660 KB Output is correct
33 Correct 6 ms 8532 KB Output is correct
34 Correct 6 ms 8532 KB Output is correct
35 Correct 5 ms 8620 KB Output is correct
36 Correct 9 ms 8580 KB Output is correct
37 Correct 5 ms 8532 KB Output is correct
38 Correct 6 ms 8532 KB Output is correct
39 Correct 7 ms 8532 KB Output is correct
40 Correct 6 ms 8532 KB Output is correct
41 Correct 5 ms 8532 KB Output is correct
42 Correct 5 ms 8532 KB Output is correct
43 Correct 8 ms 8660 KB Output is correct
44 Correct 5 ms 8652 KB Output is correct
45 Correct 3 ms 8276 KB Output is correct
46 Correct 319 ms 33648 KB Output is correct
47 Correct 1085 ms 44104 KB Output is correct
48 Correct 513 ms 33612 KB Output is correct
49 Correct 1004 ms 33832 KB Output is correct
50 Correct 268 ms 33660 KB Output is correct
51 Correct 950 ms 33552 KB Output is correct
52 Correct 311 ms 33664 KB Output is correct
53 Correct 519 ms 33404 KB Output is correct
54 Correct 983 ms 35332 KB Output is correct
55 Correct 533 ms 33568 KB Output is correct
56 Correct 273 ms 33608 KB Output is correct
57 Correct 367 ms 33484 KB Output is correct
58 Correct 1081 ms 41356 KB Output is correct
59 Correct 212 ms 33912 KB Output is correct
60 Correct 4 ms 8328 KB Output is correct
61 Correct 863 ms 35048 KB Output is correct
62 Correct 806 ms 44560 KB Output is correct
63 Correct 380 ms 35044 KB Output is correct
64 Correct 590 ms 35104 KB Output is correct
65 Correct 317 ms 34784 KB Output is correct
66 Correct 549 ms 34952 KB Output is correct
67 Correct 324 ms 34824 KB Output is correct
68 Correct 384 ms 34940 KB Output is correct
69 Correct 1262 ms 36780 KB Output is correct
70 Correct 635 ms 34724 KB Output is correct
71 Correct 274 ms 35100 KB Output is correct
72 Correct 278 ms 35572 KB Output is correct
73 Correct 1311 ms 42376 KB Output is correct
74 Correct 220 ms 36880 KB Output is correct