Submission #91267

# Submission time Handle Problem Language Result Execution time Memory
91267 2018-12-26T19:55:14 Z popovicirobert Hard route (IZhO17_road) C++14
52 / 100
1026 ms 88060 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() < arr[2]) {
        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 12152 KB Output is correct
2 Correct 11 ms 12156 KB Output is correct
3 Correct 11 ms 12232 KB Output is correct
4 Correct 11 ms 12364 KB Output is correct
5 Correct 11 ms 12364 KB Output is correct
6 Correct 12 ms 12364 KB Output is correct
7 Correct 12 ms 12364 KB Output is correct
8 Correct 11 ms 12364 KB Output is correct
9 Correct 11 ms 12364 KB Output is correct
10 Correct 11 ms 12364 KB Output is correct
11 Correct 11 ms 12364 KB Output is correct
12 Correct 11 ms 12364 KB Output is correct
13 Correct 11 ms 12364 KB Output is correct
14 Correct 12 ms 12364 KB Output is correct
15 Correct 10 ms 12364 KB Output is correct
16 Correct 10 ms 12368 KB Output is correct
17 Correct 10 ms 12368 KB Output is correct
18 Correct 10 ms 12368 KB Output is correct
19 Correct 10 ms 12384 KB Output is correct
20 Correct 11 ms 12384 KB Output is correct
21 Correct 11 ms 12384 KB Output is correct
22 Correct 12 ms 12384 KB Output is correct
23 Correct 9 ms 12384 KB Output is correct
24 Correct 10 ms 12384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12152 KB Output is correct
2 Correct 11 ms 12156 KB Output is correct
3 Correct 11 ms 12232 KB Output is correct
4 Correct 11 ms 12364 KB Output is correct
5 Correct 11 ms 12364 KB Output is correct
6 Correct 12 ms 12364 KB Output is correct
7 Correct 12 ms 12364 KB Output is correct
8 Correct 11 ms 12364 KB Output is correct
9 Correct 11 ms 12364 KB Output is correct
10 Correct 11 ms 12364 KB Output is correct
11 Correct 11 ms 12364 KB Output is correct
12 Correct 11 ms 12364 KB Output is correct
13 Correct 11 ms 12364 KB Output is correct
14 Correct 12 ms 12364 KB Output is correct
15 Correct 10 ms 12364 KB Output is correct
16 Correct 10 ms 12368 KB Output is correct
17 Correct 10 ms 12368 KB Output is correct
18 Correct 10 ms 12368 KB Output is correct
19 Correct 10 ms 12384 KB Output is correct
20 Correct 11 ms 12384 KB Output is correct
21 Correct 11 ms 12384 KB Output is correct
22 Correct 12 ms 12384 KB Output is correct
23 Correct 9 ms 12384 KB Output is correct
24 Correct 10 ms 12384 KB Output is correct
25 Correct 13 ms 12908 KB Output is correct
26 Correct 13 ms 13036 KB Output is correct
27 Correct 13 ms 13036 KB Output is correct
28 Correct 15 ms 13164 KB Output is correct
29 Correct 15 ms 13164 KB Output is correct
30 Correct 15 ms 13312 KB Output is correct
31 Correct 17 ms 13312 KB Output is correct
32 Correct 14 ms 13312 KB Output is correct
33 Correct 27 ms 13312 KB Output is correct
34 Correct 14 ms 13312 KB Output is correct
35 Correct 13 ms 13312 KB Output is correct
36 Correct 13 ms 13312 KB Output is correct
37 Correct 14 ms 13312 KB Output is correct
38 Correct 14 ms 13696 KB Output is correct
39 Correct 15 ms 13696 KB Output is correct
40 Correct 13 ms 13696 KB Output is correct
41 Correct 13 ms 13696 KB Output is correct
42 Correct 13 ms 13696 KB Output is correct
43 Correct 13 ms 13696 KB Output is correct
44 Correct 13 ms 13696 KB Output is correct
45 Correct 13 ms 13696 KB Output is correct
46 Correct 12 ms 13696 KB Output is correct
47 Correct 13 ms 13696 KB Output is correct
48 Correct 13 ms 13696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12152 KB Output is correct
2 Correct 11 ms 12156 KB Output is correct
3 Correct 11 ms 12232 KB Output is correct
4 Correct 11 ms 12364 KB Output is correct
5 Correct 11 ms 12364 KB Output is correct
6 Correct 12 ms 12364 KB Output is correct
7 Correct 12 ms 12364 KB Output is correct
8 Correct 11 ms 12364 KB Output is correct
9 Correct 11 ms 12364 KB Output is correct
10 Correct 11 ms 12364 KB Output is correct
11 Correct 11 ms 12364 KB Output is correct
12 Correct 11 ms 12364 KB Output is correct
13 Correct 11 ms 12364 KB Output is correct
14 Correct 12 ms 12364 KB Output is correct
15 Correct 10 ms 12364 KB Output is correct
16 Correct 10 ms 12368 KB Output is correct
17 Correct 10 ms 12368 KB Output is correct
18 Correct 10 ms 12368 KB Output is correct
19 Correct 10 ms 12384 KB Output is correct
20 Correct 11 ms 12384 KB Output is correct
21 Correct 11 ms 12384 KB Output is correct
22 Correct 12 ms 12384 KB Output is correct
23 Correct 9 ms 12384 KB Output is correct
24 Correct 10 ms 12384 KB Output is correct
25 Correct 13 ms 12908 KB Output is correct
26 Correct 13 ms 13036 KB Output is correct
27 Correct 13 ms 13036 KB Output is correct
28 Correct 15 ms 13164 KB Output is correct
29 Correct 15 ms 13164 KB Output is correct
30 Correct 15 ms 13312 KB Output is correct
31 Correct 17 ms 13312 KB Output is correct
32 Correct 14 ms 13312 KB Output is correct
33 Correct 27 ms 13312 KB Output is correct
34 Correct 14 ms 13312 KB Output is correct
35 Correct 13 ms 13312 KB Output is correct
36 Correct 13 ms 13312 KB Output is correct
37 Correct 14 ms 13312 KB Output is correct
38 Correct 14 ms 13696 KB Output is correct
39 Correct 15 ms 13696 KB Output is correct
40 Correct 13 ms 13696 KB Output is correct
41 Correct 13 ms 13696 KB Output is correct
42 Correct 13 ms 13696 KB Output is correct
43 Correct 13 ms 13696 KB Output is correct
44 Correct 13 ms 13696 KB Output is correct
45 Correct 13 ms 13696 KB Output is correct
46 Correct 12 ms 13696 KB Output is correct
47 Correct 13 ms 13696 KB Output is correct
48 Correct 13 ms 13696 KB Output is correct
49 Correct 939 ms 79864 KB Output is correct
50 Correct 1004 ms 88060 KB Output is correct
51 Correct 1026 ms 88060 KB Output is correct
52 Incorrect 989 ms 88060 KB Output isn't correct
53 Halted 0 ms 0 KB -