Submission #25423

# Submission time Handle Problem Language Result Execution time Memory
25423 2017-06-22T06:35:32 Z 시제연(#1067) Election Campaign (JOI15_election_campaign) C++
100 / 100
479 ms 39156 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 0 ms 16884 KB Output is correct
2 Correct 3 ms 16884 KB Output is correct
3 Correct 0 ms 16884 KB Output is correct
4 Correct 0 ms 17016 KB Output is correct
5 Correct 249 ms 29424 KB Output is correct
6 Correct 86 ms 37048 KB Output is correct
7 Correct 186 ms 34448 KB Output is correct
8 Correct 233 ms 31660 KB Output is correct
9 Correct 229 ms 33284 KB Output is correct
10 Correct 239 ms 32524 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 0 ms 17016 KB Output is correct
4 Correct 263 ms 39028 KB Output is correct
5 Correct 233 ms 39024 KB Output is correct
6 Correct 193 ms 39024 KB Output is correct
7 Correct 289 ms 39028 KB Output is correct
8 Correct 256 ms 39024 KB Output is correct
9 Correct 263 ms 39028 KB Output is correct
10 Correct 266 ms 39028 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 0 ms 17016 KB Output is correct
4 Correct 263 ms 39028 KB Output is correct
5 Correct 233 ms 39024 KB Output is correct
6 Correct 193 ms 39024 KB Output is correct
7 Correct 289 ms 39028 KB Output is correct
8 Correct 256 ms 39024 KB Output is correct
9 Correct 263 ms 39028 KB Output is correct
10 Correct 266 ms 39028 KB Output is correct
11 Correct 29 ms 17416 KB Output is correct
12 Correct 273 ms 39028 KB Output is correct
13 Correct 296 ms 39032 KB Output is correct
14 Correct 229 ms 39156 KB Output is correct
15 Correct 319 ms 39028 KB Output is correct
16 Correct 243 ms 39024 KB Output is correct
17 Correct 293 ms 39028 KB Output is correct
18 Correct 303 ms 39028 KB Output is correct
19 Correct 249 ms 39028 KB Output is correct
20 Correct 289 ms 39024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 449 ms 31164 KB Output is correct
2 Correct 189 ms 39024 KB Output is correct
3 Correct 479 ms 36108 KB Output is correct
4 Correct 386 ms 33600 KB Output is correct
5 Correct 293 ms 35840 KB Output is correct
6 Correct 379 ms 33536 KB Output is correct
7 Correct 439 ms 35564 KB Output is correct
8 Correct 409 ms 30744 KB Output is correct
9 Correct 209 ms 39028 KB Output is correct
10 Correct 413 ms 35104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 16884 KB Output is correct
2 Correct 3 ms 16884 KB Output is correct
3 Correct 0 ms 16884 KB Output is correct
4 Correct 0 ms 17016 KB Output is correct
5 Correct 249 ms 29424 KB Output is correct
6 Correct 86 ms 37048 KB Output is correct
7 Correct 186 ms 34448 KB Output is correct
8 Correct 233 ms 31660 KB Output is correct
9 Correct 229 ms 33284 KB Output is correct
10 Correct 239 ms 32524 KB Output is correct
11 Correct 0 ms 17016 KB Output is correct
12 Correct 0 ms 17020 KB Output is correct
13 Correct 0 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 3 ms 17020 KB Output is correct
18 Correct 0 ms 17016 KB Output is correct
19 Correct 3 ms 17016 KB Output is correct
20 Correct 3 ms 17020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 16884 KB Output is correct
2 Correct 3 ms 16884 KB Output is correct
3 Correct 0 ms 16884 KB Output is correct
4 Correct 0 ms 17016 KB Output is correct
5 Correct 249 ms 29424 KB Output is correct
6 Correct 86 ms 37048 KB Output is correct
7 Correct 186 ms 34448 KB Output is correct
8 Correct 233 ms 31660 KB Output is correct
9 Correct 229 ms 33284 KB Output is correct
10 Correct 239 ms 32524 KB Output is correct
11 Correct 3 ms 16884 KB Output is correct
12 Correct 3 ms 16884 KB Output is correct
13 Correct 0 ms 17016 KB Output is correct
14 Correct 263 ms 39028 KB Output is correct
15 Correct 233 ms 39024 KB Output is correct
16 Correct 193 ms 39024 KB Output is correct
17 Correct 289 ms 39028 KB Output is correct
18 Correct 256 ms 39024 KB Output is correct
19 Correct 263 ms 39028 KB Output is correct
20 Correct 266 ms 39028 KB Output is correct
21 Correct 29 ms 17416 KB Output is correct
22 Correct 273 ms 39028 KB Output is correct
23 Correct 296 ms 39032 KB Output is correct
24 Correct 229 ms 39156 KB Output is correct
25 Correct 319 ms 39028 KB Output is correct
26 Correct 243 ms 39024 KB Output is correct
27 Correct 293 ms 39028 KB Output is correct
28 Correct 303 ms 39028 KB Output is correct
29 Correct 249 ms 39028 KB Output is correct
30 Correct 289 ms 39024 KB Output is correct
31 Correct 449 ms 31164 KB Output is correct
32 Correct 189 ms 39024 KB Output is correct
33 Correct 479 ms 36108 KB Output is correct
34 Correct 386 ms 33600 KB Output is correct
35 Correct 293 ms 35840 KB Output is correct
36 Correct 379 ms 33536 KB Output is correct
37 Correct 439 ms 35564 KB Output is correct
38 Correct 409 ms 30744 KB Output is correct
39 Correct 209 ms 39028 KB Output is correct
40 Correct 413 ms 35104 KB Output is correct
41 Correct 0 ms 17016 KB Output is correct
42 Correct 0 ms 17020 KB Output is correct
43 Correct 0 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 3 ms 17020 KB Output is correct
48 Correct 0 ms 17016 KB Output is correct
49 Correct 3 ms 17016 KB Output is correct
50 Correct 3 ms 17020 KB Output is correct
51 Correct 356 ms 31424 KB Output is correct
52 Correct 289 ms 39024 KB Output is correct
53 Correct 399 ms 35164 KB Output is correct
54 Correct 366 ms 33532 KB Output is correct
55 Correct 419 ms 30996 KB Output is correct
56 Correct 286 ms 39028 KB Output is correct
57 Correct 383 ms 35468 KB Output is correct
58 Correct 386 ms 35016 KB Output is correct
59 Correct 426 ms 30960 KB Output is correct
60 Correct 289 ms 39028 KB Output is correct
61 Correct 399 ms 35516 KB Output is correct
62 Correct 413 ms 33904 KB Output is correct
63 Correct 359 ms 30548 KB Output is correct
64 Correct 269 ms 39028 KB Output is correct
65 Correct 479 ms 35724 KB Output is correct
66 Correct 373 ms 33104 KB Output is correct
67 Correct 429 ms 31508 KB Output is correct
68 Correct 263 ms 39032 KB Output is correct
69 Correct 393 ms 34584 KB Output is correct
70 Correct 366 ms 33304 KB Output is correct