Submission #253778

# Submission time Handle Problem Language Result Execution time Memory
253778 2020-07-28T16:36:18 Z someone Election Campaign (JOI15_election_campaign) C++14
100 / 100
271 ms 47356 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;
 
struct Way {
    int a, b, cost;
};

const int M = 1 << 18, N = 2*M;

vector<int> adj[M];
vector<Way> way[M];
int n, m, l = M, deb[M], fin[M], sum[M], a[N], par[N][19];

void DFS(int i, int pre) {
    deb[i] = l++;
    par[i][0] = pre;
    for(int j : adj[i])
        if(j != pre)
            DFS(j, i);
    fin[i] = l++;
}

bool isAnc(int a, int b) {
    if(a == -1) return true;
    return deb[a] <= deb[b] && fin[b] <= fin[a];
}

int LCA(int a, int b) {
    if(isAnc(a, b)) return a;
    if(isAnc(b, a)) return b;
    for(int i = 18; i > -1; i--)
        if(!isAnc(par[a][i], b))
            a = par[a][i];
    return par[a][0];
}

int getSum(int i)  {
    int t = a[i];
    while(i > 0) {
        if(i % 2 == 1)
            t += a[i-1];
        i /= 2;
    }
    return t;
}

void insert(int i, int val) {
    while(i > 0) {
        a[i] += val;
        i /= 2;
    }
}

int get(int i) {
    for(int j : adj[i])
        if(j != par[i][0])
            sum[i] += get(j);
    int val = 0;
    for(Way w : way[i])
        val = max(val, w.cost - getSum(deb[w.a]) - getSum(deb[w.b]));
    sum[i] += val;
    insert(deb[i], val);
    insert(fin[i], -val);
    return sum[i];
}

signed main() {
    /*
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    */
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    cin >> n;
    for(int i = 1; i < n; i++) {
        int a, b; cin >> a >> b;
        a--, b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    
    DFS(0, -1);
    for(int i = 0; i < 18; i++)
        for(int j = 0; j < n; j++)
            if(par[j][i] == -1) par[j][i+1] = -1;
            else par[j][i+1] = par[par[j][i]][i];
    
    cin >> m;
    for(int i = 0; i < m; i++) {
        int a, b, cost;
        cin >> a >> b >> cost;
        a--, b--;
        int c = LCA(a, b);
        way[c].push_back({a, b, cost});
    }
    cout << get(0) << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12672 KB Output is correct
2 Correct 9 ms 12672 KB Output is correct
3 Correct 8 ms 12672 KB Output is correct
4 Correct 10 ms 12928 KB Output is correct
5 Correct 115 ms 38136 KB Output is correct
6 Correct 80 ms 42236 KB Output is correct
7 Correct 119 ms 40916 KB Output is correct
8 Correct 98 ms 38392 KB Output is correct
9 Correct 106 ms 39928 KB Output is correct
10 Correct 96 ms 38264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12672 KB Output is correct
2 Correct 8 ms 12800 KB Output is correct
3 Correct 11 ms 13056 KB Output is correct
4 Correct 144 ms 46880 KB Output is correct
5 Correct 136 ms 46968 KB Output is correct
6 Correct 154 ms 47096 KB Output is correct
7 Correct 139 ms 46968 KB Output is correct
8 Correct 163 ms 46968 KB Output is correct
9 Correct 157 ms 47096 KB Output is correct
10 Correct 151 ms 46968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12672 KB Output is correct
2 Correct 8 ms 12800 KB Output is correct
3 Correct 11 ms 13056 KB Output is correct
4 Correct 144 ms 46880 KB Output is correct
5 Correct 136 ms 46968 KB Output is correct
6 Correct 154 ms 47096 KB Output is correct
7 Correct 139 ms 46968 KB Output is correct
8 Correct 163 ms 46968 KB Output is correct
9 Correct 157 ms 47096 KB Output is correct
10 Correct 151 ms 46968 KB Output is correct
11 Correct 20 ms 14208 KB Output is correct
12 Correct 164 ms 47224 KB Output is correct
13 Correct 151 ms 47224 KB Output is correct
14 Correct 131 ms 47224 KB Output is correct
15 Correct 150 ms 47224 KB Output is correct
16 Correct 137 ms 47356 KB Output is correct
17 Correct 155 ms 47228 KB Output is correct
18 Correct 154 ms 47224 KB Output is correct
19 Correct 142 ms 47224 KB Output is correct
20 Correct 151 ms 47224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 227 ms 42408 KB Output is correct
2 Correct 153 ms 46968 KB Output is correct
3 Correct 253 ms 45444 KB Output is correct
4 Correct 187 ms 42796 KB Output is correct
5 Correct 209 ms 45112 KB Output is correct
6 Correct 189 ms 42724 KB Output is correct
7 Correct 271 ms 44844 KB Output is correct
8 Correct 243 ms 42492 KB Output is correct
9 Correct 134 ms 46968 KB Output is correct
10 Correct 222 ms 43944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12672 KB Output is correct
2 Correct 9 ms 12672 KB Output is correct
3 Correct 8 ms 12672 KB Output is correct
4 Correct 10 ms 12928 KB Output is correct
5 Correct 115 ms 38136 KB Output is correct
6 Correct 80 ms 42236 KB Output is correct
7 Correct 119 ms 40916 KB Output is correct
8 Correct 98 ms 38392 KB Output is correct
9 Correct 106 ms 39928 KB Output is correct
10 Correct 96 ms 38264 KB Output is correct
11 Correct 8 ms 13056 KB Output is correct
12 Correct 8 ms 13056 KB Output is correct
13 Correct 9 ms 13056 KB Output is correct
14 Correct 9 ms 13056 KB Output is correct
15 Correct 8 ms 13056 KB Output is correct
16 Correct 9 ms 13056 KB Output is correct
17 Correct 9 ms 13056 KB Output is correct
18 Correct 8 ms 13056 KB Output is correct
19 Correct 10 ms 13056 KB Output is correct
20 Correct 8 ms 13056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12672 KB Output is correct
2 Correct 9 ms 12672 KB Output is correct
3 Correct 8 ms 12672 KB Output is correct
4 Correct 10 ms 12928 KB Output is correct
5 Correct 115 ms 38136 KB Output is correct
6 Correct 80 ms 42236 KB Output is correct
7 Correct 119 ms 40916 KB Output is correct
8 Correct 98 ms 38392 KB Output is correct
9 Correct 106 ms 39928 KB Output is correct
10 Correct 96 ms 38264 KB Output is correct
11 Correct 8 ms 12672 KB Output is correct
12 Correct 8 ms 12800 KB Output is correct
13 Correct 11 ms 13056 KB Output is correct
14 Correct 144 ms 46880 KB Output is correct
15 Correct 136 ms 46968 KB Output is correct
16 Correct 154 ms 47096 KB Output is correct
17 Correct 139 ms 46968 KB Output is correct
18 Correct 163 ms 46968 KB Output is correct
19 Correct 157 ms 47096 KB Output is correct
20 Correct 151 ms 46968 KB Output is correct
21 Correct 20 ms 14208 KB Output is correct
22 Correct 164 ms 47224 KB Output is correct
23 Correct 151 ms 47224 KB Output is correct
24 Correct 131 ms 47224 KB Output is correct
25 Correct 150 ms 47224 KB Output is correct
26 Correct 137 ms 47356 KB Output is correct
27 Correct 155 ms 47228 KB Output is correct
28 Correct 154 ms 47224 KB Output is correct
29 Correct 142 ms 47224 KB Output is correct
30 Correct 151 ms 47224 KB Output is correct
31 Correct 227 ms 42408 KB Output is correct
32 Correct 153 ms 46968 KB Output is correct
33 Correct 253 ms 45444 KB Output is correct
34 Correct 187 ms 42796 KB Output is correct
35 Correct 209 ms 45112 KB Output is correct
36 Correct 189 ms 42724 KB Output is correct
37 Correct 271 ms 44844 KB Output is correct
38 Correct 243 ms 42492 KB Output is correct
39 Correct 134 ms 46968 KB Output is correct
40 Correct 222 ms 43944 KB Output is correct
41 Correct 8 ms 13056 KB Output is correct
42 Correct 8 ms 13056 KB Output is correct
43 Correct 9 ms 13056 KB Output is correct
44 Correct 9 ms 13056 KB Output is correct
45 Correct 8 ms 13056 KB Output is correct
46 Correct 9 ms 13056 KB Output is correct
47 Correct 9 ms 13056 KB Output is correct
48 Correct 8 ms 13056 KB Output is correct
49 Correct 10 ms 13056 KB Output is correct
50 Correct 8 ms 13056 KB Output is correct
51 Correct 194 ms 42932 KB Output is correct
52 Correct 136 ms 47224 KB Output is correct
53 Correct 225 ms 44332 KB Output is correct
54 Correct 153 ms 42860 KB Output is correct
55 Correct 202 ms 42920 KB Output is correct
56 Correct 138 ms 47224 KB Output is correct
57 Correct 196 ms 44980 KB Output is correct
58 Correct 179 ms 43052 KB Output is correct
59 Correct 216 ms 42736 KB Output is correct
60 Correct 153 ms 47224 KB Output is correct
61 Correct 202 ms 44980 KB Output is correct
62 Correct 159 ms 42440 KB Output is correct
63 Correct 194 ms 42380 KB Output is correct
64 Correct 140 ms 47224 KB Output is correct
65 Correct 223 ms 44876 KB Output is correct
66 Correct 147 ms 42792 KB Output is correct
67 Correct 199 ms 42620 KB Output is correct
68 Correct 138 ms 47224 KB Output is correct
69 Correct 196 ms 44212 KB Output is correct
70 Correct 156 ms 42628 KB Output is correct