Submission #968545

# Submission time Handle Problem Language Result Execution time Memory
968545 2024-04-23T15:14:54 Z vjudge1 Election Campaign (JOI15_election_campaign) C++17
100 / 100
116 ms 39940 KB
#include <bits/stdc++.h>

#define ll long long
#define all(x) x.begin(), x.end()
#define Neco "Election Campaign"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 2e5 + 7

using namespace std;

const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

ll GetRandom(ll l, ll r)
{
    return uniform_int_distribution<ll> (l, r)(rng);
}

int n, m;
int cha[maxn][20];
ll dp[maxn], Sum[maxn];
struct dl { int x, y, w; } a[maxn];
vector<int> edges[maxn], store[maxn];

/// PRE DFS
int In[maxn], Out[maxn];
void pre_dfs(int u, int par) {
    static int Time = 0;
    In[u] = ++Time;
    for(int v : edges[u]) if(v != par) {
        cha[v][0] = u;
        fi(i, 1, 17) cha[v][i] = cha[cha[v][i - 1]][i - 1];
        pre_dfs(v, u);
    }
    Out[u] = Time;
}

bool insubtree(int u, int p) {
    return p == 0 || In[p] <= In[u] && In[u] <= Out[p];
}

int lca(int u, int v) {
    if(insubtree(u, v)) return v;
    if(insubtree(v, u)) return u;
    fid(i, 17, 0) if(!insubtree(v, cha[u][i])) u = cha[u][i];
    return cha[u][0];
}

/*-------------------------------------*/

/// DS
struct BIT {
    ll bit[maxn];
    void update(int l, int r, ll val) {
        for(; l < maxn; l += (l & -l)) bit[l] += val;
        for(++r; r < maxn; r += (r & -r)) bit[r] -= val;
    }
    ll get(int x, ll ans = 0) {
        for(; x > 0; x -= (x & -x)) ans += bit[x];
        return ans;
    }
} Bit;
/*------------------------*/

void dfs(int u, int par)
{
    for(int v : edges[u]) if(v != par) {
        dfs(v, u);
        Sum[u] += dp[v];
    }

    dp[u] = Sum[u];
    for(int id : store[u]) {
        ll val = a[id].w;

        if(a[id].x != u) val += Bit.get(In[a[id].x]);
        if(a[id].y != u) val += Bit.get(In[a[id].y]);

        dp[u] = max(dp[u], Sum[u] + val);
    }

    Bit.update(In[u], Out[u], Sum[u] - dp[u]);
}

void solve()
{

    cin >> n;

    fi(i, 1, n - 1) {
        int x, y; cin >> x >> y;
        edges[x].push_back(y), edges[y].push_back(x);
    }

    pre_dfs(1, 0);

    cin >> m;

    fi(i, 1, m) {
        int x, y, w; cin >> x >> y >> w;
        store[lca(x, y)].push_back(i);
        a[i] = {x, y, w};
    }

    dfs(1, 0);

    cout << dp[1];

}


int main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

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


    int nTest = 1;
//    cin >> nTest;


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


    return 0;
}

Compilation message

election_campaign.cpp: In function 'bool insubtree(int, int)':
election_campaign.cpp:47:37: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   47 |     return p == 0 || In[p] <= In[u] && In[u] <= Out[p];
      |                      ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:128:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  128 |         freopen(Neco".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:129:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  129 |         freopen(Neco".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 16740 KB Output is correct
2 Correct 3 ms 16732 KB Output is correct
3 Correct 4 ms 16988 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 57 ms 28508 KB Output is correct
6 Correct 39 ms 38552 KB Output is correct
7 Correct 76 ms 35452 KB Output is correct
8 Correct 49 ms 28308 KB Output is correct
9 Correct 56 ms 32596 KB Output is correct
10 Correct 43 ms 28088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16728 KB Output is correct
2 Correct 3 ms 16732 KB Output is correct
3 Correct 5 ms 16988 KB Output is correct
4 Correct 65 ms 39592 KB Output is correct
5 Correct 70 ms 39600 KB Output is correct
6 Correct 67 ms 39764 KB Output is correct
7 Correct 66 ms 39568 KB Output is correct
8 Correct 90 ms 39764 KB Output is correct
9 Correct 73 ms 39764 KB Output is correct
10 Correct 69 ms 39688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16728 KB Output is correct
2 Correct 3 ms 16732 KB Output is correct
3 Correct 5 ms 16988 KB Output is correct
4 Correct 65 ms 39592 KB Output is correct
5 Correct 70 ms 39600 KB Output is correct
6 Correct 67 ms 39764 KB Output is correct
7 Correct 66 ms 39568 KB Output is correct
8 Correct 90 ms 39764 KB Output is correct
9 Correct 73 ms 39764 KB Output is correct
10 Correct 69 ms 39688 KB Output is correct
11 Correct 8 ms 17244 KB Output is correct
12 Correct 76 ms 39940 KB Output is correct
13 Correct 67 ms 39616 KB Output is correct
14 Correct 68 ms 39760 KB Output is correct
15 Correct 76 ms 39760 KB Output is correct
16 Correct 66 ms 39760 KB Output is correct
17 Correct 66 ms 39628 KB Output is correct
18 Correct 71 ms 39756 KB Output is correct
19 Correct 70 ms 39724 KB Output is correct
20 Correct 69 ms 39772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 29136 KB Output is correct
2 Correct 69 ms 39936 KB Output is correct
3 Correct 105 ms 35372 KB Output is correct
4 Correct 86 ms 28676 KB Output is correct
5 Correct 97 ms 34640 KB Output is correct
6 Correct 67 ms 28880 KB Output is correct
7 Correct 109 ms 34360 KB Output is correct
8 Correct 93 ms 29524 KB Output is correct
9 Correct 64 ms 39760 KB Output is correct
10 Correct 111 ms 34080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 16740 KB Output is correct
2 Correct 3 ms 16732 KB Output is correct
3 Correct 4 ms 16988 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 57 ms 28508 KB Output is correct
6 Correct 39 ms 38552 KB Output is correct
7 Correct 76 ms 35452 KB Output is correct
8 Correct 49 ms 28308 KB Output is correct
9 Correct 56 ms 32596 KB Output is correct
10 Correct 43 ms 28088 KB Output is correct
11 Correct 5 ms 16884 KB Output is correct
12 Correct 4 ms 16988 KB Output is correct
13 Correct 4 ms 16988 KB Output is correct
14 Correct 4 ms 16732 KB Output is correct
15 Correct 4 ms 16732 KB Output is correct
16 Correct 4 ms 16988 KB Output is correct
17 Correct 5 ms 17184 KB Output is correct
18 Correct 4 ms 16988 KB Output is correct
19 Correct 3 ms 16732 KB Output is correct
20 Correct 3 ms 16988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 16740 KB Output is correct
2 Correct 3 ms 16732 KB Output is correct
3 Correct 4 ms 16988 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 57 ms 28508 KB Output is correct
6 Correct 39 ms 38552 KB Output is correct
7 Correct 76 ms 35452 KB Output is correct
8 Correct 49 ms 28308 KB Output is correct
9 Correct 56 ms 32596 KB Output is correct
10 Correct 43 ms 28088 KB Output is correct
11 Correct 3 ms 16728 KB Output is correct
12 Correct 3 ms 16732 KB Output is correct
13 Correct 5 ms 16988 KB Output is correct
14 Correct 65 ms 39592 KB Output is correct
15 Correct 70 ms 39600 KB Output is correct
16 Correct 67 ms 39764 KB Output is correct
17 Correct 66 ms 39568 KB Output is correct
18 Correct 90 ms 39764 KB Output is correct
19 Correct 73 ms 39764 KB Output is correct
20 Correct 69 ms 39688 KB Output is correct
21 Correct 8 ms 17244 KB Output is correct
22 Correct 76 ms 39940 KB Output is correct
23 Correct 67 ms 39616 KB Output is correct
24 Correct 68 ms 39760 KB Output is correct
25 Correct 76 ms 39760 KB Output is correct
26 Correct 66 ms 39760 KB Output is correct
27 Correct 66 ms 39628 KB Output is correct
28 Correct 71 ms 39756 KB Output is correct
29 Correct 70 ms 39724 KB Output is correct
30 Correct 69 ms 39772 KB Output is correct
31 Correct 99 ms 29136 KB Output is correct
32 Correct 69 ms 39936 KB Output is correct
33 Correct 105 ms 35372 KB Output is correct
34 Correct 86 ms 28676 KB Output is correct
35 Correct 97 ms 34640 KB Output is correct
36 Correct 67 ms 28880 KB Output is correct
37 Correct 109 ms 34360 KB Output is correct
38 Correct 93 ms 29524 KB Output is correct
39 Correct 64 ms 39760 KB Output is correct
40 Correct 111 ms 34080 KB Output is correct
41 Correct 5 ms 16884 KB Output is correct
42 Correct 4 ms 16988 KB Output is correct
43 Correct 4 ms 16988 KB Output is correct
44 Correct 4 ms 16732 KB Output is correct
45 Correct 4 ms 16732 KB Output is correct
46 Correct 4 ms 16988 KB Output is correct
47 Correct 5 ms 17184 KB Output is correct
48 Correct 4 ms 16988 KB Output is correct
49 Correct 3 ms 16732 KB Output is correct
50 Correct 3 ms 16988 KB Output is correct
51 Correct 104 ms 29400 KB Output is correct
52 Correct 71 ms 39608 KB Output is correct
53 Correct 110 ms 33484 KB Output is correct
54 Correct 69 ms 28764 KB Output is correct
55 Correct 85 ms 29132 KB Output is correct
56 Correct 76 ms 39756 KB Output is correct
57 Correct 93 ms 34160 KB Output is correct
58 Correct 69 ms 28760 KB Output is correct
59 Correct 86 ms 29548 KB Output is correct
60 Correct 66 ms 39756 KB Output is correct
61 Correct 116 ms 34332 KB Output is correct
62 Correct 70 ms 28688 KB Output is correct
63 Correct 82 ms 29936 KB Output is correct
64 Correct 71 ms 39760 KB Output is correct
65 Correct 111 ms 34256 KB Output is correct
66 Correct 75 ms 29520 KB Output is correct
67 Correct 84 ms 29984 KB Output is correct
68 Correct 85 ms 39712 KB Output is correct
69 Correct 89 ms 32848 KB Output is correct
70 Correct 77 ms 28976 KB Output is correct