Submission #1070332

# Submission time Handle Problem Language Result Execution time Memory
1070332 2024-08-22T13:18:37 Z Boycl07 Election Campaign (JOI15_election_campaign) C++17
100 / 100
86 ms 29028 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 1; (i) <= (n); ++i)
#define forn(i, l, r) for(int i = (l); i <= (r); ++i)
#define ford(i, r, l) for(int i = (r); i >= (l); --i)
#define FOR(i, n) for(int i = 0; i < (n); ++i)
#define FORD(i, n) for(int i = ((n) - 1); i >= 0; --i)
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define endl "\n"
#define task "FFILL"
#define sz(a) int(a.size())
#define C(x, y) make_pair(x, y)
#define all(a) (a).begin(), (a).end()
#define bit(i, mask) (mask >> i & 1)

template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }


const int N = 1e5 + 3;
const int LIM = (1 << 16) + 3;
const ll INF = 1e18 + 1;
const int LOG = 16;

int n, q;
vector<int> g[N];
vector<array<int, 3>> f[N];
int tin[N], tout[N], up[N][LOG + 1], timedfs = 0;
int val[N];
int dp[N];
int bit[N];

void update(int u, int v)
{
    for(; u <= n; u += u & -u) bit[u] += v;
}

int get(int u)
{
    int res = 0;
    for(; u; u -= u & -u) res += bit[u];
    return res;
}

void pre_dfs(int u, int p = 1)
{
    up[u][0] = p;
    tin[u] = ++timedfs;
    forn(i, 1, LOG) up[u][i] = up[up[u][i - 1]][i - 1];
    for(int v : g[u]) if(v != p) pre_dfs(v, u);
    tout[u] = timedfs;
}

bool isAnc(int u, int v) {return tin[u] <= tin[v] && tout[v] <= tout[u];}

int lca(int u, int v)
{
    if(isAnc(u, v)) return u;
    if(isAnc(v, u)) return v;
    ford(i, LOG, 0) if(!isAnc(up[u][i], v)) u = up[u][i];
    return up[u][0];
}

void dfs(int u, int p = 1)
{
    int sum = 0, best = 0;
    for(int v : g[u]) if(v != p) dfs(v, u), sum += dp[v];
    for(auto &[x, y, c] : f[u])
    {
        int val = c + (x == u ? 0 : get(tin[x]))
                    + (y == u ? 0 : get(tin[y]));
        maximize(best, val);
    }
    dp[u] = sum + best;
    update(tin[u], -best); update(tout[u] + 1, best);
}

void solve()
{
    cin >> n;
    rep(i, n - 1)
    {
        int u, v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }

    pre_dfs(1);
    cin >> q;
    rep(i, q)
    {
        int u, v, c;
        cin >> u >> v >> c;
        f[lca(u, v)].pb({u, v, c});
    }
    dfs(1);
    cout << dp[1];
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int TC = 1;

    if(fopen("note.inp", "r"))
    {
        freopen("note.inp", "r", stdin);
        freopen("note.out", "w", stdout);
    }


    while(TC--)
    {
        solve();
    }

    return 0;
}

Compilation message

election_campaign.cpp: In function 'int 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("note.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("note.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 7256 KB Output is correct
2 Correct 2 ms 7260 KB Output is correct
3 Correct 2 ms 7260 KB Output is correct
4 Correct 2 ms 7516 KB Output is correct
5 Correct 40 ms 17620 KB Output is correct
6 Correct 26 ms 25432 KB Output is correct
7 Correct 42 ms 22608 KB Output is correct
8 Correct 28 ms 18012 KB Output is correct
9 Correct 53 ms 20976 KB Output is correct
10 Correct 29 ms 18000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 7260 KB Output is correct
2 Correct 2 ms 7256 KB Output is correct
3 Correct 2 ms 7516 KB Output is correct
4 Correct 53 ms 28680 KB Output is correct
5 Correct 53 ms 28752 KB Output is correct
6 Correct 49 ms 28752 KB Output is correct
7 Correct 63 ms 28752 KB Output is correct
8 Correct 50 ms 28756 KB Output is correct
9 Correct 50 ms 28688 KB Output is correct
10 Correct 50 ms 28752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 7260 KB Output is correct
2 Correct 2 ms 7256 KB Output is correct
3 Correct 2 ms 7516 KB Output is correct
4 Correct 53 ms 28680 KB Output is correct
5 Correct 53 ms 28752 KB Output is correct
6 Correct 49 ms 28752 KB Output is correct
7 Correct 63 ms 28752 KB Output is correct
8 Correct 50 ms 28756 KB Output is correct
9 Correct 50 ms 28688 KB Output is correct
10 Correct 50 ms 28752 KB Output is correct
11 Correct 6 ms 8284 KB Output is correct
12 Correct 53 ms 29028 KB Output is correct
13 Correct 54 ms 29012 KB Output is correct
14 Correct 52 ms 28896 KB Output is correct
15 Correct 50 ms 29008 KB Output is correct
16 Correct 55 ms 29012 KB Output is correct
17 Correct 49 ms 29012 KB Output is correct
18 Correct 49 ms 29008 KB Output is correct
19 Correct 48 ms 29012 KB Output is correct
20 Correct 50 ms 29008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 20492 KB Output is correct
2 Correct 61 ms 28752 KB Output is correct
3 Correct 80 ms 25672 KB Output is correct
4 Correct 56 ms 20816 KB Output is correct
5 Correct 66 ms 25176 KB Output is correct
6 Correct 61 ms 20876 KB Output is correct
7 Correct 85 ms 24844 KB Output is correct
8 Correct 60 ms 20724 KB Output is correct
9 Correct 47 ms 28756 KB Output is correct
10 Correct 77 ms 23816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 7256 KB Output is correct
2 Correct 2 ms 7260 KB Output is correct
3 Correct 2 ms 7260 KB Output is correct
4 Correct 2 ms 7516 KB Output is correct
5 Correct 40 ms 17620 KB Output is correct
6 Correct 26 ms 25432 KB Output is correct
7 Correct 42 ms 22608 KB Output is correct
8 Correct 28 ms 18012 KB Output is correct
9 Correct 53 ms 20976 KB Output is correct
10 Correct 29 ms 18000 KB Output is correct
11 Correct 2 ms 7512 KB Output is correct
12 Correct 2 ms 7512 KB Output is correct
13 Correct 2 ms 7516 KB Output is correct
14 Correct 2 ms 7444 KB Output is correct
15 Correct 2 ms 7516 KB Output is correct
16 Correct 2 ms 7580 KB Output is correct
17 Correct 2 ms 7516 KB Output is correct
18 Correct 2 ms 7516 KB Output is correct
19 Correct 2 ms 7516 KB Output is correct
20 Correct 2 ms 7516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 7256 KB Output is correct
2 Correct 2 ms 7260 KB Output is correct
3 Correct 2 ms 7260 KB Output is correct
4 Correct 2 ms 7516 KB Output is correct
5 Correct 40 ms 17620 KB Output is correct
6 Correct 26 ms 25432 KB Output is correct
7 Correct 42 ms 22608 KB Output is correct
8 Correct 28 ms 18012 KB Output is correct
9 Correct 53 ms 20976 KB Output is correct
10 Correct 29 ms 18000 KB Output is correct
11 Correct 1 ms 7260 KB Output is correct
12 Correct 2 ms 7256 KB Output is correct
13 Correct 2 ms 7516 KB Output is correct
14 Correct 53 ms 28680 KB Output is correct
15 Correct 53 ms 28752 KB Output is correct
16 Correct 49 ms 28752 KB Output is correct
17 Correct 63 ms 28752 KB Output is correct
18 Correct 50 ms 28756 KB Output is correct
19 Correct 50 ms 28688 KB Output is correct
20 Correct 50 ms 28752 KB Output is correct
21 Correct 6 ms 8284 KB Output is correct
22 Correct 53 ms 29028 KB Output is correct
23 Correct 54 ms 29012 KB Output is correct
24 Correct 52 ms 28896 KB Output is correct
25 Correct 50 ms 29008 KB Output is correct
26 Correct 55 ms 29012 KB Output is correct
27 Correct 49 ms 29012 KB Output is correct
28 Correct 49 ms 29008 KB Output is correct
29 Correct 48 ms 29012 KB Output is correct
30 Correct 50 ms 29008 KB Output is correct
31 Correct 62 ms 20492 KB Output is correct
32 Correct 61 ms 28752 KB Output is correct
33 Correct 80 ms 25672 KB Output is correct
34 Correct 56 ms 20816 KB Output is correct
35 Correct 66 ms 25176 KB Output is correct
36 Correct 61 ms 20876 KB Output is correct
37 Correct 85 ms 24844 KB Output is correct
38 Correct 60 ms 20724 KB Output is correct
39 Correct 47 ms 28756 KB Output is correct
40 Correct 77 ms 23816 KB Output is correct
41 Correct 2 ms 7512 KB Output is correct
42 Correct 2 ms 7512 KB Output is correct
43 Correct 2 ms 7516 KB Output is correct
44 Correct 2 ms 7444 KB Output is correct
45 Correct 2 ms 7516 KB Output is correct
46 Correct 2 ms 7580 KB Output is correct
47 Correct 2 ms 7516 KB Output is correct
48 Correct 2 ms 7516 KB Output is correct
49 Correct 2 ms 7516 KB Output is correct
50 Correct 2 ms 7516 KB Output is correct
51 Correct 64 ms 20960 KB Output is correct
52 Correct 48 ms 29012 KB Output is correct
53 Correct 83 ms 24320 KB Output is correct
54 Correct 63 ms 21388 KB Output is correct
55 Correct 71 ms 20804 KB Output is correct
56 Correct 51 ms 29008 KB Output is correct
57 Correct 71 ms 24912 KB Output is correct
58 Correct 59 ms 21000 KB Output is correct
59 Correct 68 ms 21072 KB Output is correct
60 Correct 53 ms 29008 KB Output is correct
61 Correct 84 ms 25112 KB Output is correct
62 Correct 60 ms 20812 KB Output is correct
63 Correct 71 ms 20664 KB Output is correct
64 Correct 57 ms 29012 KB Output is correct
65 Correct 83 ms 24940 KB Output is correct
66 Correct 55 ms 21260 KB Output is correct
67 Correct 66 ms 20772 KB Output is correct
68 Correct 49 ms 28812 KB Output is correct
69 Correct 86 ms 23892 KB Output is correct
70 Correct 61 ms 21332 KB Output is correct