답안 #1111104

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1111104 2024-11-11T13:47:22 Z ljtunas Election Campaign (JOI15_election_campaign) C++17
100 / 100
167 ms 87116 KB
// author : anhtun_hi , nqg
#include <bits/stdc++.h>
#define ll long long
#define ii pair<ll, ll>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define reset(h, v)    memset(h, v, sizeof h)
#define Forv(i, a)     for(auto& i : a)
#define For(i, a, b)   for(int i = a; i <= b; ++i)
#define Ford(i, a, b)  for(int i = a; i >= b; --i)
#define c_bit(i)     __builtin_popcountll(i)
#define Bit(mask, i)    ((mask >> i) & 1LL)
#define onbit(mask, i)  ((mask) bitor (1LL << i))
#define offbit(mask, i) ((mask) &~ (1LL << i))
using namespace std;
namespace std {
    template <typename T, int D>
    struct _vector : public vector <_vector <T, D - 1>> {
        static_assert(D >= 1, "Dimension must be positive!");
        template <typename... Args>
        _vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
    };
    template <typename T> struct _vector <T, 1> : public vector <T> {
        _vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
    };
    template <class A, class B> bool minimize(A &a, B b){return a > b ? a = b, true : false;}
    template <class A, class B> bool maximize(A &a, B b){return a < b ? a = b, true : false;}
}
const int dx[] = {0, 0, +1, -1}, dy[] = {-1, +1, 0, 0}, LOG = 20, base = 311, inf = 1e9 + 5;
const int MAXN = 1e6 + 5;
const  ll  mod = 1e9 + 7;
const  ll   oo = 1e18;

//#define int long long

int n, m; vector<int> g[MAXN];

struct Data{ int a, b; ll w; };
vector<Data> qr[MAXN];

int sz[MAXN], dep[MAXN], p[MAXN][22], in[MAXN], en[MAXN];

void dfs(int u, int par) {
    static int timedfs = 0;
    in[u] = ++timedfs;
    p[u][0] = par, sz[u] = 1;
    For(k, 1, 20) p[u][k] = p[p[u][k - 1]][k - 1];
    for(int v : g[u]) {
        if (v == par) continue;
        dep[v] = dep[u] + 1;
        dfs(v, u);
        sz[u] += sz[v];
    }
    en[u] = timedfs;
}

int getlca(int u, int v) {
    if (dep[u] < dep[v]) swap(u, v);
    int len = dep[u] - dep[v];
    For(k, 0, 20) if (Bit(len, k)) u = p[u][k];
    if (u == v) return u;
    Ford(k, 20, 0) if (p[u][k] != p[v][k])
        u = p[u][k], v = p[v][k];
    return p[u][0];
}

ll dp[MAXN];

ll bit[MAXN];
void update(int u, ll val){
    while(u <= 1e5) bit[u] += val, u += u &- u;
}
void update(int l, int r, ll val){
    update(l, val), update(r + 1, - val);
}
ll get(int u){
    ll res = 0;
    while(u) res += bit[u], u -= u &- u;
    return res;
}

void solve(int u, int par){
    ll sum = 0;
    Forv(v, g[u]) if(v ^ par) solve(v, u), sum += dp[v];
    dp[u] = sum;
    for(auto [a, b, c] : qr[u]){
        ll tmp1 = get(in[a]); if(a == u) tmp1 = 0;
        ll tmp2 = get(in[b]); if(b == u) tmp2 = 0;
        maximize(dp[u], c + sum + tmp1 + tmp2);
    }
    update(in[u], en[u], sum - dp[u]);
}

void Solve() {
    cin >> n;
    For(i, 1, n - 1){
        int u, v; cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1, 0);
    cin >> m;
    For(i, 1, m){
        int a, b; ll w; cin >> a >> b >> w;
        qr[getlca(a, b)].push_back(Data{a, b, w});
    }
    solve(1, 0);
    ll ans = 0;
    For(i, 1, n) maximize(ans, dp[i]);
    cout << ans << '\n';
}

int32_t main() {
    cin.tie(0) -> sync_with_stdio(0);
    if(fopen("JOI15_election_campaign.inp", "r")) {
        freopen("JOI15_election_campaign.inp", "r", stdin);
        freopen("JOI15_election_campaign.out", "w", stdout);
    }

    int t = 1;
//    cin >> t;

    for (int test = 1; test <= t; test++) {
        Solve();
    }
    return 0;
}

Compilation message

election_campaign.cpp: In function 'int32_t main()':
election_campaign.cpp:117:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |         freopen("JOI15_election_campaign.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:118:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  118 |         freopen("JOI15_election_campaign.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 59960 KB Output is correct
2 Correct 10 ms 60156 KB Output is correct
3 Correct 11 ms 59984 KB Output is correct
4 Correct 11 ms 59984 KB Output is correct
5 Correct 55 ms 75080 KB Output is correct
6 Correct 51 ms 82760 KB Output is correct
7 Correct 61 ms 80200 KB Output is correct
8 Correct 50 ms 75604 KB Output is correct
9 Correct 64 ms 78688 KB Output is correct
10 Correct 64 ms 72520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 59992 KB Output is correct
2 Correct 10 ms 59984 KB Output is correct
3 Correct 10 ms 59984 KB Output is correct
4 Correct 83 ms 81996 KB Output is correct
5 Correct 92 ms 86600 KB Output is correct
6 Correct 78 ms 86864 KB Output is correct
7 Correct 84 ms 86796 KB Output is correct
8 Correct 80 ms 85320 KB Output is correct
9 Correct 75 ms 81844 KB Output is correct
10 Correct 81 ms 86600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 59992 KB Output is correct
2 Correct 10 ms 59984 KB Output is correct
3 Correct 10 ms 59984 KB Output is correct
4 Correct 83 ms 81996 KB Output is correct
5 Correct 92 ms 86600 KB Output is correct
6 Correct 78 ms 86864 KB Output is correct
7 Correct 84 ms 86796 KB Output is correct
8 Correct 80 ms 85320 KB Output is correct
9 Correct 75 ms 81844 KB Output is correct
10 Correct 81 ms 86600 KB Output is correct
11 Correct 15 ms 61008 KB Output is correct
12 Correct 92 ms 80648 KB Output is correct
13 Correct 83 ms 87036 KB Output is correct
14 Correct 75 ms 87116 KB Output is correct
15 Correct 88 ms 85552 KB Output is correct
16 Correct 76 ms 80656 KB Output is correct
17 Correct 83 ms 85576 KB Output is correct
18 Correct 90 ms 86856 KB Output is correct
19 Correct 83 ms 84040 KB Output is correct
20 Correct 90 ms 84136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 91 ms 78312 KB Output is correct
2 Correct 83 ms 86848 KB Output is correct
3 Correct 117 ms 83592 KB Output is correct
4 Correct 86 ms 78844 KB Output is correct
5 Correct 107 ms 83140 KB Output is correct
6 Correct 93 ms 78828 KB Output is correct
7 Correct 167 ms 81352 KB Output is correct
8 Correct 94 ms 76852 KB Output is correct
9 Correct 81 ms 82052 KB Output is correct
10 Correct 110 ms 76996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 59960 KB Output is correct
2 Correct 10 ms 60156 KB Output is correct
3 Correct 11 ms 59984 KB Output is correct
4 Correct 11 ms 59984 KB Output is correct
5 Correct 55 ms 75080 KB Output is correct
6 Correct 51 ms 82760 KB Output is correct
7 Correct 61 ms 80200 KB Output is correct
8 Correct 50 ms 75604 KB Output is correct
9 Correct 64 ms 78688 KB Output is correct
10 Correct 64 ms 72520 KB Output is correct
11 Correct 14 ms 59984 KB Output is correct
12 Correct 11 ms 60204 KB Output is correct
13 Correct 11 ms 59984 KB Output is correct
14 Correct 13 ms 57936 KB Output is correct
15 Correct 11 ms 55912 KB Output is correct
16 Correct 10 ms 53840 KB Output is correct
17 Correct 11 ms 54008 KB Output is correct
18 Correct 14 ms 60200 KB Output is correct
19 Correct 11 ms 55888 KB Output is correct
20 Correct 14 ms 59984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 59960 KB Output is correct
2 Correct 10 ms 60156 KB Output is correct
3 Correct 11 ms 59984 KB Output is correct
4 Correct 11 ms 59984 KB Output is correct
5 Correct 55 ms 75080 KB Output is correct
6 Correct 51 ms 82760 KB Output is correct
7 Correct 61 ms 80200 KB Output is correct
8 Correct 50 ms 75604 KB Output is correct
9 Correct 64 ms 78688 KB Output is correct
10 Correct 64 ms 72520 KB Output is correct
11 Correct 9 ms 59992 KB Output is correct
12 Correct 10 ms 59984 KB Output is correct
13 Correct 10 ms 59984 KB Output is correct
14 Correct 83 ms 81996 KB Output is correct
15 Correct 92 ms 86600 KB Output is correct
16 Correct 78 ms 86864 KB Output is correct
17 Correct 84 ms 86796 KB Output is correct
18 Correct 80 ms 85320 KB Output is correct
19 Correct 75 ms 81844 KB Output is correct
20 Correct 81 ms 86600 KB Output is correct
21 Correct 15 ms 61008 KB Output is correct
22 Correct 92 ms 80648 KB Output is correct
23 Correct 83 ms 87036 KB Output is correct
24 Correct 75 ms 87116 KB Output is correct
25 Correct 88 ms 85552 KB Output is correct
26 Correct 76 ms 80656 KB Output is correct
27 Correct 83 ms 85576 KB Output is correct
28 Correct 90 ms 86856 KB Output is correct
29 Correct 83 ms 84040 KB Output is correct
30 Correct 90 ms 84136 KB Output is correct
31 Correct 91 ms 78312 KB Output is correct
32 Correct 83 ms 86848 KB Output is correct
33 Correct 117 ms 83592 KB Output is correct
34 Correct 86 ms 78844 KB Output is correct
35 Correct 107 ms 83140 KB Output is correct
36 Correct 93 ms 78828 KB Output is correct
37 Correct 167 ms 81352 KB Output is correct
38 Correct 94 ms 76852 KB Output is correct
39 Correct 81 ms 82052 KB Output is correct
40 Correct 110 ms 76996 KB Output is correct
41 Correct 14 ms 59984 KB Output is correct
42 Correct 11 ms 60204 KB Output is correct
43 Correct 11 ms 59984 KB Output is correct
44 Correct 13 ms 57936 KB Output is correct
45 Correct 11 ms 55912 KB Output is correct
46 Correct 10 ms 53840 KB Output is correct
47 Correct 11 ms 54008 KB Output is correct
48 Correct 14 ms 60200 KB Output is correct
49 Correct 11 ms 55888 KB Output is correct
50 Correct 14 ms 59984 KB Output is correct
51 Correct 108 ms 73412 KB Output is correct
52 Correct 98 ms 84040 KB Output is correct
53 Correct 113 ms 77144 KB Output is correct
54 Correct 87 ms 71748 KB Output is correct
55 Correct 87 ms 73148 KB Output is correct
56 Correct 90 ms 82504 KB Output is correct
57 Correct 118 ms 76456 KB Output is correct
58 Correct 84 ms 71756 KB Output is correct
59 Correct 137 ms 72592 KB Output is correct
60 Correct 91 ms 79688 KB Output is correct
61 Correct 111 ms 78448 KB Output is correct
62 Correct 87 ms 74004 KB Output is correct
63 Correct 101 ms 73184 KB Output is correct
64 Correct 90 ms 85524 KB Output is correct
65 Correct 114 ms 82716 KB Output is correct
66 Correct 73 ms 77344 KB Output is correct
67 Correct 79 ms 78768 KB Output is correct
68 Correct 95 ms 86992 KB Output is correct
69 Correct 102 ms 79996 KB Output is correct
70 Correct 80 ms 78780 KB Output is correct