Submission #154007

# Submission time Handle Problem Language Result Execution time Memory
154007 2019-09-17T17:06:42 Z georgerapeanu Election Campaign (JOI15_election_campaign) C++11
100 / 100
282 ms 29816 KB
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

const int NMAX = 1e5;
const int LGMAX = 17;

int n,q;
vector<int> graph[NMAX + 5];
vector<pair<pair<int,int>,int> > paths[NMAX + 5];

int lvl[NMAX + 5];
int fst[NMAX + 5];
int lst[NMAX + 5];
int last;

int father[LGMAX + 1][NMAX + 5];

int dp[NMAX + 5];
int aib[NMAX + 5];

void update(int pos,int val){
    for(;pos <= n;pos += (-pos) & pos){
        aib[pos] += val;
    }
}

int query(int pos){
    int ans = 0;
    for(;pos;pos -= (-pos) & pos){
        ans += aib[pos];
    }
    return ans;
}

void dfs(int nod,int tata){
    father[0][nod] = tata;
    lvl[nod] = 1 + lvl[tata];
    fst[nod] = ++last;

    for(auto it:graph[nod]){
        if(it == tata){
            continue;
        }
        dfs(it,nod);
    }

    lst[nod] = last;
}

void make_lca(){
    for(int h = 1;h <= LGMAX;h++){
        for(int i = 1;i <= n;i++){
            father[h][i] = father[h - 1][father[h - 1][i]];
        }
    }
}

int lca(int x,int y){
    if(lvl[x] > lvl[y]){
        swap(x,y);
    }
    
    int diff = (lvl[y] - lvl[x]);

    for(int h = LGMAX;h >= 0;h--){
        if((diff >> h) & 1){
            y = father[h][y];
        }
    }

    if(x == y){
        return x;
    }

    for(int h = LGMAX;h >= 0;h--){
        if(father[h][x] != father[h][y]){
            x = father[h][x];
            y = father[h][y];
        }
    }

    return father[0][x];
}

void dfs2(int nod,int tata){

    int tmp = 0;
    for(auto it:graph[nod]){
        if(it == tata){
            continue;
        }
        dfs2(it,nod);
        tmp += dp[it];
    }


    dp[nod] = tmp;
    
    for(auto it:graph[nod]){
        if(it == tata){
            continue;
        }
        update(fst[it],-dp[it]);
        update(lst[it] + 1,dp[it]);
    }

    for(auto it:paths[nod]){
        dp[nod] = max(dp[nod],tmp + query(fst[it.first.first]) + query(fst[it.first.second]) + it.second);
    }

    update(fst[nod],tmp);
    update(lst[nod] + 1,-tmp);
}

int main(){

    scanf("%d",&n);

    for(int i = 1;i < n;i++){
        int x,y;
        scanf("%d %d",&x,&y);
        graph[x].push_back(y);
        graph[y].push_back(x);
    }

    dfs(1,0);
    make_lca();

    scanf("%d",&q);

    while(q--){
        int a,b,c;
        scanf("%d %d %d",&a,&b,&c);
        paths[lca(a,b)].push_back({{a,b},c});
    }

    dfs2(1,0);

    printf("%d\n",dp[1]);

    return 0;
}

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:120:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&n);
     ~~~~~^~~~~~~~~
election_campaign.cpp:124:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&x,&y);
         ~~~~~^~~~~~~~~~~~~~~
election_campaign.cpp:132:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&q);
     ~~~~~^~~~~~~~~
election_campaign.cpp:136:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d",&a,&b,&c);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5116 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 8 ms 5112 KB Output is correct
4 Correct 7 ms 5240 KB Output is correct
5 Correct 116 ms 18552 KB Output is correct
6 Correct 67 ms 26104 KB Output is correct
7 Correct 111 ms 23504 KB Output is correct
8 Correct 94 ms 18824 KB Output is correct
9 Correct 113 ms 21812 KB Output is correct
10 Correct 96 ms 18808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 7 ms 5416 KB Output is correct
4 Correct 143 ms 29492 KB Output is correct
5 Correct 148 ms 29444 KB Output is correct
6 Correct 141 ms 29432 KB Output is correct
7 Correct 149 ms 29432 KB Output is correct
8 Correct 169 ms 29432 KB Output is correct
9 Correct 154 ms 29444 KB Output is correct
10 Correct 148 ms 29576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 7 ms 5416 KB Output is correct
4 Correct 143 ms 29492 KB Output is correct
5 Correct 148 ms 29444 KB Output is correct
6 Correct 141 ms 29432 KB Output is correct
7 Correct 149 ms 29432 KB Output is correct
8 Correct 169 ms 29432 KB Output is correct
9 Correct 154 ms 29444 KB Output is correct
10 Correct 148 ms 29576 KB Output is correct
11 Correct 20 ms 6124 KB Output is correct
12 Correct 180 ms 29816 KB Output is correct
13 Correct 160 ms 29688 KB Output is correct
14 Correct 145 ms 29816 KB Output is correct
15 Correct 158 ms 29664 KB Output is correct
16 Correct 144 ms 29688 KB Output is correct
17 Correct 151 ms 29688 KB Output is correct
18 Correct 155 ms 29688 KB Output is correct
19 Correct 163 ms 29792 KB Output is correct
20 Correct 153 ms 29816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 187 ms 21192 KB Output is correct
2 Correct 136 ms 29432 KB Output is correct
3 Correct 227 ms 26360 KB Output is correct
4 Correct 148 ms 21680 KB Output is correct
5 Correct 219 ms 25848 KB Output is correct
6 Correct 149 ms 21612 KB Output is correct
7 Correct 254 ms 25524 KB Output is correct
8 Correct 198 ms 21452 KB Output is correct
9 Correct 139 ms 29588 KB Output is correct
10 Correct 227 ms 24752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5116 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 8 ms 5112 KB Output is correct
4 Correct 7 ms 5240 KB Output is correct
5 Correct 116 ms 18552 KB Output is correct
6 Correct 67 ms 26104 KB Output is correct
7 Correct 111 ms 23504 KB Output is correct
8 Correct 94 ms 18824 KB Output is correct
9 Correct 113 ms 21812 KB Output is correct
10 Correct 96 ms 18808 KB Output is correct
11 Correct 8 ms 5240 KB Output is correct
12 Correct 7 ms 5368 KB Output is correct
13 Correct 7 ms 5240 KB Output is correct
14 Correct 7 ms 5244 KB Output is correct
15 Correct 9 ms 5240 KB Output is correct
16 Correct 7 ms 5240 KB Output is correct
17 Correct 7 ms 5368 KB Output is correct
18 Correct 8 ms 5368 KB Output is correct
19 Correct 7 ms 5244 KB Output is correct
20 Correct 8 ms 5364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5116 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 8 ms 5112 KB Output is correct
4 Correct 7 ms 5240 KB Output is correct
5 Correct 116 ms 18552 KB Output is correct
6 Correct 67 ms 26104 KB Output is correct
7 Correct 111 ms 23504 KB Output is correct
8 Correct 94 ms 18824 KB Output is correct
9 Correct 113 ms 21812 KB Output is correct
10 Correct 96 ms 18808 KB Output is correct
11 Correct 8 ms 5112 KB Output is correct
12 Correct 6 ms 5112 KB Output is correct
13 Correct 7 ms 5416 KB Output is correct
14 Correct 143 ms 29492 KB Output is correct
15 Correct 148 ms 29444 KB Output is correct
16 Correct 141 ms 29432 KB Output is correct
17 Correct 149 ms 29432 KB Output is correct
18 Correct 169 ms 29432 KB Output is correct
19 Correct 154 ms 29444 KB Output is correct
20 Correct 148 ms 29576 KB Output is correct
21 Correct 20 ms 6124 KB Output is correct
22 Correct 180 ms 29816 KB Output is correct
23 Correct 160 ms 29688 KB Output is correct
24 Correct 145 ms 29816 KB Output is correct
25 Correct 158 ms 29664 KB Output is correct
26 Correct 144 ms 29688 KB Output is correct
27 Correct 151 ms 29688 KB Output is correct
28 Correct 155 ms 29688 KB Output is correct
29 Correct 163 ms 29792 KB Output is correct
30 Correct 153 ms 29816 KB Output is correct
31 Correct 187 ms 21192 KB Output is correct
32 Correct 136 ms 29432 KB Output is correct
33 Correct 227 ms 26360 KB Output is correct
34 Correct 148 ms 21680 KB Output is correct
35 Correct 219 ms 25848 KB Output is correct
36 Correct 149 ms 21612 KB Output is correct
37 Correct 254 ms 25524 KB Output is correct
38 Correct 198 ms 21452 KB Output is correct
39 Correct 139 ms 29588 KB Output is correct
40 Correct 227 ms 24752 KB Output is correct
41 Correct 8 ms 5240 KB Output is correct
42 Correct 7 ms 5368 KB Output is correct
43 Correct 7 ms 5240 KB Output is correct
44 Correct 7 ms 5244 KB Output is correct
45 Correct 9 ms 5240 KB Output is correct
46 Correct 7 ms 5240 KB Output is correct
47 Correct 7 ms 5368 KB Output is correct
48 Correct 8 ms 5368 KB Output is correct
49 Correct 7 ms 5244 KB Output is correct
50 Correct 8 ms 5364 KB Output is correct
51 Correct 197 ms 21808 KB Output is correct
52 Correct 150 ms 29688 KB Output is correct
53 Correct 257 ms 25008 KB Output is correct
54 Correct 155 ms 21940 KB Output is correct
55 Correct 198 ms 21676 KB Output is correct
56 Correct 146 ms 29688 KB Output is correct
57 Correct 203 ms 25720 KB Output is correct
58 Correct 152 ms 21912 KB Output is correct
59 Correct 255 ms 21752 KB Output is correct
60 Correct 160 ms 29812 KB Output is correct
61 Correct 225 ms 25848 KB Output is correct
62 Correct 152 ms 21696 KB Output is correct
63 Correct 202 ms 21324 KB Output is correct
64 Correct 190 ms 29688 KB Output is correct
65 Correct 282 ms 25700 KB Output is correct
66 Correct 149 ms 21940 KB Output is correct
67 Correct 201 ms 21484 KB Output is correct
68 Correct 152 ms 29780 KB Output is correct
69 Correct 262 ms 24568 KB Output is correct
70 Correct 152 ms 22108 KB Output is correct