Submission #248943

# Submission time Handle Problem Language Result Execution time Memory
248943 2020-07-13T18:30:18 Z Richem Election Campaign (JOI15_election_campaign) C++14
100 / 100
359 ms 31480 KB
#include <bits/stdc++.h>
using namespace std;

const int MAX_NB = 1e5, MAX_PUISSANCE = 19, INFINI = -1e9;

int nbSommet, nbChemin;
int nbFeuille;

vector<int> adj[MAX_NB];

void input() {
    cin >> nbSommet;

    nbFeuille = (1 << ((int)ceil(log2(nbSommet*2))));

    for(int arete = 0; arete < nbSommet-1; arete++) {
        int u,v;
        cin >> u >> v;
        u--; v--;

        adj[u].push_back(v);
        adj[v].push_back(u);
    }
}

int bl[MAX_NB][MAX_PUISSANCE];
int temps[MAX_NB][2];
int tempsDfs = 0;

int dfs(int sommet, int preced) {
    bl[sommet][0] = preced;
    for(int exp = 1; exp < MAX_PUISSANCE; exp++) {
        bl[sommet][exp] = -1;
        if(bl[sommet][exp-1] != -1) {
            bl[sommet][exp] = bl[bl[sommet][exp-1]][exp-1];
        }
    }

    temps[sommet][0] = tempsDfs++;

    for(auto fils : adj[sommet]) {
        if(fils != preced) {
            dfs(fils, sommet);
        }
    }

    temps[sommet][1] = tempsDfs++;
}

bool pere(int u, int v) {
    return (temps[u][0] <= temps[v][0] && temps[u][1] >= temps[v][1]);
}

int lca(int u, int v) {
    if(pere(u, v))
        return u;
    if(pere(v, u))
        return v;
    for(int exp = MAX_PUISSANCE-1; exp >= 0; exp--) {
        if(bl[u][exp] != -1 && !pere(bl[u][exp], v)) {
            u = bl[u][exp];
        }
    }

    return bl[u][0];
}

int arbre[(1 << MAX_PUISSANCE)] = {0};


void modif(int idSommet, int valeur) {
    int id[2] = {temps[idSommet][0]+nbFeuille, temps[idSommet][1]+nbFeuille};
    int nombre[2] = {valeur, -valeur};

    for(int i = 0; i < 2; i++) {
        arbre[id[i]] = nombre[i];
        id[i] /= 2;
        for(; id[i] > 0; id[i] /= 2) {
            arbre[id[i]] = arbre[id[i]*2] + arbre[id[i]*2+1];
        }
    }
}

int somme(int debut, int fin) {
    int total = 0, requete[2][2] = {{temps[lca(debut, fin)][0], temps[debut][0]}, {temps[lca(debut, fin)][0], temps[fin][0]}};

    for(int i = 0; i < 2; i++) {
        int gauche = requete[i][0]+nbFeuille, droite = requete[i][1]+nbFeuille;

        while(gauche <= droite) {
            if(gauche%2==1) {
                total += arbre[gauche];
                gauche++;
            }
            if(droite % 2 == 0) {
                total += arbre[droite];
                droite--;
            }
            gauche /= 2; droite /= 2;
        }
    }

    return total;
}

struct Chemin{
    int u, v, nombre;
};

vector<Chemin> chemin[MAX_NB];

void inputChemin() {
    cin >> nbChemin;

    for(int i = 0; i < nbChemin; i++) {
        int a,b,c;
        cin >> a >> b >> c;
        a--; b--;
        chemin[lca(a, b)].push_back({a, b, c});
    }
}

int dp[MAX_NB][2];

void calculerDp(int sommet, int preced) {
    if(dp[sommet][0] != INFINI)
        return;

    dp[sommet][0] = 0;

    for(auto fils : adj[sommet]) {
        if(fils != preced) {
            calculerDp(fils, sommet);
            dp[sommet][0] += dp[fils][1];
        }
    }

    dp[sommet][1] = dp[sommet][0];

    for(auto path : chemin[sommet]) {
        dp[sommet][1] = max(dp[sommet][1], dp[sommet][0] + path.nombre + somme(sommet, path.u) + somme(sommet, path.v) - 2 * somme(sommet, sommet));
    }

    modif(sommet, dp[sommet][0] - dp[sommet][1]);
}

int main() {

    for(int i = 0; i < MAX_NB; i++) dp[i][0] = dp[i][1] = INFINI;

    input();
    dfs(0, -1);
    inputChemin();

    calculerDp(0, -1);

    cout << max(dp[0][0], dp[0][1]);
}

Compilation message

election_campaign.cpp: In function 'int dfs(int, int)':
election_campaign.cpp:48:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5760 KB Output is correct
2 Correct 3 ms 5760 KB Output is correct
3 Correct 4 ms 5760 KB Output is correct
4 Correct 5 ms 6016 KB Output is correct
5 Correct 162 ms 20092 KB Output is correct
6 Correct 125 ms 27640 KB Output is correct
7 Correct 160 ms 24952 KB Output is correct
8 Correct 138 ms 20472 KB Output is correct
9 Correct 159 ms 23416 KB Output is correct
10 Correct 135 ms 20344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5760 KB Output is correct
2 Correct 4 ms 5760 KB Output is correct
3 Correct 5 ms 6016 KB Output is correct
4 Correct 259 ms 30968 KB Output is correct
5 Correct 253 ms 30968 KB Output is correct
6 Correct 250 ms 30968 KB Output is correct
7 Correct 248 ms 30968 KB Output is correct
8 Correct 249 ms 30968 KB Output is correct
9 Correct 246 ms 31096 KB Output is correct
10 Correct 251 ms 31024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5760 KB Output is correct
2 Correct 4 ms 5760 KB Output is correct
3 Correct 5 ms 6016 KB Output is correct
4 Correct 259 ms 30968 KB Output is correct
5 Correct 253 ms 30968 KB Output is correct
6 Correct 250 ms 30968 KB Output is correct
7 Correct 248 ms 30968 KB Output is correct
8 Correct 249 ms 30968 KB Output is correct
9 Correct 246 ms 31096 KB Output is correct
10 Correct 251 ms 31024 KB Output is correct
11 Correct 31 ms 6784 KB Output is correct
12 Correct 272 ms 31360 KB Output is correct
13 Correct 259 ms 31352 KB Output is correct
14 Correct 249 ms 31352 KB Output is correct
15 Correct 275 ms 31480 KB Output is correct
16 Correct 250 ms 31352 KB Output is correct
17 Correct 260 ms 31224 KB Output is correct
18 Correct 268 ms 31352 KB Output is correct
19 Correct 258 ms 31352 KB Output is correct
20 Correct 267 ms 31356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 301 ms 22832 KB Output is correct
2 Correct 244 ms 30968 KB Output is correct
3 Correct 356 ms 28024 KB Output is correct
4 Correct 287 ms 23216 KB Output is correct
5 Correct 325 ms 27640 KB Output is correct
6 Correct 270 ms 23144 KB Output is correct
7 Correct 339 ms 27188 KB Output is correct
8 Correct 309 ms 23160 KB Output is correct
9 Correct 238 ms 30988 KB Output is correct
10 Correct 347 ms 26452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5760 KB Output is correct
2 Correct 3 ms 5760 KB Output is correct
3 Correct 4 ms 5760 KB Output is correct
4 Correct 5 ms 6016 KB Output is correct
5 Correct 162 ms 20092 KB Output is correct
6 Correct 125 ms 27640 KB Output is correct
7 Correct 160 ms 24952 KB Output is correct
8 Correct 138 ms 20472 KB Output is correct
9 Correct 159 ms 23416 KB Output is correct
10 Correct 135 ms 20344 KB Output is correct
11 Correct 5 ms 6016 KB Output is correct
12 Correct 6 ms 6016 KB Output is correct
13 Correct 5 ms 6016 KB Output is correct
14 Correct 6 ms 6016 KB Output is correct
15 Correct 6 ms 6016 KB Output is correct
16 Correct 6 ms 6016 KB Output is correct
17 Correct 6 ms 6016 KB Output is correct
18 Correct 7 ms 6016 KB Output is correct
19 Correct 5 ms 6016 KB Output is correct
20 Correct 6 ms 6016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5760 KB Output is correct
2 Correct 3 ms 5760 KB Output is correct
3 Correct 4 ms 5760 KB Output is correct
4 Correct 5 ms 6016 KB Output is correct
5 Correct 162 ms 20092 KB Output is correct
6 Correct 125 ms 27640 KB Output is correct
7 Correct 160 ms 24952 KB Output is correct
8 Correct 138 ms 20472 KB Output is correct
9 Correct 159 ms 23416 KB Output is correct
10 Correct 135 ms 20344 KB Output is correct
11 Correct 4 ms 5760 KB Output is correct
12 Correct 4 ms 5760 KB Output is correct
13 Correct 5 ms 6016 KB Output is correct
14 Correct 259 ms 30968 KB Output is correct
15 Correct 253 ms 30968 KB Output is correct
16 Correct 250 ms 30968 KB Output is correct
17 Correct 248 ms 30968 KB Output is correct
18 Correct 249 ms 30968 KB Output is correct
19 Correct 246 ms 31096 KB Output is correct
20 Correct 251 ms 31024 KB Output is correct
21 Correct 31 ms 6784 KB Output is correct
22 Correct 272 ms 31360 KB Output is correct
23 Correct 259 ms 31352 KB Output is correct
24 Correct 249 ms 31352 KB Output is correct
25 Correct 275 ms 31480 KB Output is correct
26 Correct 250 ms 31352 KB Output is correct
27 Correct 260 ms 31224 KB Output is correct
28 Correct 268 ms 31352 KB Output is correct
29 Correct 258 ms 31352 KB Output is correct
30 Correct 267 ms 31356 KB Output is correct
31 Correct 301 ms 22832 KB Output is correct
32 Correct 244 ms 30968 KB Output is correct
33 Correct 356 ms 28024 KB Output is correct
34 Correct 287 ms 23216 KB Output is correct
35 Correct 325 ms 27640 KB Output is correct
36 Correct 270 ms 23144 KB Output is correct
37 Correct 339 ms 27188 KB Output is correct
38 Correct 309 ms 23160 KB Output is correct
39 Correct 238 ms 30988 KB Output is correct
40 Correct 347 ms 26452 KB Output is correct
41 Correct 5 ms 6016 KB Output is correct
42 Correct 6 ms 6016 KB Output is correct
43 Correct 5 ms 6016 KB Output is correct
44 Correct 6 ms 6016 KB Output is correct
45 Correct 6 ms 6016 KB Output is correct
46 Correct 6 ms 6016 KB Output is correct
47 Correct 6 ms 6016 KB Output is correct
48 Correct 7 ms 6016 KB Output is correct
49 Correct 5 ms 6016 KB Output is correct
50 Correct 6 ms 6016 KB Output is correct
51 Correct 316 ms 23324 KB Output is correct
52 Correct 267 ms 31224 KB Output is correct
53 Correct 359 ms 26544 KB Output is correct
54 Correct 273 ms 23476 KB Output is correct
55 Correct 325 ms 23296 KB Output is correct
56 Correct 263 ms 31352 KB Output is correct
57 Correct 324 ms 27384 KB Output is correct
58 Correct 282 ms 23476 KB Output is correct
59 Correct 305 ms 23416 KB Output is correct
60 Correct 269 ms 31352 KB Output is correct
61 Correct 317 ms 27384 KB Output is correct
62 Correct 291 ms 23524 KB Output is correct
63 Correct 313 ms 22888 KB Output is correct
64 Correct 263 ms 31352 KB Output is correct
65 Correct 345 ms 27184 KB Output is correct
66 Correct 280 ms 23476 KB Output is correct
67 Correct 318 ms 23184 KB Output is correct
68 Correct 267 ms 31228 KB Output is correct
69 Correct 323 ms 26232 KB Output is correct
70 Correct 282 ms 23644 KB Output is correct