Submission #927444

# Submission time Handle Problem Language Result Execution time Memory
927444 2024-02-14T21:25:30 Z AlphaMale06 Election Campaign (JOI15_election_campaign) C++17
100 / 100
141 ms 27476 KB
#include <bits/stdc++.h>

using namespace std;

struct path{
    int a, b, c;
};

#define pb push_back
#define F first
#define S second

const int N = 1e5+3;
vector<int> adj[N];
int tin[N], tout[N], h[N];
vector<path> pth[N];
int p[N][18];
int timer=1;
int dp[N][2];
int f[2*N];

void dfs(int v, int par){
    h[v]=h[par]+1;
    tin[v]=timer++;
    p[v][0]=par;
    for(int i=1; i<18; i++){
        p[v][i]=p[p[v][i-1]][i-1];
    }
    for(int to : adj[v]){
        if(to!=par){
            dfs(to, v);
        }
    }
    tout[v]=timer++;
}

bool anc(int a, int b){
    if(tin[a]<=tin[b] && tout[a]>=tout[b])return 1;
    return 0;
}

int lca(int a, int b){
    if(anc(a, b))return a;
    if(anc(b, a))return b;
    for(int i=17; i>=0; i--){
        if(!anc(p[a][i], b)){
            a=p[a][i];
        }
    }
    return p[a][0];
}

void Update(int ind, int add){
    for(int i=ind; i<2*N; i+=i&-i)f[i]+=add;
}

int Get(int ind){
    int ret=0;
    for(int i = ind; i>0; i-=i&-i)ret+=f[i];
    return ret;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    cin >> n;
    for(int i=0; i< n-1; i++){
        int a, b;
        cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    dfs(1, 1);
    cin >> m;
    for(int i=0; i< m; i++){
        int a, b, c;
        cin >> a >> b >> c;
        pth[lca(a, b)].pb({a, b, c});
    }
    pair<int, int> order[n];
    for(int i=0; i< n; i++){
        order[i]={h[i+1], i+1};
    }
    sort(order, order+n);
    reverse(order, order+n);
    for(int i=0; i< n; i++){
        int node=order[i].S;
        for(int to : adj[node]){
            if(to!=p[node][0]){
                dp[node][0]+=dp[to][1];
            }
        }
        dp[node][1]=dp[node][0];
        for(path pt : pth[node]){
            dp[node][1]=max(dp[node][1], dp[node][0]+Get(tin[pt.a])+Get(tin[pt.b])-2*Get(tin[node])+pt.c);
        }
        Update(tin[node], dp[node][0]-dp[node][1]);
        Update(tout[node], dp[node][1]-dp[node][0]);
    }
    cout << dp[1][1] << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8536 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 3 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 60 ms 20052 KB Output is correct
6 Correct 45 ms 23892 KB Output is correct
7 Correct 92 ms 22096 KB Output is correct
8 Correct 48 ms 20316 KB Output is correct
9 Correct 56 ms 21252 KB Output is correct
10 Correct 59 ms 20388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 77 ms 27100 KB Output is correct
5 Correct 60 ms 27212 KB Output is correct
6 Correct 62 ms 27216 KB Output is correct
7 Correct 70 ms 27352 KB Output is correct
8 Correct 60 ms 27072 KB Output is correct
9 Correct 61 ms 27068 KB Output is correct
10 Correct 61 ms 27048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 77 ms 27100 KB Output is correct
5 Correct 60 ms 27212 KB Output is correct
6 Correct 62 ms 27216 KB Output is correct
7 Correct 70 ms 27352 KB Output is correct
8 Correct 60 ms 27072 KB Output is correct
9 Correct 61 ms 27068 KB Output is correct
10 Correct 61 ms 27048 KB Output is correct
11 Correct 9 ms 9308 KB Output is correct
12 Correct 72 ms 27316 KB Output is correct
13 Correct 64 ms 27476 KB Output is correct
14 Correct 62 ms 27432 KB Output is correct
15 Correct 65 ms 27476 KB Output is correct
16 Correct 61 ms 27424 KB Output is correct
17 Correct 72 ms 27244 KB Output is correct
18 Correct 69 ms 27472 KB Output is correct
19 Correct 66 ms 27340 KB Output is correct
20 Correct 61 ms 27472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 22796 KB Output is correct
2 Correct 78 ms 27204 KB Output is correct
3 Correct 100 ms 25172 KB Output is correct
4 Correct 92 ms 23368 KB Output is correct
5 Correct 90 ms 24912 KB Output is correct
6 Correct 82 ms 23364 KB Output is correct
7 Correct 115 ms 24684 KB Output is correct
8 Correct 115 ms 23436 KB Output is correct
9 Correct 60 ms 27100 KB Output is correct
10 Correct 120 ms 24324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8536 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 3 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 60 ms 20052 KB Output is correct
6 Correct 45 ms 23892 KB Output is correct
7 Correct 92 ms 22096 KB Output is correct
8 Correct 48 ms 20316 KB Output is correct
9 Correct 56 ms 21252 KB Output is correct
10 Correct 59 ms 20388 KB Output is correct
11 Correct 3 ms 8536 KB Output is correct
12 Correct 3 ms 8540 KB Output is correct
13 Correct 4 ms 8540 KB Output is correct
14 Correct 3 ms 8540 KB Output is correct
15 Correct 3 ms 8540 KB Output is correct
16 Correct 3 ms 8540 KB Output is correct
17 Correct 3 ms 8540 KB Output is correct
18 Correct 3 ms 8540 KB Output is correct
19 Correct 3 ms 8668 KB Output is correct
20 Correct 3 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8536 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 3 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 60 ms 20052 KB Output is correct
6 Correct 45 ms 23892 KB Output is correct
7 Correct 92 ms 22096 KB Output is correct
8 Correct 48 ms 20316 KB Output is correct
9 Correct 56 ms 21252 KB Output is correct
10 Correct 59 ms 20388 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 2 ms 8540 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 77 ms 27100 KB Output is correct
15 Correct 60 ms 27212 KB Output is correct
16 Correct 62 ms 27216 KB Output is correct
17 Correct 70 ms 27352 KB Output is correct
18 Correct 60 ms 27072 KB Output is correct
19 Correct 61 ms 27068 KB Output is correct
20 Correct 61 ms 27048 KB Output is correct
21 Correct 9 ms 9308 KB Output is correct
22 Correct 72 ms 27316 KB Output is correct
23 Correct 64 ms 27476 KB Output is correct
24 Correct 62 ms 27432 KB Output is correct
25 Correct 65 ms 27476 KB Output is correct
26 Correct 61 ms 27424 KB Output is correct
27 Correct 72 ms 27244 KB Output is correct
28 Correct 69 ms 27472 KB Output is correct
29 Correct 66 ms 27340 KB Output is correct
30 Correct 61 ms 27472 KB Output is correct
31 Correct 98 ms 22796 KB Output is correct
32 Correct 78 ms 27204 KB Output is correct
33 Correct 100 ms 25172 KB Output is correct
34 Correct 92 ms 23368 KB Output is correct
35 Correct 90 ms 24912 KB Output is correct
36 Correct 82 ms 23364 KB Output is correct
37 Correct 115 ms 24684 KB Output is correct
38 Correct 115 ms 23436 KB Output is correct
39 Correct 60 ms 27100 KB Output is correct
40 Correct 120 ms 24324 KB Output is correct
41 Correct 3 ms 8536 KB Output is correct
42 Correct 3 ms 8540 KB Output is correct
43 Correct 4 ms 8540 KB Output is correct
44 Correct 3 ms 8540 KB Output is correct
45 Correct 3 ms 8540 KB Output is correct
46 Correct 3 ms 8540 KB Output is correct
47 Correct 3 ms 8540 KB Output is correct
48 Correct 3 ms 8540 KB Output is correct
49 Correct 3 ms 8668 KB Output is correct
50 Correct 3 ms 8540 KB Output is correct
51 Correct 141 ms 23376 KB Output is correct
52 Correct 67 ms 27476 KB Output is correct
53 Correct 103 ms 24328 KB Output is correct
54 Correct 86 ms 23424 KB Output is correct
55 Correct 96 ms 23052 KB Output is correct
56 Correct 64 ms 27284 KB Output is correct
57 Correct 95 ms 24900 KB Output is correct
58 Correct 91 ms 23460 KB Output is correct
59 Correct 105 ms 23492 KB Output is correct
60 Correct 64 ms 27468 KB Output is correct
61 Correct 107 ms 25256 KB Output is correct
62 Correct 84 ms 23316 KB Output is correct
63 Correct 109 ms 23056 KB Output is correct
64 Correct 64 ms 27408 KB Output is correct
65 Correct 111 ms 24812 KB Output is correct
66 Correct 86 ms 23460 KB Output is correct
67 Correct 108 ms 23108 KB Output is correct
68 Correct 63 ms 27476 KB Output is correct
69 Correct 95 ms 24120 KB Output is correct
70 Correct 83 ms 23604 KB Output is correct