답안 #202976

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
202976 2020-02-18T20:11:41 Z tri Election Campaign (JOI15_election_campaign) C++14
100 / 100
479 ms 26744 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define pb push_back
#define f first
#define s second

namespace debug {
    const int DEBUG = true;

    template<class T1, class T2>
    void pr(const pair<T1, T2> &x);

    template<class T, size_t SZ>
    void pr(const array<T, SZ> &x);

    template<class T>
    void pr(const vector<T> &x);

    template<class T>
    void pr(const set<T> &x);

    template<class T1, class T2>
    void pr(const map<T1, T2> &x);

    template<class T>
    void pr(const T &x) { if (DEBUG) cout << x; }

    template<class T, class... Ts>
    void pr(const T &first, const Ts &... rest) { pr(first), pr(rest...); }

    template<class T1, class T2>
    void pr(const pair<T1, T2> &x) { pr("{", x.f, ", ", x.s, "}"); }

    template<class T>
    void prIn(const T &x) {
        pr("{");
        bool fst = 1;
        for (auto &a : x) {
            pr(fst ? "" : ", ", a), fst = 0;
        }
        pr("}");
    }

    template<class T, size_t SZ>
    void pr(const array<T, SZ> &x) { prIn(x); }

    template<class T>
    void pr(const vector<T> &x) { prIn(x); }

    template<class T>
    void pr(const set<T> &x) { prIn(x); }

    template<class T1, class T2>
    void pr(const map<T1, T2> &x) { prIn(x); }

    void ps() { pr("\n"), cout << flush; }

    template<class Arg, class... Args>
    void ps(const Arg &first, const Args &... rest) {
        pr(first, " ");
        ps(rest...);
    }
}
using namespace debug;


const int MAXN = 1e5 + 10;

struct SegTr {
    ll tr[4 * MAXN];

    ll q(int i, int l, int r, int s, int e) {
        if (e < l || r < s) {
            return 0;
        }
        if (s <= l && r <= e) {
            return tr[i];
        }
        int mid = (l + r) / 2;
        return q(i * 2, l, mid, s, e) + q(i * 2 + 1, mid + 1, r, s, e);
    }

    void u(int i, int l, int r, int x, ll d) {
        if (l == r) {
            tr[i] = d;
            return;
        }
        int mid = (l + r) / 2;
        if (x <= mid) {
            u(i * 2, l, mid, x, d);
        } else {
            u(i * 2 + 1, mid + 1, r, x, d);
        }
        tr[i] = tr[i * 2] + tr[i * 2 + 1];
    }
};

int N, rt;

vi aL[MAXN];

int p[MAXN], d[MAXN], hC[MAXN], cS[MAXN];

SegTr tr;
int tI[MAXN];

int initDFS(int cV) {
    int cCnt = 1, mSCnt = 0;

    for (int aV : aL[cV]) {
        if (aV != p[cV]) {
            p[aV] = cV;
            d[aV] = d[cV] + 1;

            int aCnt = initDFS(aV);
            if (aCnt > mSCnt) {
                mSCnt = aCnt;
                hC[cV] = aV;
            }
            cCnt += aCnt;
        }
    }
    return cCnt;
}

void init() {
    fill(hC, hC + MAXN, -1);
    p[rt] = -1;
    initDFS(rt);

    int nTI = 0;
    for (int curS = 0; curS < N; curS++) {
        if (curS == rt || hC[p[curS]] != curS) {
            for (int cV = curS; cV != -1; cV = hC[cV]) {
                cS[cV] = curS;
                tI[cV] = nTI++;
            }
        }
    }
}

ll query(int a, int b) {
    ll sum = 0;
    while (cS[a] != cS[b]) {
        if (d[cS[a]] < d[cS[b]]) swap(a, b);

        sum += tr.q(1, 0, N - 1, tI[cS[a]], tI[a]);
        a = p[cS[a]];
    }

    if (tI[a] > tI[b]) swap(a, b);

    sum += tr.q(1, 0, N - 1, tI[a], tI[b]);
    return sum;
}

void update(int cV, ll nV) {
    tr.u(1, 0, N - 1, tI[cV], nV);
}

int lca(int a, int b) {
    while (cS[a] != cS[b]) {
        if (d[cS[a]] < d[cS[b]]) swap(a, b);

        a = p[cS[a]];
    }

    if (tI[a] > tI[b]) swap(a, b);

    return a;
}

vector<pair<pi, int>> ops[MAXN];
ll dp[MAXN];

void compute(int cV) {
    ll chSum = 0;
    for (int aV : aL[cV]) {
        if (aV != p[cV]) {
            compute(aV);
            chSum += dp[aV];
        }
    }

    ll cBest = chSum;
    for (auto x : ops[cV]) {
        ll cVal = query(x.f.f, x.f.s) + x.s + chSum;
        cBest = max(cBest, cVal);
    }

    dp[cV] = cBest;
    update(cV, chSum - cBest);
//    ps("stored", cV, chSum - cBest);
}

int main() {
    cin >> N;
    for (int i = 0; i < N - 1; i++) {
        int x, y;
        cin >> x >> y;
        x--, y--;
        aL[x].pb(y);
        aL[y].pb(x);
    }

    rt = 0;
    init();

    memset(tr.tr, 0, sizeof(tr.tr));

    int M;
    cin >> M;

    for (int i = 0; i < M; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        a--, b--;
        int cLCA = lca(a, b);
        ops[cLCA].pb({{a, b}, c});
    }

    compute(rt);
//
//    for (int i = 0; i < N; i++) {
//        ps(i, dp[i]);
//    }
//

    ll ans = dp[rt];
    cout << ans << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 8568 KB Output is correct
2 Correct 10 ms 8568 KB Output is correct
3 Correct 10 ms 8568 KB Output is correct
4 Correct 14 ms 8664 KB Output is correct
5 Correct 181 ms 15480 KB Output is correct
6 Correct 127 ms 22996 KB Output is correct
7 Correct 144 ms 20216 KB Output is correct
8 Correct 140 ms 15736 KB Output is correct
9 Correct 152 ms 18808 KB Output is correct
10 Correct 142 ms 15608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 8568 KB Output is correct
2 Correct 10 ms 8568 KB Output is correct
3 Correct 11 ms 8824 KB Output is correct
4 Correct 271 ms 26488 KB Output is correct
5 Correct 266 ms 26360 KB Output is correct
6 Correct 259 ms 26360 KB Output is correct
7 Correct 271 ms 26360 KB Output is correct
8 Correct 266 ms 26364 KB Output is correct
9 Correct 264 ms 26360 KB Output is correct
10 Correct 274 ms 26360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 8568 KB Output is correct
2 Correct 10 ms 8568 KB Output is correct
3 Correct 11 ms 8824 KB Output is correct
4 Correct 271 ms 26488 KB Output is correct
5 Correct 266 ms 26360 KB Output is correct
6 Correct 259 ms 26360 KB Output is correct
7 Correct 271 ms 26360 KB Output is correct
8 Correct 266 ms 26364 KB Output is correct
9 Correct 264 ms 26360 KB Output is correct
10 Correct 274 ms 26360 KB Output is correct
11 Correct 38 ms 9592 KB Output is correct
12 Correct 293 ms 26592 KB Output is correct
13 Correct 279 ms 26616 KB Output is correct
14 Correct 271 ms 26616 KB Output is correct
15 Correct 304 ms 26616 KB Output is correct
16 Correct 268 ms 26616 KB Output is correct
17 Correct 284 ms 26656 KB Output is correct
18 Correct 275 ms 26616 KB Output is correct
19 Correct 271 ms 26616 KB Output is correct
20 Correct 288 ms 26616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 456 ms 18096 KB Output is correct
2 Correct 255 ms 26488 KB Output is correct
3 Correct 319 ms 23288 KB Output is correct
4 Correct 309 ms 18608 KB Output is correct
5 Correct 303 ms 22776 KB Output is correct
6 Correct 312 ms 18536 KB Output is correct
7 Correct 329 ms 22456 KB Output is correct
8 Correct 372 ms 18452 KB Output is correct
9 Correct 259 ms 26364 KB Output is correct
10 Correct 325 ms 21400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 8568 KB Output is correct
2 Correct 10 ms 8568 KB Output is correct
3 Correct 10 ms 8568 KB Output is correct
4 Correct 14 ms 8664 KB Output is correct
5 Correct 181 ms 15480 KB Output is correct
6 Correct 127 ms 22996 KB Output is correct
7 Correct 144 ms 20216 KB Output is correct
8 Correct 140 ms 15736 KB Output is correct
9 Correct 152 ms 18808 KB Output is correct
10 Correct 142 ms 15608 KB Output is correct
11 Correct 12 ms 8696 KB Output is correct
12 Correct 12 ms 8696 KB Output is correct
13 Correct 13 ms 8696 KB Output is correct
14 Correct 13 ms 8696 KB Output is correct
15 Correct 12 ms 8696 KB Output is correct
16 Correct 12 ms 8700 KB Output is correct
17 Correct 12 ms 8696 KB Output is correct
18 Correct 12 ms 8696 KB Output is correct
19 Correct 12 ms 8696 KB Output is correct
20 Correct 12 ms 8696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 8568 KB Output is correct
2 Correct 10 ms 8568 KB Output is correct
3 Correct 10 ms 8568 KB Output is correct
4 Correct 14 ms 8664 KB Output is correct
5 Correct 181 ms 15480 KB Output is correct
6 Correct 127 ms 22996 KB Output is correct
7 Correct 144 ms 20216 KB Output is correct
8 Correct 140 ms 15736 KB Output is correct
9 Correct 152 ms 18808 KB Output is correct
10 Correct 142 ms 15608 KB Output is correct
11 Correct 10 ms 8568 KB Output is correct
12 Correct 10 ms 8568 KB Output is correct
13 Correct 11 ms 8824 KB Output is correct
14 Correct 271 ms 26488 KB Output is correct
15 Correct 266 ms 26360 KB Output is correct
16 Correct 259 ms 26360 KB Output is correct
17 Correct 271 ms 26360 KB Output is correct
18 Correct 266 ms 26364 KB Output is correct
19 Correct 264 ms 26360 KB Output is correct
20 Correct 274 ms 26360 KB Output is correct
21 Correct 38 ms 9592 KB Output is correct
22 Correct 293 ms 26592 KB Output is correct
23 Correct 279 ms 26616 KB Output is correct
24 Correct 271 ms 26616 KB Output is correct
25 Correct 304 ms 26616 KB Output is correct
26 Correct 268 ms 26616 KB Output is correct
27 Correct 284 ms 26656 KB Output is correct
28 Correct 275 ms 26616 KB Output is correct
29 Correct 271 ms 26616 KB Output is correct
30 Correct 288 ms 26616 KB Output is correct
31 Correct 456 ms 18096 KB Output is correct
32 Correct 255 ms 26488 KB Output is correct
33 Correct 319 ms 23288 KB Output is correct
34 Correct 309 ms 18608 KB Output is correct
35 Correct 303 ms 22776 KB Output is correct
36 Correct 312 ms 18536 KB Output is correct
37 Correct 329 ms 22456 KB Output is correct
38 Correct 372 ms 18452 KB Output is correct
39 Correct 259 ms 26364 KB Output is correct
40 Correct 325 ms 21400 KB Output is correct
41 Correct 12 ms 8696 KB Output is correct
42 Correct 12 ms 8696 KB Output is correct
43 Correct 13 ms 8696 KB Output is correct
44 Correct 13 ms 8696 KB Output is correct
45 Correct 12 ms 8696 KB Output is correct
46 Correct 12 ms 8700 KB Output is correct
47 Correct 12 ms 8696 KB Output is correct
48 Correct 12 ms 8696 KB Output is correct
49 Correct 12 ms 8696 KB Output is correct
50 Correct 12 ms 8696 KB Output is correct
51 Correct 398 ms 18748 KB Output is correct
52 Correct 281 ms 26616 KB Output is correct
53 Correct 369 ms 21936 KB Output is correct
54 Correct 313 ms 18792 KB Output is correct
55 Correct 479 ms 18608 KB Output is correct
56 Correct 286 ms 26616 KB Output is correct
57 Correct 327 ms 22624 KB Output is correct
58 Correct 327 ms 18736 KB Output is correct
59 Correct 393 ms 18812 KB Output is correct
60 Correct 277 ms 26616 KB Output is correct
61 Correct 322 ms 22776 KB Output is correct
62 Correct 328 ms 18512 KB Output is correct
63 Correct 468 ms 18376 KB Output is correct
64 Correct 279 ms 26616 KB Output is correct
65 Correct 338 ms 22448 KB Output is correct
66 Correct 309 ms 18868 KB Output is correct
67 Correct 474 ms 18540 KB Output is correct
68 Correct 286 ms 26744 KB Output is correct
69 Correct 315 ms 21496 KB Output is correct
70 Correct 321 ms 19016 KB Output is correct