Submission #91630

# Submission time Handle Problem Language Result Execution time Memory
91630 2018-12-28T18:01:32 Z popovicirobert Hard route (IZhO17_road) C++14
100 / 100
1527 ms 159608 KB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define ld long double
// 217
// 44

using namespace std;

const int MAXN = (int) 5e5;

vector <int> g[MAXN + 1];
pair <int, int> down[MAXN + 1], up[MAXN + 1];

pair <ll, ll> ans = {0, 1};

inline void update(pair <int, int> &a, pair <int, int> b) {
    if(b.second == 0) {
        return ;
    }
    if(a.first < b.first) {
        a = b;
    }
    else if(a.first == b.first) {
        a.second += b.second;
    }
}

inline void update(pair <ll, ll> &a, pair <ll, ll> b) {
    if(b.second == 0) {
        return ;
    }
    if(a.first < b.first) {
        a = b;
    }
    else if(a.first == b.first) {
        a.second += b.second;
    }
}

void dfs_down(int nod, int par) {
    down[nod] = {0, 1};
    for(auto it : g[nod]) {
        if(it != par) {
            dfs_down(it, nod);
            update(down[nod], {down[it].first + 1, down[it].second});
        }
    }
}


void dfs_up(int nod, int par) {
    int sz = g[nod].size();
    vector < pair <int, int> > pref(sz), suff(sz);
    for(int i = 0; i < sz; i++) {
        int it = g[nod][i];
        if(i > 0) {
            pref[i] = pref[i - 1];
        }
        if(it == par) {
            continue;
        }
        update(pref[i], {down[it].first + 2, down[it].second});
    }
    for(int i = sz - 1; i >= 0; i--) {
        int it = g[nod][i];
        if(i < sz - 1) {
            suff[i] = suff[i + 1];
        }
        if(it == par) {
            continue;
        }
        update(suff[i], {down[it].first + 2, down[it].second});
    }
    for(int i = 0; i < sz; i++) {
        int it = g[nod][i];
        if(it != par) {
            update(up[it], {up[nod].first + 1, up[nod].second});
            if(i > 0) {
                update(up[it], pref[i - 1]);
            }
            if(i < sz - 1) {
                update(up[it], suff[i + 1]);
            }
            dfs_up(it, nod);
        }
    }
}

inline bool cmp(pair <int, int> a, pair <int, int> b) {
    return a.first > b.first;
}

struct Data {
    int val, cnt, nr;
};

void solve(int nod, int par) {
    vector < pair <int, int> > arr;
    for(auto it : g[nod]) {
        if(it != par) {
            arr.push_back({down[it].first + 1, down[it].second});
        }
    }
    if(up[nod].first > 0) {
        arr.push_back(up[nod]);
    }
    sort(arr.begin(), arr.end(), cmp);
    while(arr.size() >= 3 && arr.back().first < arr[2].first) {
        arr.pop_back();
    }
    if(arr.size() >= 3) {
        vector < Data > aux;
        int sz = arr.size();
        int i = 0;
        while(i < sz) {
            int j = i;
            int cnt = 0;
            while(j < sz && arr[i].first == arr[j].first) {
                cnt += arr[j].second;
                j++;
            }
            aux.push_back({arr[i].first, cnt, j - i});
            i = j;
        }
        if(aux.size() == 1) {
            vector <ll> dp(3);
            for(auto it : arr) {
                vector <ll> new_dp(3);
                new_dp[1] = dp[1] + it.second;
                new_dp[2] = dp[2] + dp[1] * it.second;
                swap(dp, new_dp);
            }
            update(ans, {2LL * aux[0].val * aux[0].val, dp[2]});
        }
        else if(aux.size() == 2) {
            vector <ll> dp(3);
            if(aux[0].nr > 1) {
                update(ans, {1LL * aux[0].val * (aux[0].val + aux[1].val), 1LL * aux[0].cnt * aux[1].cnt});
            }
            else {
                for(int i = 0; i < arr.size(); i++) {
                    if(arr[i].first == aux[1].val) {
                        vector <ll> new_dp(3);
                        new_dp[1] = dp[1] + arr[i].second;
                        new_dp[2] = dp[2] + dp[1] * arr[i].second;
                        swap(dp, new_dp);
                    }
                }
                update(ans, {2LL * aux[0].val * aux[1].val, dp[2]});
            }
        }
        else {
            update(ans, {1LL * aux[0].val * (aux[1].val + aux[2].val), 1LL * aux[1].cnt * aux[2].cnt});
        }
    }
    for(auto it : g[nod]) {
        if(it != par) {
            solve(it, nod);
        }
    }
}

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i, n;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n;
    for(i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    int root = -1;
    for(i = 1; i <= n; i++) {
        if(g[i].size() > 1) {
            root = i;
            break;
        }
    }
    if(root > -1) {
        dfs_down(root, 0);
        up[root] = {-MAXN, 0};
        dfs_up(root, 0);
        solve(root, 0);
    }
    cout << ans.first << " " << ans.second;
    //cin.close();
    //cout.close();
    return 0;
}

Compilation message

road.cpp: In function 'void solve(int, int)':
road.cpp:143:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 for(int i = 0; i < arr.size(); i++) {
                                ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12280 KB Output is correct
2 Correct 10 ms 12024 KB Output is correct
3 Correct 11 ms 12024 KB Output is correct
4 Correct 10 ms 12024 KB Output is correct
5 Correct 11 ms 12152 KB Output is correct
6 Correct 10 ms 11996 KB Output is correct
7 Correct 10 ms 12152 KB Output is correct
8 Correct 10 ms 12152 KB Output is correct
9 Correct 12 ms 12152 KB Output is correct
10 Correct 10 ms 12152 KB Output is correct
11 Correct 10 ms 12152 KB Output is correct
12 Correct 10 ms 12152 KB Output is correct
13 Correct 10 ms 12152 KB Output is correct
14 Correct 24 ms 12152 KB Output is correct
15 Correct 10 ms 12152 KB Output is correct
16 Correct 10 ms 12024 KB Output is correct
17 Correct 10 ms 12152 KB Output is correct
18 Correct 10 ms 12152 KB Output is correct
19 Correct 10 ms 12152 KB Output is correct
20 Correct 10 ms 12152 KB Output is correct
21 Correct 9 ms 12024 KB Output is correct
22 Correct 10 ms 12024 KB Output is correct
23 Correct 10 ms 12152 KB Output is correct
24 Correct 10 ms 12024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12280 KB Output is correct
2 Correct 10 ms 12024 KB Output is correct
3 Correct 11 ms 12024 KB Output is correct
4 Correct 10 ms 12024 KB Output is correct
5 Correct 11 ms 12152 KB Output is correct
6 Correct 10 ms 11996 KB Output is correct
7 Correct 10 ms 12152 KB Output is correct
8 Correct 10 ms 12152 KB Output is correct
9 Correct 12 ms 12152 KB Output is correct
10 Correct 10 ms 12152 KB Output is correct
11 Correct 10 ms 12152 KB Output is correct
12 Correct 10 ms 12152 KB Output is correct
13 Correct 10 ms 12152 KB Output is correct
14 Correct 24 ms 12152 KB Output is correct
15 Correct 10 ms 12152 KB Output is correct
16 Correct 10 ms 12024 KB Output is correct
17 Correct 10 ms 12152 KB Output is correct
18 Correct 10 ms 12152 KB Output is correct
19 Correct 10 ms 12152 KB Output is correct
20 Correct 10 ms 12152 KB Output is correct
21 Correct 9 ms 12024 KB Output is correct
22 Correct 10 ms 12024 KB Output is correct
23 Correct 10 ms 12152 KB Output is correct
24 Correct 10 ms 12024 KB Output is correct
25 Correct 13 ms 12664 KB Output is correct
26 Correct 13 ms 12920 KB Output is correct
27 Correct 13 ms 12664 KB Output is correct
28 Correct 13 ms 13048 KB Output is correct
29 Correct 12 ms 12892 KB Output is correct
30 Correct 14 ms 12920 KB Output is correct
31 Correct 14 ms 12920 KB Output is correct
32 Correct 13 ms 12792 KB Output is correct
33 Correct 13 ms 12920 KB Output is correct
34 Correct 13 ms 12920 KB Output is correct
35 Correct 13 ms 13048 KB Output is correct
36 Correct 12 ms 12920 KB Output is correct
37 Correct 16 ms 13020 KB Output is correct
38 Correct 14 ms 13560 KB Output is correct
39 Correct 16 ms 12792 KB Output is correct
40 Correct 15 ms 12536 KB Output is correct
41 Correct 13 ms 12408 KB Output is correct
42 Correct 14 ms 12408 KB Output is correct
43 Correct 13 ms 12412 KB Output is correct
44 Correct 15 ms 12408 KB Output is correct
45 Correct 15 ms 12372 KB Output is correct
46 Correct 14 ms 12412 KB Output is correct
47 Correct 13 ms 12408 KB Output is correct
48 Correct 14 ms 12456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12280 KB Output is correct
2 Correct 10 ms 12024 KB Output is correct
3 Correct 11 ms 12024 KB Output is correct
4 Correct 10 ms 12024 KB Output is correct
5 Correct 11 ms 12152 KB Output is correct
6 Correct 10 ms 11996 KB Output is correct
7 Correct 10 ms 12152 KB Output is correct
8 Correct 10 ms 12152 KB Output is correct
9 Correct 12 ms 12152 KB Output is correct
10 Correct 10 ms 12152 KB Output is correct
11 Correct 10 ms 12152 KB Output is correct
12 Correct 10 ms 12152 KB Output is correct
13 Correct 10 ms 12152 KB Output is correct
14 Correct 24 ms 12152 KB Output is correct
15 Correct 10 ms 12152 KB Output is correct
16 Correct 10 ms 12024 KB Output is correct
17 Correct 10 ms 12152 KB Output is correct
18 Correct 10 ms 12152 KB Output is correct
19 Correct 10 ms 12152 KB Output is correct
20 Correct 10 ms 12152 KB Output is correct
21 Correct 9 ms 12024 KB Output is correct
22 Correct 10 ms 12024 KB Output is correct
23 Correct 10 ms 12152 KB Output is correct
24 Correct 10 ms 12024 KB Output is correct
25 Correct 13 ms 12664 KB Output is correct
26 Correct 13 ms 12920 KB Output is correct
27 Correct 13 ms 12664 KB Output is correct
28 Correct 13 ms 13048 KB Output is correct
29 Correct 12 ms 12892 KB Output is correct
30 Correct 14 ms 12920 KB Output is correct
31 Correct 14 ms 12920 KB Output is correct
32 Correct 13 ms 12792 KB Output is correct
33 Correct 13 ms 12920 KB Output is correct
34 Correct 13 ms 12920 KB Output is correct
35 Correct 13 ms 13048 KB Output is correct
36 Correct 12 ms 12920 KB Output is correct
37 Correct 16 ms 13020 KB Output is correct
38 Correct 14 ms 13560 KB Output is correct
39 Correct 16 ms 12792 KB Output is correct
40 Correct 15 ms 12536 KB Output is correct
41 Correct 13 ms 12408 KB Output is correct
42 Correct 14 ms 12408 KB Output is correct
43 Correct 13 ms 12412 KB Output is correct
44 Correct 15 ms 12408 KB Output is correct
45 Correct 15 ms 12372 KB Output is correct
46 Correct 14 ms 12412 KB Output is correct
47 Correct 13 ms 12408 KB Output is correct
48 Correct 14 ms 12456 KB Output is correct
49 Correct 1037 ms 86608 KB Output is correct
50 Correct 1063 ms 94752 KB Output is correct
51 Correct 972 ms 93912 KB Output is correct
52 Correct 1044 ms 76876 KB Output is correct
53 Correct 954 ms 101204 KB Output is correct
54 Correct 1020 ms 96760 KB Output is correct
55 Correct 925 ms 83452 KB Output is correct
56 Correct 871 ms 72312 KB Output is correct
57 Correct 942 ms 107000 KB Output is correct
58 Correct 943 ms 97784 KB Output is correct
59 Correct 1049 ms 97528 KB Output is correct
60 Correct 935 ms 93208 KB Output is correct
61 Correct 1527 ms 159608 KB Output is correct
62 Correct 1474 ms 138308 KB Output is correct
63 Correct 1416 ms 76524 KB Output is correct
64 Correct 1198 ms 61688 KB Output is correct
65 Correct 960 ms 52344 KB Output is correct
66 Correct 909 ms 47336 KB Output is correct
67 Correct 967 ms 44292 KB Output is correct
68 Correct 899 ms 43460 KB Output is correct
69 Correct 902 ms 43280 KB Output is correct
70 Correct 951 ms 42952 KB Output is correct
71 Correct 1129 ms 42744 KB Output is correct
72 Correct 1079 ms 42932 KB Output is correct
73 Correct 1161 ms 43004 KB Output is correct
74 Correct 1116 ms 43168 KB Output is correct
75 Correct 1064 ms 43996 KB Output is correct
76 Correct 814 ms 44908 KB Output is correct
77 Correct 791 ms 47708 KB Output is correct
78 Correct 564 ms 52796 KB Output is correct