Submission #25424

# Submission time Handle Problem Language Result Execution time Memory
25424 2017-06-22T06:36:56 Z tlwpdus Election Campaign (JOI15_election_campaign) C++14
100 / 100
486 ms 42288 KB
#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> pii;

struct plan {
    int a, b, c;
    plan(int a, int b, int c):a(a),b(b),c(c){}
};

int n, m;
vector<int> lis[100100];
vector<plan> pl[100100];
int sz[100100];
map<int,int> *E[100100];
int D[100100];
int bias[100100];
int sps[100100][20];
int dep[100100];

void idfs(int here, int p, int d) {
    int i;
    sps[here][0] = p;
    dep[here] = d;
    for (i=0;i<lis[here].size();i++) {
        int there = lis[here][i];
        if (there==p) continue;
        idfs(there,here,d+1);
    }
}

void init() {
    int i, j;
    for (j=1;j<20;j++) {
        for (i=0;i<n;i++) sps[i][j] = ((~sps[i][j-1])?sps[sps[i][j-1]][j-1]:-1);
    }
}

int lca(int u, int v) {
    if (dep[u]>dep[v]) swap(u,v);
    int td = dep[v]-dep[u];
    int i;
    for (i=0;i<20;i++) {
        if ((td>>i)&1) v = sps[v][i];
    }
    if (u==v) return u;
    for (i=19;i>=0;i--) {
        if (sps[u][i]==sps[v][i]) continue;
        u=sps[u][i];v=sps[v][i];
    }
    return sps[u][0];
}

void dfs(int here, int p) {
    int i, maxi = -1, t = -1, dsum = 0;
    sz[here] = 1;
    for (i=0;i<lis[here].size();i++) {
        int there = lis[here][i];
        if (there==p) continue;
        dfs(there,here);
        if (maxi<sz[there]) {
            maxi=sz[there];
            t=there;
        }
        dsum += D[there];
        sz[here]+=sz[there];
    }
    if (~t) {
        E[here] = E[t];
        bias[here] = bias[t]+dsum-D[t];
    }
    else {
        E[here] = new map<int,int>();
        E[here]->insert(pii(here,0));
    }
    for (i=0;i<lis[here].size();i++) {
        int there = lis[here][i];
        if (there==p||there==t) continue;
        map<int,int>::iterator it = E[there]->begin();
        while(it!=E[there]->end()) {
            E[here]->insert(pii((*it).first,(*it).second+bias[there]-bias[here]+dsum-D[there]));
            it++;
        }
        E[there]->clear();
    }
    E[here]->insert(pii(here,dsum-bias[here]));
    D[here] = dsum;
    for (i=0;i<pl[here].size();i++) {
        D[here] = max(D[here],pl[here][i].c+(*E[here])[pl[here][i].a]+bias[here]+(*E[here])[pl[here][i].b]+bias[here]-dsum);
    }
}

int main() {
    int i;

    scanf("%d",&n);
    for (i=0;i<n-1;i++) {
        int x, y;
        scanf("%d%d",&x,&y);
        x--;y--;
        lis[x].push_back(y);
        lis[y].push_back(x);
    }
    idfs(0,-1,0);
    init();
    scanf("%d",&m);
    for (i=0;i<m;i++) {
        int a, b, c;
        scanf("%d%d%d",&a,&b,&c);
        a--;b--;
        pl[lca(a,b)].push_back(plan(a,b,c));
    }
    dfs(0,-1);
    printf("%d\n",D[0]);

    return 0;
}

Compilation message

election_campaign.cpp: In function 'void idfs(int, int, int)':
election_campaign.cpp:26:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i=0;i<lis[here].size();i++) {
               ^
election_campaign.cpp: In function 'void dfs(int, int)':
election_campaign.cpp:58:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i=0;i<lis[here].size();i++) {
               ^
election_campaign.cpp:77:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i=0;i<lis[here].size();i++) {
               ^
election_campaign.cpp:89:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i=0;i<pl[here].size();i++) {
               ^
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:97:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&n);
                   ^
election_campaign.cpp:100:28: 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:107:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&m);
                   ^
election_campaign.cpp:110:33: 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 3 ms 16884 KB Output is correct
2 Correct 3 ms 16884 KB Output is correct
3 Correct 3 ms 16884 KB Output is correct
4 Correct 3 ms 17016 KB Output is correct
5 Correct 263 ms 29424 KB Output is correct
6 Correct 86 ms 40172 KB Output is correct
7 Correct 189 ms 36488 KB Output is correct
8 Correct 249 ms 31660 KB Output is correct
9 Correct 219 ms 34712 KB Output is correct
10 Correct 229 ms 32524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 16884 KB Output is correct
2 Correct 0 ms 16884 KB Output is correct
3 Correct 0 ms 17048 KB Output is correct
4 Correct 296 ms 42152 KB Output is correct
5 Correct 179 ms 42156 KB Output is correct
6 Correct 239 ms 42156 KB Output is correct
7 Correct 293 ms 42148 KB Output is correct
8 Correct 276 ms 42152 KB Output is correct
9 Correct 223 ms 42152 KB Output is correct
10 Correct 289 ms 42156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 16884 KB Output is correct
2 Correct 0 ms 16884 KB Output is correct
3 Correct 0 ms 17048 KB Output is correct
4 Correct 296 ms 42152 KB Output is correct
5 Correct 179 ms 42156 KB Output is correct
6 Correct 239 ms 42156 KB Output is correct
7 Correct 293 ms 42148 KB Output is correct
8 Correct 276 ms 42152 KB Output is correct
9 Correct 223 ms 42152 KB Output is correct
10 Correct 289 ms 42156 KB Output is correct
11 Correct 16 ms 17448 KB Output is correct
12 Correct 243 ms 42152 KB Output is correct
13 Correct 249 ms 42152 KB Output is correct
14 Correct 226 ms 42288 KB Output is correct
15 Correct 249 ms 42152 KB Output is correct
16 Correct 216 ms 42152 KB Output is correct
17 Correct 283 ms 42152 KB Output is correct
18 Correct 259 ms 42152 KB Output is correct
19 Correct 233 ms 42148 KB Output is correct
20 Correct 293 ms 42156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 419 ms 31164 KB Output is correct
2 Correct 239 ms 42152 KB Output is correct
3 Correct 389 ms 38056 KB Output is correct
4 Correct 366 ms 33600 KB Output is correct
5 Correct 366 ms 37568 KB Output is correct
6 Correct 356 ms 33536 KB Output is correct
7 Correct 439 ms 37256 KB Output is correct
8 Correct 386 ms 30744 KB Output is correct
9 Correct 216 ms 42152 KB Output is correct
10 Correct 486 ms 36448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16884 KB Output is correct
2 Correct 3 ms 16884 KB Output is correct
3 Correct 3 ms 16884 KB Output is correct
4 Correct 3 ms 17016 KB Output is correct
5 Correct 263 ms 29424 KB Output is correct
6 Correct 86 ms 40172 KB Output is correct
7 Correct 189 ms 36488 KB Output is correct
8 Correct 249 ms 31660 KB Output is correct
9 Correct 219 ms 34712 KB Output is correct
10 Correct 229 ms 32524 KB Output is correct
11 Correct 0 ms 17016 KB Output is correct
12 Correct 3 ms 17052 KB Output is correct
13 Correct 6 ms 17016 KB Output is correct
14 Correct 0 ms 17016 KB Output is correct
15 Correct 0 ms 17016 KB Output is correct
16 Correct 0 ms 17016 KB Output is correct
17 Correct 0 ms 17020 KB Output is correct
18 Correct 0 ms 17016 KB Output is correct
19 Correct 0 ms 17016 KB Output is correct
20 Correct 0 ms 17048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16884 KB Output is correct
2 Correct 3 ms 16884 KB Output is correct
3 Correct 3 ms 16884 KB Output is correct
4 Correct 3 ms 17016 KB Output is correct
5 Correct 263 ms 29424 KB Output is correct
6 Correct 86 ms 40172 KB Output is correct
7 Correct 189 ms 36488 KB Output is correct
8 Correct 249 ms 31660 KB Output is correct
9 Correct 219 ms 34712 KB Output is correct
10 Correct 229 ms 32524 KB Output is correct
11 Correct 0 ms 16884 KB Output is correct
12 Correct 0 ms 16884 KB Output is correct
13 Correct 0 ms 17048 KB Output is correct
14 Correct 296 ms 42152 KB Output is correct
15 Correct 179 ms 42156 KB Output is correct
16 Correct 239 ms 42156 KB Output is correct
17 Correct 293 ms 42148 KB Output is correct
18 Correct 276 ms 42152 KB Output is correct
19 Correct 223 ms 42152 KB Output is correct
20 Correct 289 ms 42156 KB Output is correct
21 Correct 16 ms 17448 KB Output is correct
22 Correct 243 ms 42152 KB Output is correct
23 Correct 249 ms 42152 KB Output is correct
24 Correct 226 ms 42288 KB Output is correct
25 Correct 249 ms 42152 KB Output is correct
26 Correct 216 ms 42152 KB Output is correct
27 Correct 283 ms 42152 KB Output is correct
28 Correct 259 ms 42152 KB Output is correct
29 Correct 233 ms 42148 KB Output is correct
30 Correct 293 ms 42156 KB Output is correct
31 Correct 419 ms 31164 KB Output is correct
32 Correct 239 ms 42152 KB Output is correct
33 Correct 389 ms 38056 KB Output is correct
34 Correct 366 ms 33600 KB Output is correct
35 Correct 366 ms 37568 KB Output is correct
36 Correct 356 ms 33536 KB Output is correct
37 Correct 439 ms 37256 KB Output is correct
38 Correct 386 ms 30744 KB Output is correct
39 Correct 216 ms 42152 KB Output is correct
40 Correct 486 ms 36448 KB Output is correct
41 Correct 0 ms 17016 KB Output is correct
42 Correct 3 ms 17052 KB Output is correct
43 Correct 6 ms 17016 KB Output is correct
44 Correct 0 ms 17016 KB Output is correct
45 Correct 0 ms 17016 KB Output is correct
46 Correct 0 ms 17016 KB Output is correct
47 Correct 0 ms 17020 KB Output is correct
48 Correct 0 ms 17016 KB Output is correct
49 Correct 0 ms 17016 KB Output is correct
50 Correct 0 ms 17048 KB Output is correct
51 Correct 353 ms 31424 KB Output is correct
52 Correct 243 ms 42152 KB Output is correct
53 Correct 449 ms 36560 KB Output is correct
54 Correct 346 ms 33532 KB Output is correct
55 Correct 426 ms 30996 KB Output is correct
56 Correct 286 ms 42152 KB Output is correct
57 Correct 409 ms 37012 KB Output is correct
58 Correct 396 ms 35016 KB Output is correct
59 Correct 453 ms 30960 KB Output is correct
60 Correct 243 ms 42152 KB Output is correct
61 Correct 349 ms 37104 KB Output is correct
62 Correct 339 ms 33904 KB Output is correct
63 Correct 436 ms 30548 KB Output is correct
64 Correct 246 ms 42148 KB Output is correct
65 Correct 453 ms 37336 KB Output is correct
66 Correct 383 ms 33104 KB Output is correct
67 Correct 416 ms 31508 KB Output is correct
68 Correct 229 ms 42148 KB Output is correct
69 Correct 316 ms 35688 KB Output is correct
70 Correct 283 ms 33304 KB Output is correct