Submission #198653

# Submission time Handle Problem Language Result Execution time Memory
198653 2020-01-27T08:22:07 Z osaaateiasavtnl Election Campaign (JOI15_election_campaign) C++14
100 / 100
353 ms 44792 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 1e5 + 7, LG = 20;
vector <int> g[N];
vector <int> mem[N];
int to[N][LG];
int tin[N], tout[N], timer = 0;

int in = 0, out = 0, in_pos[N], out_pos[N], r[N];

void dfs(int u, int p) {
    to[u][0] = p;
    for (int i = 1; i < LG; ++i)
        to[u][i] = to[to[u][i - 1]][i - 1];
    r[u] = out - 1;
    tin[u] = timer++;        
    in_pos[u] = in++;
    for (int v : g[u]) 
        if (v != p)
            dfs(v, u);
    tout[u] = timer++;
    out_pos[u] = out++;
}   
bool anc(int u, int v) {
    return tin[u] <= tin[v] && tout[v] <= tout[u];
}   
int lca(int u, int v) {
    if (anc(u, v))
        return u;
    for (int i = LG - 1; i >= 0; --i) 
        if (!anc(to[u][i], v))
            u = to[u][i];
    return to[u][0];
}
int dp[N];  
struct Quer {
    int u, v, c;
} d[N];  

void add(int i, int x, int *f) {
    for (; i < N; i |= i + 1)
        f[i] += x;
}   
int get(int i, int *f) {
    int ans = 0;
    for (; i >= 0; i &= i + 1, --i)
        ans += f[i];    
    return ans;
}   
int get(int l, int r, int *f) {
    return get(r, f) - get(l - 1, f);
}   

int in_f[N], out_f[N];

int get(int u) {
    return get(in_pos[u], in_f) - get(r[u], out_f);
}   
int get(int u, int p) {
    return get(u) - get(p);
}   
void solve(int u, int p) {
    int sum = 0;
    for (int v : g[u])
        if (v != p) {
            solve(v, u);
            dp[u] += dp[v];
            sum += dp[v];
        }
    for (int i : mem[u]) 
        dp[u] = max(dp[u], get(d[i].u, u) + get(d[i].v, u) + sum + d[i].c);
    sum -= dp[u];
    add(in_pos[u], sum, in_f);
    add(out_pos[u], sum, out_f);
}
signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    int n;
    cin >> n;    
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        g[u].app(v); g[v].app(u);
    }   
    dfs(1, 1);
    int m;
    cin >> m;
    for (int i = 0; i < m; ++i) {
        cin >> d[i].u >> d[i].v >> d[i].c;
        mem[lca(d[i].u, d[i].v)].app(i);                        
    }
    solve(1, 1);
    #ifdef HOME
    for (int i = 1; i <= n; ++i)
        cout << dp[i] << ' ';
    cout << '\n';
    #endif
    cout << dp[1] << '\n';
}   
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 8 ms 5112 KB Output is correct
3 Correct 8 ms 5112 KB Output is correct
4 Correct 10 ms 5368 KB Output is correct
5 Correct 153 ms 30840 KB Output is correct
6 Correct 84 ms 37968 KB Output is correct
7 Correct 151 ms 35576 KB Output is correct
8 Correct 100 ms 30216 KB Output is correct
9 Correct 153 ms 34040 KB Output is correct
10 Correct 111 ms 30284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 8 ms 5112 KB Output is correct
3 Correct 10 ms 5496 KB Output is correct
4 Correct 209 ms 41912 KB Output is correct
5 Correct 202 ms 44280 KB Output is correct
6 Correct 164 ms 44408 KB Output is correct
7 Correct 207 ms 44408 KB Output is correct
8 Correct 209 ms 44408 KB Output is correct
9 Correct 159 ms 44408 KB Output is correct
10 Correct 202 ms 44536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 8 ms 5112 KB Output is correct
3 Correct 10 ms 5496 KB Output is correct
4 Correct 209 ms 41912 KB Output is correct
5 Correct 202 ms 44280 KB Output is correct
6 Correct 164 ms 44408 KB Output is correct
7 Correct 207 ms 44408 KB Output is correct
8 Correct 209 ms 44408 KB Output is correct
9 Correct 159 ms 44408 KB Output is correct
10 Correct 202 ms 44536 KB Output is correct
11 Correct 21 ms 6648 KB Output is correct
12 Correct 206 ms 44640 KB Output is correct
13 Correct 207 ms 44640 KB Output is correct
14 Correct 166 ms 44664 KB Output is correct
15 Correct 202 ms 44540 KB Output is correct
16 Correct 168 ms 44792 KB Output is correct
17 Correct 205 ms 44536 KB Output is correct
18 Correct 216 ms 44536 KB Output is correct
19 Correct 170 ms 44792 KB Output is correct
20 Correct 215 ms 44684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 231 ms 34288 KB Output is correct
2 Correct 162 ms 41848 KB Output is correct
3 Correct 353 ms 41592 KB Output is correct
4 Correct 186 ms 36208 KB Output is correct
5 Correct 314 ms 41324 KB Output is correct
6 Correct 183 ms 36208 KB Output is correct
7 Correct 351 ms 40944 KB Output is correct
8 Correct 278 ms 36984 KB Output is correct
9 Correct 172 ms 44408 KB Output is correct
10 Correct 341 ms 40040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 8 ms 5112 KB Output is correct
3 Correct 8 ms 5112 KB Output is correct
4 Correct 10 ms 5368 KB Output is correct
5 Correct 153 ms 30840 KB Output is correct
6 Correct 84 ms 37968 KB Output is correct
7 Correct 151 ms 35576 KB Output is correct
8 Correct 100 ms 30216 KB Output is correct
9 Correct 153 ms 34040 KB Output is correct
10 Correct 111 ms 30284 KB Output is correct
11 Correct 9 ms 5368 KB Output is correct
12 Correct 9 ms 5496 KB Output is correct
13 Correct 9 ms 5496 KB Output is correct
14 Correct 9 ms 5368 KB Output is correct
15 Correct 9 ms 5368 KB Output is correct
16 Correct 10 ms 5496 KB Output is correct
17 Correct 9 ms 5496 KB Output is correct
18 Correct 10 ms 5496 KB Output is correct
19 Correct 9 ms 5496 KB Output is correct
20 Correct 10 ms 5496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 8 ms 5112 KB Output is correct
3 Correct 8 ms 5112 KB Output is correct
4 Correct 10 ms 5368 KB Output is correct
5 Correct 153 ms 30840 KB Output is correct
6 Correct 84 ms 37968 KB Output is correct
7 Correct 151 ms 35576 KB Output is correct
8 Correct 100 ms 30216 KB Output is correct
9 Correct 153 ms 34040 KB Output is correct
10 Correct 111 ms 30284 KB Output is correct
11 Correct 8 ms 5112 KB Output is correct
12 Correct 8 ms 5112 KB Output is correct
13 Correct 10 ms 5496 KB Output is correct
14 Correct 209 ms 41912 KB Output is correct
15 Correct 202 ms 44280 KB Output is correct
16 Correct 164 ms 44408 KB Output is correct
17 Correct 207 ms 44408 KB Output is correct
18 Correct 209 ms 44408 KB Output is correct
19 Correct 159 ms 44408 KB Output is correct
20 Correct 202 ms 44536 KB Output is correct
21 Correct 21 ms 6648 KB Output is correct
22 Correct 206 ms 44640 KB Output is correct
23 Correct 207 ms 44640 KB Output is correct
24 Correct 166 ms 44664 KB Output is correct
25 Correct 202 ms 44540 KB Output is correct
26 Correct 168 ms 44792 KB Output is correct
27 Correct 205 ms 44536 KB Output is correct
28 Correct 216 ms 44536 KB Output is correct
29 Correct 170 ms 44792 KB Output is correct
30 Correct 215 ms 44684 KB Output is correct
31 Correct 231 ms 34288 KB Output is correct
32 Correct 162 ms 41848 KB Output is correct
33 Correct 353 ms 41592 KB Output is correct
34 Correct 186 ms 36208 KB Output is correct
35 Correct 314 ms 41324 KB Output is correct
36 Correct 183 ms 36208 KB Output is correct
37 Correct 351 ms 40944 KB Output is correct
38 Correct 278 ms 36984 KB Output is correct
39 Correct 172 ms 44408 KB Output is correct
40 Correct 341 ms 40040 KB Output is correct
41 Correct 9 ms 5368 KB Output is correct
42 Correct 9 ms 5496 KB Output is correct
43 Correct 9 ms 5496 KB Output is correct
44 Correct 9 ms 5368 KB Output is correct
45 Correct 9 ms 5368 KB Output is correct
46 Correct 10 ms 5496 KB Output is correct
47 Correct 9 ms 5496 KB Output is correct
48 Correct 10 ms 5496 KB Output is correct
49 Correct 9 ms 5496 KB Output is correct
50 Correct 10 ms 5496 KB Output is correct
51 Correct 278 ms 37240 KB Output is correct
52 Correct 219 ms 44560 KB Output is correct
53 Correct 323 ms 40300 KB Output is correct
54 Correct 193 ms 36428 KB Output is correct
55 Correct 258 ms 36976 KB Output is correct
56 Correct 210 ms 44616 KB Output is correct
57 Correct 300 ms 41080 KB Output is correct
58 Correct 186 ms 36332 KB Output is correct
59 Correct 283 ms 37220 KB Output is correct
60 Correct 234 ms 44536 KB Output is correct
61 Correct 346 ms 41208 KB Output is correct
62 Correct 208 ms 36264 KB Output is correct
63 Correct 243 ms 37056 KB Output is correct
64 Correct 219 ms 44664 KB Output is correct
65 Correct 346 ms 41072 KB Output is correct
66 Correct 226 ms 36468 KB Output is correct
67 Correct 246 ms 36948 KB Output is correct
68 Correct 224 ms 44776 KB Output is correct
69 Correct 298 ms 39928 KB Output is correct
70 Correct 183 ms 36448 KB Output is correct