답안 #39864

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
39864 2018-01-22T10:17:21 Z grumpy_gordon Election Campaign (JOI15_election_campaign) C++14
100 / 100
486 ms 42484 KB
/*
                     .:*+=%@@@@@@=-.
                 .:=@#@@@#@@#######%==*.
              .-=####@######%*-.....:%##%.
            .*@###########%+:--........-%@-
          .*@##############@+--.........-:%-
        .+##################@==%%%%=+*:----+.
      .-@####################%++%@@@@@=+**%@@*
      .%###################@%%@@@###@%+:--%@@%.
     -@###################@%%%%*::*%++:-----=@+.
    -#####################@%=++++++*:-------.-=:
   .+####################@%++*::-:::--::*:::***=:
   .@#####################%=++*::::-:::++*=##@@#@-
  ..#####################@%%=++**:::::**+%@#@%%##-..
   .%####################@@%=+++*+****::*=@######@.
  .=######################@%%==+==++**+=@%@##@###+:...
  -#######################@@@%%%===++=@@@%=++===*::--...
  -########################@@@@@@@%==%%=++==@@:::::*:--.
..:#########################@@@@@@%%======++++::-..:-.--...
%#############################@###@%%@@%==%=%*----.--.::---.
#############################################*-:*:-:---*---- .
#############################################*--*--:---*---:-.
#############################################+--::--::-*::-::.
###########################################+:*-.---.---.:---*-..
###########################################**:-----------------.
##########################################@::**:--::::::--:::::-
###########################################:--:*:::::::::**::*+*
###########################################=:::***::::::**:::*+*
############################@@@@@@#########@+****::::********+++
############################@%%%%%@@@@@@@###%+***::::::::***+==+
############################@%%%%%%%%%%%@####=+:::-::::-::*+=%%+
#############################@%%%%%%%%%%@#####=::--------:*=%@%+
%###########################@%%%%==%%%%%%@##@#=:------..-:+%@@%=
----------------------------------------------
--------------------------------------------
----------------------------------------------
--------------------------------------------
----------------------------------------------

         o###########oo
      o##"          ""##o
    o#"                "##
  o#"                    "#o
 #"  ##              ##   "##
#"                          ##
#  ###################       #
#                            #
#                            #
#                            #
#                            #
#                            #
#                            #
#o                           #
"#o                         ##
 "#o                       ##
  "#o                    o#"
   "#o                  ##
     "#o              o#"
       "#ooo      ooo#######oo
        ###############   "######o
     o###""        "###o      # ###
   o###o     oooo    ###    oo####"
 o###**#     #**#   ############"
 ""##""""""""""###########    #
    # oooooooo#"#**     ##    #
    # #       # # **    ##    #
    #o#       #o#  *****###ooo#
                        ##
                        ##   o###o
                        ## o##***##
               o########## #***#**##o
             o##"   ""###  #***##***#
 o#######o  ###   oo####   ##**####*#
o##"  ""#############""     ##****###
##"         ##              ##*##*###
##          ###              ##### ##
##           ###              # ##  #
##            ##                 #
##             ##
##             ###
##              ###oo
###              ""###
 ###
  ###
*/
#include <bits/stdc++.h>

//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
//#pragma GCC target("avx,tune=native")
//float __attribute__((aligned(32)))

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

typedef long double ld;

int mysqrt(ll x){
    int l = 0, r = 1e9 + 1;
    while (r - l > 1){
        int m = (l + r) / 2;
        if (m * (ll)m <= x)
            l = m;
        else
            r = m;
    }
    return l;
}

mt19937 rnd(1337);

ll AR = 19, BR = 13, CR = 23, XR = 228, YR = 322, MODR = 1e9 + 993;

ll myrand(){
    ll ZR = (XR * AR + YR * BR + CR) % MODR;
    XR = YR;
    YR = ZR;
    return ZR;
}

const int Mod = 1e9 + 7;

int bpow(int x, int y){
    if (y == 0)
        return 1;
    if (y == 1)
        return x;
    int ret = bpow(x, y >> 1);
    ret = (ret * (ll)ret) % Mod;
    if (y & 1)
        ret = (ret * (ll)x) % Mod;
    return ret;
}

int bdiv(int x, int y){
    return (x * (ll)bpow(y, Mod - 2)) % Mod;
}

void setmin(int &x, int y){
    x = min(x, y);
}

void setmax(int &x, int y){
    x = max(x, y);
}

int gcd(int a, int b){
    return a ? gcd(b % a, a) : b;
}

const ll llinf = 2e18 + 100;

const int maxn = 1e5 + 100, maxw = (1 << 17) + 100, inf = 2e9 + 100, sq = 300, mod = 1e9 + 7, LG = 17;

int n;

vector<int> e[maxn];

int up[LG][maxn], q[LG][maxn];

int pep[LG][maxn];

int mas[maxn];

int dp[maxn];

int h[maxn];

int climb(int v, int hei){
    for (int i = LG - 1; i >= 0; i--)
    if (hei >= (1 << i))
        hei -= (1 << i), v = up[i][v];
    return v;
}

int getlca(int v, int u){
    if (h[v] < h[u])
        swap(v, u);
    v = climb(v, h[v] - h[u]);
    if (v == u)
        return v;
    for (int i = LG - 1; i >= 0; i--)
    if (up[i][v] != up[i][u])
        v = up[i][v], u = up[i][u];
    return up[0][v];
}

void predfs(int v, int par){
    for (auto i : e[v])
    if (i != par)
        h[i] = h[v] + 1, up[0][i] = v, predfs(i, v);
}

int get(int v, int it){
    if (q[it][v] == -1){
        if (it == 0)
            q[it][v] = mas[v];
        else
            q[it][v] = get(v, it - 1) + get(up[it - 1][v], it - 1) - dp[pep[it - 1][v]];
    }
    return q[it][v];
}

int getpath(int v, int hei){
    if (!hei)
        return 0;
    int r = get(v, 0);
    hei--;
    for (int i = LG - 1; i >= 0; i--)
    if (hei >= (1 << i))
        r += get(up[0][v], i) - dp[v], v = up[i][v], hei -= (1 << i);
    return r;
}

void precalc(){
    for (int i = 0; i < LG; i++)
        for (int j = 0; j < n; j++)
            q[i][j] = -1;
    predfs(0, -1);
    for (int i = 1; i < LG; i++)
        for (int j = 0; j < n; j++)
            up[i][j] = up[i - 1][up[i - 1][j]];
    for (int j = 0; j < n; j++)
        pep[0][j] = j;
    for (int i = 1; i < LG; i++)
        for (int j = 0; j < n; j++)
            pep[i][j] = pep[i - 1][up[i - 1][j]];
}

pair<pair<int, int>, int> que[maxn];

vector<int> qs[maxn];

void dfs(int v, int par){
    for (auto i : e[v])
    if (i != par){
        dfs(i, v);
        mas[v] += dp[i];
    }
    dp[v] = mas[v];
    for (auto i : qs[v]){
        int a = que[i].first.first, b = que[i].first.second, w = que[i].second;
        int val = getpath(a, h[a] - h[v]) + getpath(b, h[b] - h[v]) + mas[v] + w;
        if (a != v)
            val -= dp[climb(a, h[a] - h[v] - 1)];
        if (b != v)
            val -= dp[climb(b, h[b] - h[v] - 1)];
        dp[v] = max(dp[v], val);
    }
}

int main()
{
    #ifdef ONPC
    //ifstream cin("a.in");
    //ofstream cout("a.out");
    freopen("a.in", "r", stdin);
    freopen("a.out", "w", stdout);
    #else
    //ifstream cin("sprinklers.in");
    //ofstream cout("sprinklers.out");
    //freopen("road.in", "r", stdin);
    //freopen("road.out", "w", stdout);
    #endif // ONPC
    //ios::sync_with_stdio(0);
    //cin.tie(0);
    //cout.tie(0);
    scanf("%d", &n);
    for (int i = 1; i < n; i++){
        int v, u;
        scanf("%d%d", &v, &u);
        v--;
        u--;
        e[v].push_back(u);
        e[u].push_back(v);
    }
    precalc();
    int zap;
    scanf("%d", &zap);
    for (int i = 0; i < zap; i++){
        int v, u, w;
        scanf("%d%d%d", &v, &u, &w);
        v--;
        u--;
        qs[getlca(v, u)].push_back(i);
        que[i] = make_pair(make_pair(v, u), w);
    }
    dfs(0, -1);
    printf("%d\n", dp[0]);
}

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:272:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
                    ^
election_campaign.cpp:275:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &v, &u);
                              ^
election_campaign.cpp:283:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &zap);
                      ^
election_campaign.cpp:286:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d", &v, &u, &w);
                                    ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 29008 KB Output is correct
2 Correct 0 ms 29008 KB Output is correct
3 Correct 0 ms 29008 KB Output is correct
4 Correct 0 ms 29008 KB Output is correct
5 Correct 88 ms 32308 KB Output is correct
6 Correct 49 ms 41420 KB Output is correct
7 Correct 98 ms 38184 KB Output is correct
8 Correct 65 ms 32828 KB Output is correct
9 Correct 110 ms 36328 KB Output is correct
10 Correct 58 ms 32768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 29008 KB Output is correct
2 Correct 0 ms 29008 KB Output is correct
3 Correct 6 ms 29008 KB Output is correct
4 Correct 164 ms 42484 KB Output is correct
5 Correct 192 ms 42476 KB Output is correct
6 Correct 138 ms 42480 KB Output is correct
7 Correct 201 ms 42484 KB Output is correct
8 Correct 169 ms 42480 KB Output is correct
9 Correct 143 ms 42480 KB Output is correct
10 Correct 178 ms 42480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 29008 KB Output is correct
2 Correct 0 ms 29008 KB Output is correct
3 Correct 6 ms 29008 KB Output is correct
4 Correct 164 ms 42484 KB Output is correct
5 Correct 192 ms 42476 KB Output is correct
6 Correct 138 ms 42480 KB Output is correct
7 Correct 201 ms 42484 KB Output is correct
8 Correct 169 ms 42480 KB Output is correct
9 Correct 143 ms 42480 KB Output is correct
10 Correct 178 ms 42480 KB Output is correct
11 Correct 25 ms 29272 KB Output is correct
12 Correct 202 ms 42484 KB Output is correct
13 Correct 176 ms 42476 KB Output is correct
14 Correct 163 ms 42476 KB Output is correct
15 Correct 218 ms 42484 KB Output is correct
16 Correct 158 ms 42484 KB Output is correct
17 Correct 167 ms 42484 KB Output is correct
18 Correct 183 ms 42484 KB Output is correct
19 Correct 150 ms 42484 KB Output is correct
20 Correct 184 ms 42480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 208 ms 32856 KB Output is correct
2 Correct 142 ms 42484 KB Output is correct
3 Correct 486 ms 38860 KB Output is correct
4 Correct 139 ms 33484 KB Output is correct
5 Correct 368 ms 38416 KB Output is correct
6 Correct 145 ms 33608 KB Output is correct
7 Correct 402 ms 38056 KB Output is correct
8 Correct 200 ms 33256 KB Output is correct
9 Correct 149 ms 42480 KB Output is correct
10 Correct 446 ms 37008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 29008 KB Output is correct
2 Correct 0 ms 29008 KB Output is correct
3 Correct 0 ms 29008 KB Output is correct
4 Correct 0 ms 29008 KB Output is correct
5 Correct 88 ms 32308 KB Output is correct
6 Correct 49 ms 41420 KB Output is correct
7 Correct 98 ms 38184 KB Output is correct
8 Correct 65 ms 32828 KB Output is correct
9 Correct 110 ms 36328 KB Output is correct
10 Correct 58 ms 32768 KB Output is correct
11 Correct 1 ms 29008 KB Output is correct
12 Correct 2 ms 29008 KB Output is correct
13 Correct 4 ms 29008 KB Output is correct
14 Correct 1 ms 29008 KB Output is correct
15 Correct 1 ms 29008 KB Output is correct
16 Correct 2 ms 29008 KB Output is correct
17 Correct 0 ms 29008 KB Output is correct
18 Correct 0 ms 29008 KB Output is correct
19 Correct 0 ms 29008 KB Output is correct
20 Correct 2 ms 29008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 29008 KB Output is correct
2 Correct 0 ms 29008 KB Output is correct
3 Correct 0 ms 29008 KB Output is correct
4 Correct 0 ms 29008 KB Output is correct
5 Correct 88 ms 32308 KB Output is correct
6 Correct 49 ms 41420 KB Output is correct
7 Correct 98 ms 38184 KB Output is correct
8 Correct 65 ms 32828 KB Output is correct
9 Correct 110 ms 36328 KB Output is correct
10 Correct 58 ms 32768 KB Output is correct
11 Correct 0 ms 29008 KB Output is correct
12 Correct 0 ms 29008 KB Output is correct
13 Correct 6 ms 29008 KB Output is correct
14 Correct 164 ms 42484 KB Output is correct
15 Correct 192 ms 42476 KB Output is correct
16 Correct 138 ms 42480 KB Output is correct
17 Correct 201 ms 42484 KB Output is correct
18 Correct 169 ms 42480 KB Output is correct
19 Correct 143 ms 42480 KB Output is correct
20 Correct 178 ms 42480 KB Output is correct
21 Correct 25 ms 29272 KB Output is correct
22 Correct 202 ms 42484 KB Output is correct
23 Correct 176 ms 42476 KB Output is correct
24 Correct 163 ms 42476 KB Output is correct
25 Correct 218 ms 42484 KB Output is correct
26 Correct 158 ms 42484 KB Output is correct
27 Correct 167 ms 42484 KB Output is correct
28 Correct 183 ms 42484 KB Output is correct
29 Correct 150 ms 42484 KB Output is correct
30 Correct 184 ms 42480 KB Output is correct
31 Correct 208 ms 32856 KB Output is correct
32 Correct 142 ms 42484 KB Output is correct
33 Correct 486 ms 38860 KB Output is correct
34 Correct 139 ms 33484 KB Output is correct
35 Correct 368 ms 38416 KB Output is correct
36 Correct 145 ms 33608 KB Output is correct
37 Correct 402 ms 38056 KB Output is correct
38 Correct 200 ms 33256 KB Output is correct
39 Correct 149 ms 42480 KB Output is correct
40 Correct 446 ms 37008 KB Output is correct
41 Correct 1 ms 29008 KB Output is correct
42 Correct 2 ms 29008 KB Output is correct
43 Correct 4 ms 29008 KB Output is correct
44 Correct 1 ms 29008 KB Output is correct
45 Correct 1 ms 29008 KB Output is correct
46 Correct 2 ms 29008 KB Output is correct
47 Correct 0 ms 29008 KB Output is correct
48 Correct 0 ms 29008 KB Output is correct
49 Correct 0 ms 29008 KB Output is correct
50 Correct 2 ms 29008 KB Output is correct
51 Correct 219 ms 33268 KB Output is correct
52 Correct 197 ms 42484 KB Output is correct
53 Correct 431 ms 37148 KB Output is correct
54 Correct 138 ms 33472 KB Output is correct
55 Correct 201 ms 32996 KB Output is correct
56 Correct 183 ms 42484 KB Output is correct
57 Correct 364 ms 37756 KB Output is correct
58 Correct 178 ms 33372 KB Output is correct
59 Correct 206 ms 33268 KB Output is correct
60 Correct 192 ms 42480 KB Output is correct
61 Correct 382 ms 37996 KB Output is correct
62 Correct 136 ms 33568 KB Output is correct
63 Correct 191 ms 33004 KB Output is correct
64 Correct 196 ms 42484 KB Output is correct
65 Correct 429 ms 37868 KB Output is correct
66 Correct 132 ms 33452 KB Output is correct
67 Correct 207 ms 33116 KB Output is correct
68 Correct 187 ms 42484 KB Output is correct
69 Correct 320 ms 36480 KB Output is correct
70 Correct 115 ms 33432 KB Output is correct