Submission #38716

# Submission time Handle Problem Language Result Execution time Memory
38716 2018-01-06T10:16:59 Z grumpy_gordon Hard route (IZhO17_road) C++14
100 / 100
1749 ms 156532 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);
}

const ll llinf = 1e18 + 100;

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

pair<int, int> get(pair<int, int> x, pair<int, int> y){
    if (x.first < y.first)
        swap(x, y);
    if (x.first == y.first)
        x.second += y.second;
    return x;
}

int n;

vector<int> e[maxn];

vector<pair<int, int> > q[maxn];

vector<pair<int, int> > g[maxn];

ll ans, answer = 1;

void upd(ll x, ll y){
    if (ans > x)
        return;
    if (ans < x)
        ans = x, answer = 0;
    answer += y;
}

pair<int, int> dfs0(int v, int par){
    pair<int, int> ret = {0, 0};
    q[v].resize(e[v].size());
    g[v].resize(e[v].size());
    for (int i = 0; i < e[v].size(); i++)
    if (e[v][i] != par){
        q[v][i] = dfs0(e[v][i], v);
        q[v][i].first++;
        ret = get(ret, q[v][i]);
    }
    if (!ret.first)
        ret.second++;
    return ret;
}

void dfs(int v, int par, pair<int, int> pv){
    int a = 0, b = 0, c = 0, x = 0, y = 0, z = 0, ax = 0, ay = 0, az = 0;
    ll px = 0, py = 0, pz = 0;
    if (pv.first)
        a = pv.first, x = 1, ax = pv.second;
    for (int i = 0; i < e[v].size(); i++)
    if (e[v][i] != par){
        if (a < q[v][i].first){
            c = b;
            b = a;
            z = y;
            y = x;
            az = ay;
            ay = ax;
            pz = py;
            py = px;
            a = q[v][i].first;
            x = 1;
            px = 0;
            ax = q[v][i].second;
        }
        else
        if (a == q[v][i].first){
            x++;
            px += ax * (ll)q[v][i].second;
            ax += q[v][i].second;
        }
        else
        if (b < q[v][i].first){
            c = b;
            z = y;
            az = ay;
            pz = py;
            b = q[v][i].first;
            y = 1;
            py = 0;
            ay = q[v][i].second;
        }
        else
        if (b == q[v][i].first){
            y++;
            py += ay * (ll)q[v][i].second;
            ay += q[v][i].second;
        }
        else
        if (c < q[v][i].first){
            c = q[v][i].first;
            z = 1;
            pz = 0;
            az = q[v][i].second;
        }
        else
        if (c == q[v][i].first){
            z++;
            pz += az * (ll)q[v][i].second;
            az += q[v][i].second;
        }
    }
    if (x + y + z > 2){
        if (x == 1)
            if (y == 1)
                upd(a * (ll)(b + c), ay * (ll)az);
            else
                upd(a * (ll)2 * b, py);
        else
        if (x > 2)
            upd(a * (ll)2 * a, px);
        else
            upd(a * (ll)(a + b), ax * (ll)ay);
    }
    g[v][0] = q[v][0];
    for (int i = 1; i < q[v].size(); i++)
        g[v][i] = get(q[v][i], g[v][i - 1]);
    pair<int, int> w = pv;
    if (!w.first)
        w.second++;
    for (int i = q[v].size() - 1; i >= 0; i--)
    if (e[v][i] != par){
        pair<int, int> o = w;
        if (i)
            o = get(o, g[v][i - 1]);
        o.first++;
        dfs(e[v][i], v, o);
        w = get(w, q[v][i]);
    }
}

int dist, dans;

void getfar(int v, int par, int ds){
    if (ds > dist)
        dist = ds, dans = v;
    for (auto i : e[v])
    if (i != par)
        getfar(i, v, ds + 1);
}

ll X, Y = 1;

bool vis[maxn];

int pt[maxn];

int getf(int v, int par){
    int ret = 0;
    for (auto i : e[v])
    if (i != par)
        ret = max(ret, getf(i, v) + 1);
    return ret;
}

void go(int v, int par, int len){
    pt[len] = v;
    if (len && e[v].size() == 1){
        for (int i = 0; i < n; i++)
            vis[i] = 0;
        for (int i = 0; i <= len; i++)
            vis[pt[i]] = 1;
        int z = 0;
        for (int i = 0; i <= len; i++){
            int u = pt[i];
            for (auto j : e[u])
            if (!vis[j])
                z = max(z, getf(j, u) + 1);
        }
        if (X < z * len)
            X = z * len, Y = 1;
        else
        if (X == z * len)
            Y++;
        return;
    }
    for (auto i : e[v])
    if (i != par)
        go(i, v, len + 1);
}

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("road.in");
    //ofstream cout("road.out");
    //freopen("money.in", "r", stdin);
    //freopen("money.out", "w", stdout);
    #endif // ONPC
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1; i < n; i++){
        int v, u;
        cin >> v >> u;
        v--;
        u--;
        e[v].push_back(u);
        e[u].push_back(v);
    }
    dfs0(0, -1);
    dfs(0, -1, {0, 0});
    //for (int i = 0; i < n; i++)
    //if (e[i].size() == 1)
    //    go(i, -1, 0);
    //Y /= 2;
    //assert(ans == X);
    //assert(Y == answer);
    //cout << X << ' ' << Y << ' ' << answer;
    cout << ans << ' ' << answer;
}

Compilation message

road.cpp: In function 'std::pair<int, int> dfs0(int, int)':
road.cpp:185:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < e[v].size(); i++)
                       ^
road.cpp: In function 'void dfs(int, int, std::pair<int, int>)':
road.cpp:201:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < e[v].size(); i++)
                       ^
road.cpp:267:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 1; i < q[v].size(); i++)
                       ^
# Verdict Execution time Memory Grader output
1 Correct 16 ms 39788 KB Output is correct
2 Correct 13 ms 39788 KB Output is correct
3 Correct 13 ms 39788 KB Output is correct
4 Correct 13 ms 39788 KB Output is correct
5 Correct 9 ms 39788 KB Output is correct
6 Correct 9 ms 39788 KB Output is correct
7 Correct 16 ms 39788 KB Output is correct
8 Correct 9 ms 39788 KB Output is correct
9 Correct 13 ms 39788 KB Output is correct
10 Correct 6 ms 39788 KB Output is correct
11 Correct 6 ms 39788 KB Output is correct
12 Correct 9 ms 39788 KB Output is correct
13 Correct 6 ms 39788 KB Output is correct
14 Correct 9 ms 39788 KB Output is correct
15 Correct 9 ms 39788 KB Output is correct
16 Correct 3 ms 39788 KB Output is correct
17 Correct 16 ms 39788 KB Output is correct
18 Correct 13 ms 39788 KB Output is correct
19 Correct 13 ms 39788 KB Output is correct
20 Correct 6 ms 39788 KB Output is correct
21 Correct 9 ms 39788 KB Output is correct
22 Correct 6 ms 39788 KB Output is correct
23 Correct 19 ms 39788 KB Output is correct
24 Correct 19 ms 39788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 39788 KB Output is correct
2 Correct 13 ms 39788 KB Output is correct
3 Correct 13 ms 39788 KB Output is correct
4 Correct 13 ms 39788 KB Output is correct
5 Correct 9 ms 39788 KB Output is correct
6 Correct 9 ms 39788 KB Output is correct
7 Correct 16 ms 39788 KB Output is correct
8 Correct 9 ms 39788 KB Output is correct
9 Correct 13 ms 39788 KB Output is correct
10 Correct 6 ms 39788 KB Output is correct
11 Correct 6 ms 39788 KB Output is correct
12 Correct 9 ms 39788 KB Output is correct
13 Correct 6 ms 39788 KB Output is correct
14 Correct 9 ms 39788 KB Output is correct
15 Correct 9 ms 39788 KB Output is correct
16 Correct 3 ms 39788 KB Output is correct
17 Correct 16 ms 39788 KB Output is correct
18 Correct 13 ms 39788 KB Output is correct
19 Correct 13 ms 39788 KB Output is correct
20 Correct 6 ms 39788 KB Output is correct
21 Correct 9 ms 39788 KB Output is correct
22 Correct 6 ms 39788 KB Output is correct
23 Correct 19 ms 39788 KB Output is correct
24 Correct 19 ms 39788 KB Output is correct
25 Correct 16 ms 40252 KB Output is correct
26 Correct 16 ms 40368 KB Output is correct
27 Correct 16 ms 40412 KB Output is correct
28 Correct 6 ms 40408 KB Output is correct
29 Correct 16 ms 40324 KB Output is correct
30 Correct 13 ms 40368 KB Output is correct
31 Correct 23 ms 40360 KB Output is correct
32 Correct 13 ms 40288 KB Output is correct
33 Correct 16 ms 40356 KB Output is correct
34 Correct 13 ms 40424 KB Output is correct
35 Correct 19 ms 40416 KB Output is correct
36 Correct 13 ms 40380 KB Output is correct
37 Correct 16 ms 40476 KB Output is correct
38 Correct 13 ms 40760 KB Output is correct
39 Correct 19 ms 40292 KB Output is correct
40 Correct 13 ms 40184 KB Output is correct
41 Correct 19 ms 40184 KB Output is correct
42 Correct 16 ms 40184 KB Output is correct
43 Correct 13 ms 40184 KB Output is correct
44 Correct 16 ms 40184 KB Output is correct
45 Correct 23 ms 40184 KB Output is correct
46 Correct 13 ms 40184 KB Output is correct
47 Correct 13 ms 40316 KB Output is correct
48 Correct 16 ms 40352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 39788 KB Output is correct
2 Correct 13 ms 39788 KB Output is correct
3 Correct 13 ms 39788 KB Output is correct
4 Correct 13 ms 39788 KB Output is correct
5 Correct 9 ms 39788 KB Output is correct
6 Correct 9 ms 39788 KB Output is correct
7 Correct 16 ms 39788 KB Output is correct
8 Correct 9 ms 39788 KB Output is correct
9 Correct 13 ms 39788 KB Output is correct
10 Correct 6 ms 39788 KB Output is correct
11 Correct 6 ms 39788 KB Output is correct
12 Correct 9 ms 39788 KB Output is correct
13 Correct 6 ms 39788 KB Output is correct
14 Correct 9 ms 39788 KB Output is correct
15 Correct 9 ms 39788 KB Output is correct
16 Correct 3 ms 39788 KB Output is correct
17 Correct 16 ms 39788 KB Output is correct
18 Correct 13 ms 39788 KB Output is correct
19 Correct 13 ms 39788 KB Output is correct
20 Correct 6 ms 39788 KB Output is correct
21 Correct 9 ms 39788 KB Output is correct
22 Correct 6 ms 39788 KB Output is correct
23 Correct 19 ms 39788 KB Output is correct
24 Correct 19 ms 39788 KB Output is correct
25 Correct 16 ms 40252 KB Output is correct
26 Correct 16 ms 40368 KB Output is correct
27 Correct 16 ms 40412 KB Output is correct
28 Correct 6 ms 40408 KB Output is correct
29 Correct 16 ms 40324 KB Output is correct
30 Correct 13 ms 40368 KB Output is correct
31 Correct 23 ms 40360 KB Output is correct
32 Correct 13 ms 40288 KB Output is correct
33 Correct 16 ms 40356 KB Output is correct
34 Correct 13 ms 40424 KB Output is correct
35 Correct 19 ms 40416 KB Output is correct
36 Correct 13 ms 40380 KB Output is correct
37 Correct 16 ms 40476 KB Output is correct
38 Correct 13 ms 40760 KB Output is correct
39 Correct 19 ms 40292 KB Output is correct
40 Correct 13 ms 40184 KB Output is correct
41 Correct 19 ms 40184 KB Output is correct
42 Correct 16 ms 40184 KB Output is correct
43 Correct 13 ms 40184 KB Output is correct
44 Correct 16 ms 40184 KB Output is correct
45 Correct 23 ms 40184 KB Output is correct
46 Correct 13 ms 40184 KB Output is correct
47 Correct 13 ms 40316 KB Output is correct
48 Correct 16 ms 40352 KB Output is correct
49 Correct 1356 ms 114164 KB Output is correct
50 Correct 1306 ms 119068 KB Output is correct
51 Correct 1379 ms 123264 KB Output is correct
52 Correct 1213 ms 108368 KB Output is correct
53 Correct 1196 ms 115096 KB Output is correct
54 Correct 1163 ms 119676 KB Output is correct
55 Correct 1186 ms 108356 KB Output is correct
56 Correct 1219 ms 113276 KB Output is correct
57 Correct 1209 ms 124936 KB Output is correct
58 Correct 1243 ms 119384 KB Output is correct
59 Correct 1323 ms 119304 KB Output is correct
60 Correct 1359 ms 116696 KB Output is correct
61 Correct 1643 ms 156532 KB Output is correct
62 Correct 1729 ms 143768 KB Output is correct
63 Correct 1479 ms 106672 KB Output is correct
64 Correct 1629 ms 97868 KB Output is correct
65 Correct 1749 ms 92184 KB Output is correct
66 Correct 1599 ms 89236 KB Output is correct
67 Correct 1669 ms 87608 KB Output is correct
68 Correct 1546 ms 86964 KB Output is correct
69 Correct 1466 ms 86780 KB Output is correct
70 Correct 1423 ms 86516 KB Output is correct
71 Correct 1423 ms 86516 KB Output is correct
72 Correct 1503 ms 86660 KB Output is correct
73 Correct 1593 ms 86820 KB Output is correct
74 Correct 1233 ms 87136 KB Output is correct
75 Correct 1403 ms 87728 KB Output is correct
76 Correct 1359 ms 88744 KB Output is correct
77 Correct 1236 ms 91592 KB Output is correct
78 Correct 1093 ms 96516 KB Output is correct