Submission #129668

# Submission time Handle Problem Language Result Execution time Memory
129668 2019-07-13T01:16:31 Z letrongdat Hard route (IZhO17_road) C++17
52 / 100
2000 ms 169628 KB
#include <bits/stdc++.h>
using namespace std;
#define forn(i, a, b)               for(int i=a; i<=b; ++i)
#define ford(i, a, b)               for(int i=a; i>=b; --i)
#define repn(i, a, b)               for(int i=a; i <b; ++i)
#define repd(i, a, b)               for(int i=(int)a -1; i>=b; --i)
#define llong                       long long 
#define ii                          pair<llong, llong> 
const llong INF = 1e10 + 1;
const int maxN = 5e5 + 10;
int N;
vector<ii> D[maxN];
vector<int> E[maxN];
ii F[maxN], out[maxN];
void maximize(ii &a, ii b) {
    if (a.first < b.first + 1) {
        a.first = b.first + 1;
        a.second = b.second;
    } else if (a.first == b.first + 1) a.second += b.second;
}
void dfs1(int u, int prv = 0) {
    F[u] = {-INF, 0};
    bool leaf = true;
    for(auto v: E[u]) if (v != prv) {
        leaf = false;
        dfs1(v, u);
        maximize(F[u], F[v]);
        D[u].push_back(F[v]);
    }
    sort(D[u].begin(), D[u].end());
    reverse(D[u].begin(), D[u].end());
    D[u].push_back({-INF, 0});
    if (leaf) F[u] = {0, 1};
}
void dfs2(int u, int prv = 0) {
    out[u] = {-INF, 0};
    if (!prv) out[u] = {0, 1}; 
        else {
            maximize(out[u], out[prv]);
            llong sum2 = 0, maxDist2 = -INF;
            for(auto x: D[prv]) if (x.first + 1 != F[prv].first) {
                maxDist2 = x.first + 1;
                for(auto y: D[prv]) if (y.first == x.first) sum2 += y.second; 
                break;
            }
            if (F[u].first + 1 == F[prv].first) {
                if (F[u].second != F[prv].second) maximize(out[u], {F[prv].first, F[prv].second -F[u].second}); 
                    else maximize(out[u], {maxDist2, sum2});
            } else maximize(out[u], F[prv]);
        }
    for(auto v: E[u]) if (v != prv) dfs2(v, u);
}
int main() {
    ios::sync_with_stdio(0);
    cin >> N;
    repn(i, 1, N) {
        int u, v;
        cin >> u >> v;
        E[u].push_back(v);
        E[v].push_back(u);
    }
    dfs1(1);
    dfs2(1);
    llong result = 0, ways = 0;
    forn(u, 1, N) {
        for(auto &x: D[u]) x.first ++;
        D[u].push_back(out[u]);
        sort(D[u].begin(), D[u].end());
        reverse(D[u].begin(), D[u].end());
        if (D[u].size() <= 3 || D[u][2].first <= 0) continue;
        while (D[u].size() && D[u].back().first != D[u][2].first) D[u].pop_back();
        long long paths, a, ab, abc;
        paths = a = ab = abc = 0;
        if (D[u][0].first == D[u][2].first) {
            for(auto v: D[u]) {
                ab  += v.second * a;
                a   += v.second; 
            }
            paths = ab;
        } else {
            if (D[u][1].first == D[u][2].first) {
                for(auto v: D[u]) if (v.first != D[u][0].first) {
                    ab += v.second  * a;
                    a  += v.second;
                }
                paths = ab;
            } else {
                for(auto v: D[u]) if (v.first == D[u][2].first)
                    a += v.second;
                if (D[u][0].first == D[u][1].first) 
                    paths = (D[u][0].second + D[u][1].second) * a;
                else paths = D[u][1].second * a;
            }
        }
        llong Dist = D[u][0].first * (D[u][1].first + D[u][2].first);
        if (result < Dist) {
            result = Dist;
            ways = paths;
        } else if (result == Dist) ways += paths;
    }
    if (result == 0) ways = 1;
    cout << result << ' ' << ways << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB Output is correct
2 Correct 23 ms 23800 KB Output is correct
3 Correct 24 ms 23800 KB Output is correct
4 Correct 23 ms 23804 KB Output is correct
5 Correct 23 ms 23788 KB Output is correct
6 Correct 23 ms 23772 KB Output is correct
7 Correct 23 ms 23800 KB Output is correct
8 Correct 23 ms 23844 KB Output is correct
9 Correct 24 ms 23800 KB Output is correct
10 Correct 24 ms 23932 KB Output is correct
11 Correct 23 ms 23800 KB Output is correct
12 Correct 24 ms 23928 KB Output is correct
13 Correct 24 ms 23928 KB Output is correct
14 Correct 23 ms 23800 KB Output is correct
15 Correct 24 ms 23928 KB Output is correct
16 Correct 23 ms 23928 KB Output is correct
17 Correct 24 ms 23928 KB Output is correct
18 Correct 24 ms 23928 KB Output is correct
19 Correct 24 ms 23928 KB Output is correct
20 Correct 24 ms 23928 KB Output is correct
21 Correct 29 ms 23928 KB Output is correct
22 Correct 29 ms 23804 KB Output is correct
23 Correct 30 ms 23800 KB Output is correct
24 Correct 23 ms 23772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB Output is correct
2 Correct 23 ms 23800 KB Output is correct
3 Correct 24 ms 23800 KB Output is correct
4 Correct 23 ms 23804 KB Output is correct
5 Correct 23 ms 23788 KB Output is correct
6 Correct 23 ms 23772 KB Output is correct
7 Correct 23 ms 23800 KB Output is correct
8 Correct 23 ms 23844 KB Output is correct
9 Correct 24 ms 23800 KB Output is correct
10 Correct 24 ms 23932 KB Output is correct
11 Correct 23 ms 23800 KB Output is correct
12 Correct 24 ms 23928 KB Output is correct
13 Correct 24 ms 23928 KB Output is correct
14 Correct 23 ms 23800 KB Output is correct
15 Correct 24 ms 23928 KB Output is correct
16 Correct 23 ms 23928 KB Output is correct
17 Correct 24 ms 23928 KB Output is correct
18 Correct 24 ms 23928 KB Output is correct
19 Correct 24 ms 23928 KB Output is correct
20 Correct 24 ms 23928 KB Output is correct
21 Correct 29 ms 23928 KB Output is correct
22 Correct 29 ms 23804 KB Output is correct
23 Correct 30 ms 23800 KB Output is correct
24 Correct 23 ms 23772 KB Output is correct
25 Correct 34 ms 24828 KB Output is correct
26 Correct 28 ms 24952 KB Output is correct
27 Correct 28 ms 24952 KB Output is correct
28 Correct 29 ms 24952 KB Output is correct
29 Correct 28 ms 24752 KB Output is correct
30 Correct 28 ms 24824 KB Output is correct
31 Correct 29 ms 24824 KB Output is correct
32 Correct 28 ms 24764 KB Output is correct
33 Correct 28 ms 24952 KB Output is correct
34 Correct 28 ms 24952 KB Output is correct
35 Correct 28 ms 24952 KB Output is correct
36 Correct 28 ms 24952 KB Output is correct
37 Correct 29 ms 24952 KB Output is correct
38 Correct 29 ms 25256 KB Output is correct
39 Correct 32 ms 24952 KB Output is correct
40 Correct 33 ms 24696 KB Output is correct
41 Correct 29 ms 24696 KB Output is correct
42 Correct 27 ms 24696 KB Output is correct
43 Correct 29 ms 24696 KB Output is correct
44 Correct 28 ms 24696 KB Output is correct
45 Correct 29 ms 24584 KB Output is correct
46 Correct 31 ms 24568 KB Output is correct
47 Correct 38 ms 24568 KB Output is correct
48 Correct 72 ms 24668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB Output is correct
2 Correct 23 ms 23800 KB Output is correct
3 Correct 24 ms 23800 KB Output is correct
4 Correct 23 ms 23804 KB Output is correct
5 Correct 23 ms 23788 KB Output is correct
6 Correct 23 ms 23772 KB Output is correct
7 Correct 23 ms 23800 KB Output is correct
8 Correct 23 ms 23844 KB Output is correct
9 Correct 24 ms 23800 KB Output is correct
10 Correct 24 ms 23932 KB Output is correct
11 Correct 23 ms 23800 KB Output is correct
12 Correct 24 ms 23928 KB Output is correct
13 Correct 24 ms 23928 KB Output is correct
14 Correct 23 ms 23800 KB Output is correct
15 Correct 24 ms 23928 KB Output is correct
16 Correct 23 ms 23928 KB Output is correct
17 Correct 24 ms 23928 KB Output is correct
18 Correct 24 ms 23928 KB Output is correct
19 Correct 24 ms 23928 KB Output is correct
20 Correct 24 ms 23928 KB Output is correct
21 Correct 29 ms 23928 KB Output is correct
22 Correct 29 ms 23804 KB Output is correct
23 Correct 30 ms 23800 KB Output is correct
24 Correct 23 ms 23772 KB Output is correct
25 Correct 34 ms 24828 KB Output is correct
26 Correct 28 ms 24952 KB Output is correct
27 Correct 28 ms 24952 KB Output is correct
28 Correct 29 ms 24952 KB Output is correct
29 Correct 28 ms 24752 KB Output is correct
30 Correct 28 ms 24824 KB Output is correct
31 Correct 29 ms 24824 KB Output is correct
32 Correct 28 ms 24764 KB Output is correct
33 Correct 28 ms 24952 KB Output is correct
34 Correct 28 ms 24952 KB Output is correct
35 Correct 28 ms 24952 KB Output is correct
36 Correct 28 ms 24952 KB Output is correct
37 Correct 29 ms 24952 KB Output is correct
38 Correct 29 ms 25256 KB Output is correct
39 Correct 32 ms 24952 KB Output is correct
40 Correct 33 ms 24696 KB Output is correct
41 Correct 29 ms 24696 KB Output is correct
42 Correct 27 ms 24696 KB Output is correct
43 Correct 29 ms 24696 KB Output is correct
44 Correct 28 ms 24696 KB Output is correct
45 Correct 29 ms 24584 KB Output is correct
46 Correct 31 ms 24568 KB Output is correct
47 Correct 38 ms 24568 KB Output is correct
48 Correct 72 ms 24668 KB Output is correct
49 Correct 899 ms 128248 KB Output is correct
50 Correct 1006 ms 132728 KB Output is correct
51 Correct 919 ms 136312 KB Output is correct
52 Correct 885 ms 123000 KB Output is correct
53 Correct 914 ms 123384 KB Output is correct
54 Correct 855 ms 130680 KB Output is correct
55 Correct 850 ms 119388 KB Output is correct
56 Correct 848 ms 124328 KB Output is correct
57 Correct 876 ms 141560 KB Output is correct
58 Correct 889 ms 136748 KB Output is correct
59 Correct 887 ms 136440 KB Output is correct
60 Correct 895 ms 134176 KB Output is correct
61 Correct 1119 ms 169628 KB Output is correct
62 Correct 1126 ms 158392 KB Output is correct
63 Correct 1085 ms 125304 KB Output is correct
64 Correct 1224 ms 117384 KB Output is correct
65 Correct 1100 ms 112432 KB Output is correct
66 Correct 1116 ms 109816 KB Output is correct
67 Correct 1046 ms 108288 KB Output is correct
68 Correct 1035 ms 107896 KB Output is correct
69 Correct 1109 ms 107392 KB Output is correct
70 Correct 1049 ms 107160 KB Output is correct
71 Correct 1077 ms 106996 KB Output is correct
72 Correct 1172 ms 107272 KB Output is correct
73 Correct 1311 ms 107028 KB Output is correct
74 Correct 1806 ms 106776 KB Output is correct
75 Execution timed out 2061 ms 86024 KB Time limit exceeded
76 Halted 0 ms 0 KB -