Submission #398041

# Submission time Handle Problem Language Result Execution time Memory
398041 2021-05-03T14:59:21 Z Charis02 Election Campaign (JOI15_election_campaign) C++14
100 / 100
515 ms 65192 KB
/*

https://oj.uz/problem/view/JOI15_election_campaign

100 points sol
*/

#include<iostream>
#include<vector>
#include<string.h>
#define ll long long
#define pi pair < ll,ll >
#define mp(a,b) make_pair(a,b)
#define MAX_N 200004

using namespace std;

vector < int > graph[MAX_N];
vector < pair < pi,ll > > tours[MAX_N];
ll jump_memo[MAX_N][21];
ll tin[MAX_N],tout[MAX_N],cur_time;
ll dp[MAX_N],S[MAX_N];
ll seg[4*MAX_N];
int n,m;

int jump(int x,int i) // find 2^i -th ancestor of x
{
    if(jump_memo[x][i] != -1)
        return jump_memo[x][i];
    return jump_memo[x][i] = jump(jump(x,i-1),i-1);
}

bool is_ancestor(int x,int y) // returns if x is ancestor of y
{
    return (tin[x] <= tin[y] && tout[x] >= tout[y]);
}

int get_lca(int x,int y)
{
    if(is_ancestor(x,y))
        return x;
    if(is_ancestor(y,x))
        return y;

    for(int i = 20;i >= 0;i--)
    {
        if(!is_ancestor(jump(x,i),y))
            x = jump(x,i);
    }

    return jump(x,0);
}

void dfs(int cur,int par)
{
    tin[cur] = cur_time++;
    jump_memo[cur][0] = par;

    for(int i = 0;i < graph[cur].size();i++)
    {
        int v = graph[cur][i];
        if(v == par)
            continue;
        dfs(v,cur);
    }

    tout[cur] = cur_time++;
    return;
}

void initialize()
{
    memset(jump_memo,-1,sizeof jump_memo);
    memset(dp,-1,sizeof dp);
    dfs(1,1);

    return;
}

void input()
{
    int a,b,c;

    cin >> n;

    for(int i = 0;i < n-1;i++)
    {
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    initialize();
    cin >> m;

    for(int i = 0;i < m;i++)
    {
        cin >> a >> b >> c;
        tours[get_lca(a,b)].push_back(mp(mp(a,b),c));
    }

    return;
}

void update(int low,int high,int pos,int slow,int val)
{
    if(low == high && low == slow)
    {
        seg[pos] = val;
        return;
    }
    if(low>slow||high<slow)
        return;

    int mid = (low+high)/2;

    update(low,mid,pos*2+1,slow,val);
    update(mid+1,high,pos*2+2,slow,val);

    seg[pos] = seg[pos*2+1]+seg[pos*2+2];
    return;
}

ll query(int low,int high,int pos,int slow,int shigh)
{
    if(low >= slow && high <= shigh)
        return seg[pos];
    if(low>shigh || high < slow)
        return 0;

    int mid = (low+high)/2;

    return query(low,mid,pos*2+1,slow,shigh) + query(mid+1,high,pos*2+2,slow,shigh);
}

ll go(int cur,int target) // get value to add on path cur->target
{
    return query(0,2*n,0,tin[cur],tin[target])+S[target];
}

ll calc(int cur,int par)
{
    if(dp[cur] != -1)
        return dp[cur];

    for(int i = 0;i < graph[cur].size();i++)
    {
        int v = graph[cur][i];
        if(v==par)
            continue;

        S[cur] += calc(v,cur); // S = sum of dp of children
    }

    dp[cur] = S[cur]; // for now. dp[cur] = S[cur] if we don't do a tour on cur

    for(int i = 0;i < graph[cur].size();i++)
    {
        int v = graph[cur][i];
        if(v==par)
            continue;

        update(0,2*n,0,tin[v],S[cur]-dp[v]);  // we hold this value to utilize it in the go function. look at O(NM) implementation
        update(0,2*n,0,tout[v],-S[cur]+dp[v]); // we add opposite value, to get path sum
    }

    for(int i = 0;i < tours[cur].size();i++)
    {
        int x = tours[cur][i].first.first; // first endpoint
        int y = tours[cur][i].first.second; // second endpoint
        int C = tours[cur][i].second; // gain of tour

        dp[cur] = max(dp[cur],go(cur,x)+go(cur,y) - S[cur] + C); // S[cur] is counted twice
    }

    return dp[cur];
}

void solve()
{
    cout << calc(1,1) << endl;

    return;
}

int main()
{
    input(); // read input and initialize tree
    solve(); // solve problem

    return 0;
}

Compilation message

election_campaign.cpp: In function 'void dfs(int, int)':
election_campaign.cpp:59:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |     for(int i = 0;i < graph[cur].size();i++)
      |                   ~~^~~~~~~~~~~~~~~~~~~
election_campaign.cpp: In function 'long long int calc(int, int)':
election_campaign.cpp:146:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  146 |     for(int i = 0;i < graph[cur].size();i++)
      |                   ~~^~~~~~~~~~~~~~~~~~~
election_campaign.cpp:157:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  157 |     for(int i = 0;i < graph[cur].size();i++)
      |                   ~~^~~~~~~~~~~~~~~~~~~
election_campaign.cpp:167:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  167 |     for(int i = 0;i < tours[cur].size();i++)
      |                   ~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 20 ms 44108 KB Output is correct
2 Correct 20 ms 44056 KB Output is correct
3 Correct 20 ms 44108 KB Output is correct
4 Correct 22 ms 44224 KB Output is correct
5 Correct 229 ms 53844 KB Output is correct
6 Correct 151 ms 61476 KB Output is correct
7 Correct 193 ms 58776 KB Output is correct
8 Correct 197 ms 53616 KB Output is correct
9 Correct 195 ms 57352 KB Output is correct
10 Correct 185 ms 53444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 44076 KB Output is correct
2 Correct 26 ms 44132 KB Output is correct
3 Correct 22 ms 44236 KB Output is correct
4 Correct 300 ms 65056 KB Output is correct
5 Correct 285 ms 65072 KB Output is correct
6 Correct 287 ms 65060 KB Output is correct
7 Correct 297 ms 65084 KB Output is correct
8 Correct 300 ms 65192 KB Output is correct
9 Correct 288 ms 65140 KB Output is correct
10 Correct 285 ms 65072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 44076 KB Output is correct
2 Correct 26 ms 44132 KB Output is correct
3 Correct 22 ms 44236 KB Output is correct
4 Correct 300 ms 65056 KB Output is correct
5 Correct 285 ms 65072 KB Output is correct
6 Correct 287 ms 65060 KB Output is correct
7 Correct 297 ms 65084 KB Output is correct
8 Correct 300 ms 65192 KB Output is correct
9 Correct 288 ms 65140 KB Output is correct
10 Correct 285 ms 65072 KB Output is correct
11 Correct 46 ms 45260 KB Output is correct
12 Correct 320 ms 65076 KB Output is correct
13 Correct 297 ms 64996 KB Output is correct
14 Correct 292 ms 65048 KB Output is correct
15 Correct 299 ms 65004 KB Output is correct
16 Correct 283 ms 65176 KB Output is correct
17 Correct 316 ms 64968 KB Output is correct
18 Correct 343 ms 65076 KB Output is correct
19 Correct 284 ms 65040 KB Output is correct
20 Correct 306 ms 65088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 374 ms 56824 KB Output is correct
2 Correct 279 ms 65112 KB Output is correct
3 Correct 486 ms 62080 KB Output is correct
4 Correct 336 ms 56696 KB Output is correct
5 Correct 406 ms 61460 KB Output is correct
6 Correct 363 ms 56420 KB Output is correct
7 Correct 515 ms 61020 KB Output is correct
8 Correct 382 ms 56880 KB Output is correct
9 Correct 270 ms 65060 KB Output is correct
10 Correct 429 ms 59916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 44108 KB Output is correct
2 Correct 20 ms 44056 KB Output is correct
3 Correct 20 ms 44108 KB Output is correct
4 Correct 22 ms 44224 KB Output is correct
5 Correct 229 ms 53844 KB Output is correct
6 Correct 151 ms 61476 KB Output is correct
7 Correct 193 ms 58776 KB Output is correct
8 Correct 197 ms 53616 KB Output is correct
9 Correct 195 ms 57352 KB Output is correct
10 Correct 185 ms 53444 KB Output is correct
11 Correct 23 ms 44252 KB Output is correct
12 Correct 22 ms 44364 KB Output is correct
13 Correct 23 ms 44264 KB Output is correct
14 Correct 23 ms 44292 KB Output is correct
15 Correct 23 ms 44228 KB Output is correct
16 Correct 23 ms 44192 KB Output is correct
17 Correct 23 ms 44228 KB Output is correct
18 Correct 23 ms 44236 KB Output is correct
19 Correct 23 ms 44216 KB Output is correct
20 Correct 22 ms 44348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 44108 KB Output is correct
2 Correct 20 ms 44056 KB Output is correct
3 Correct 20 ms 44108 KB Output is correct
4 Correct 22 ms 44224 KB Output is correct
5 Correct 229 ms 53844 KB Output is correct
6 Correct 151 ms 61476 KB Output is correct
7 Correct 193 ms 58776 KB Output is correct
8 Correct 197 ms 53616 KB Output is correct
9 Correct 195 ms 57352 KB Output is correct
10 Correct 185 ms 53444 KB Output is correct
11 Correct 21 ms 44076 KB Output is correct
12 Correct 26 ms 44132 KB Output is correct
13 Correct 22 ms 44236 KB Output is correct
14 Correct 300 ms 65056 KB Output is correct
15 Correct 285 ms 65072 KB Output is correct
16 Correct 287 ms 65060 KB Output is correct
17 Correct 297 ms 65084 KB Output is correct
18 Correct 300 ms 65192 KB Output is correct
19 Correct 288 ms 65140 KB Output is correct
20 Correct 285 ms 65072 KB Output is correct
21 Correct 46 ms 45260 KB Output is correct
22 Correct 320 ms 65076 KB Output is correct
23 Correct 297 ms 64996 KB Output is correct
24 Correct 292 ms 65048 KB Output is correct
25 Correct 299 ms 65004 KB Output is correct
26 Correct 283 ms 65176 KB Output is correct
27 Correct 316 ms 64968 KB Output is correct
28 Correct 343 ms 65076 KB Output is correct
29 Correct 284 ms 65040 KB Output is correct
30 Correct 306 ms 65088 KB Output is correct
31 Correct 374 ms 56824 KB Output is correct
32 Correct 279 ms 65112 KB Output is correct
33 Correct 486 ms 62080 KB Output is correct
34 Correct 336 ms 56696 KB Output is correct
35 Correct 406 ms 61460 KB Output is correct
36 Correct 363 ms 56420 KB Output is correct
37 Correct 515 ms 61020 KB Output is correct
38 Correct 382 ms 56880 KB Output is correct
39 Correct 270 ms 65060 KB Output is correct
40 Correct 429 ms 59916 KB Output is correct
41 Correct 23 ms 44252 KB Output is correct
42 Correct 22 ms 44364 KB Output is correct
43 Correct 23 ms 44264 KB Output is correct
44 Correct 23 ms 44292 KB Output is correct
45 Correct 23 ms 44228 KB Output is correct
46 Correct 23 ms 44192 KB Output is correct
47 Correct 23 ms 44228 KB Output is correct
48 Correct 23 ms 44236 KB Output is correct
49 Correct 23 ms 44216 KB Output is correct
50 Correct 22 ms 44348 KB Output is correct
51 Correct 405 ms 57056 KB Output is correct
52 Correct 299 ms 65096 KB Output is correct
53 Correct 511 ms 60120 KB Output is correct
54 Correct 331 ms 56412 KB Output is correct
55 Correct 419 ms 56792 KB Output is correct
56 Correct 307 ms 65092 KB Output is correct
57 Correct 457 ms 60920 KB Output is correct
58 Correct 380 ms 56480 KB Output is correct
59 Correct 385 ms 57084 KB Output is correct
60 Correct 297 ms 65092 KB Output is correct
61 Correct 432 ms 60968 KB Output is correct
62 Correct 364 ms 56084 KB Output is correct
63 Correct 390 ms 56528 KB Output is correct
64 Correct 291 ms 65092 KB Output is correct
65 Correct 449 ms 60780 KB Output is correct
66 Correct 341 ms 56340 KB Output is correct
67 Correct 408 ms 56692 KB Output is correct
68 Correct 309 ms 65036 KB Output is correct
69 Correct 446 ms 59696 KB Output is correct
70 Correct 340 ms 56376 KB Output is correct