Submission #170306

# Submission time Handle Problem Language Result Execution time Memory
170306 2019-12-24T15:56:24 Z nvmdava Hard route (IZhO17_road) C++17
100 / 100
1036 ms 64888 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define N 500005

int dep[N], par[N];
bool bl[N];
vector<int> adj[N];

int dfs1(int v, int p){
    par[v] = p;
    dep[v] = dep[p] + 1;
    int res = v;
    for(int& x : adj[v]){
        if(p == x) continue;
        int w = dfs1(x, v);
        if(dep[w] > dep[res])
            res = w;
    }
    return res;
}

ll ans = 0, cnt = 1;

int d[N];
int dia;

bool ok[N];

pair<int, int> dfs3(int v, int p){
    d[v] = d[p] + 1;
    pair<int, int> res = {d[v], 1};
    if(p != 0){
        for(int& x : adj[v]){
            if(x == p) continue;
            pair<int, int> tmp = dfs3(x, v);
            if(res.ff < tmp.ff){
                res = tmp;
            } else if(res.ff == tmp.ff){
                res.ss += tmp.ss;
            }
        }
        return res;
    }
    int t = 0;
    ll cntr = 0, s = 0;
    for(int& x : adj[v]){
        pair<int, int> tmp = dfs3(x, v);
        if(tmp.ff == dia / 2) ++t;
        cntr += s * tmp.ss;
        s += tmp.ss;
    }
    if(t > 2){
        ans = 1LL * dia * dia / 2;
        cnt = cntr;
    }
    return res;
}

pair<int, int> dfs2(int v, int p){
    d[v] = d[p] + 1;
    pair<int, int> res = {d[v], 1};
    
    int sum = 0;
    ll cntr = 0;

    pair<int, int> b1 = {0, 0};

    for(auto& x : adj[v]){
        if(x == p || bl[x]) continue;
        pair<int, int> tmp = dfs2(x, v);
        if(tmp.ff > res.ff){
            res = tmp;
        } else if(tmp.ff == res.ff){
            res.ss += tmp.ss;
        }


        if(tmp.ff + b1.ff > sum){
            cntr = 1LL * tmp.ss * b1.ss;
            sum = tmp.ff + b1.ff;
            if(tmp.ff > b1.ff){
                b1 = tmp;
            } else if(tmp.ff == b1.ff){
                b1.ss += tmp.ss;
            }
        } else if(tmp.ff + b1.ff == sum){
            cntr += 1LL * tmp.ss * b1.ss;
            if(tmp.ff == b1.ff){
                b1.ss += tmp.ss;
            }
        }
    }

    if(ok[v] && sum != res.ff){
        sum -= 2 * d[v];
        ll a2 = 1LL * sum * max(dia - dep[v], dep[v]);
        if(ans < a2){
            ans = a2;
            cnt = cntr;
        } else if(ans == a2)
            cnt += cntr;
    }

    return res;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin>>n;
    dep[0] = -1;
    for(int i = 1; i < n; ++i){
        int v, u;
        cin>>v>>u;
        adj[v].push_back(u);
        adj[u].push_back(v);
    }

    int v1 = dfs1(1, 0);
    int v2 = dfs1(v1, 0);
    d[0] = -1;

    dia = dep[v2];
    if(dia % 2 == 0){
        int w = dia / 2;
        int v = v2;
        while(w--)
            v = par[v];
        dfs3(v, 0);
        if(ans){
            cout<<ans<<' '<<cnt<<'\n';
            return 0;
        }
    }
    int qv = par[v2];
    while(qv != v1){
        ok[qv] = 1;
        qv = par[qv];
    }

    if(dia % 2 == 0){
        int w = dia / 2 - 1;
        int u = v2;
        while(w--)
            u = par[u];
        int u2 = par[u];
        int u3 = par[u2];
        bl[u3] = 1;
        dfs2(u2, 0);
        bl[u3] = 0;
        bl[u] = 1;
        dfs2(u2, 0);
        bl[u] = 0;
    } else {
        int w = dia / 2;
        int u = v2;
        while(w--)
            u = par[u];
        int u2 = par[u];
        bl[u2] = 1;
        dfs2(u, 0);
        bl[u2] = 0;
        bl[u] = 1;
        dfs2(u2, 0);
        bl[u] = 0;
    }
    cout<<ans<<' '<<cnt;
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12152 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 12 ms 12152 KB Output is correct
4 Correct 12 ms 12152 KB Output is correct
5 Correct 12 ms 12152 KB Output is correct
6 Correct 12 ms 12152 KB Output is correct
7 Correct 13 ms 12236 KB Output is correct
8 Correct 13 ms 12156 KB Output is correct
9 Correct 12 ms 12152 KB Output is correct
10 Correct 12 ms 12152 KB Output is correct
11 Correct 13 ms 12152 KB Output is correct
12 Correct 12 ms 12152 KB Output is correct
13 Correct 12 ms 12152 KB Output is correct
14 Correct 12 ms 12152 KB Output is correct
15 Correct 12 ms 12152 KB Output is correct
16 Correct 12 ms 12156 KB Output is correct
17 Correct 12 ms 12152 KB Output is correct
18 Correct 12 ms 12152 KB Output is correct
19 Correct 12 ms 12152 KB Output is correct
20 Correct 12 ms 12152 KB Output is correct
21 Correct 12 ms 12228 KB Output is correct
22 Correct 13 ms 12152 KB Output is correct
23 Correct 12 ms 12152 KB Output is correct
24 Correct 12 ms 12152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12152 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 12 ms 12152 KB Output is correct
4 Correct 12 ms 12152 KB Output is correct
5 Correct 12 ms 12152 KB Output is correct
6 Correct 12 ms 12152 KB Output is correct
7 Correct 13 ms 12236 KB Output is correct
8 Correct 13 ms 12156 KB Output is correct
9 Correct 12 ms 12152 KB Output is correct
10 Correct 12 ms 12152 KB Output is correct
11 Correct 13 ms 12152 KB Output is correct
12 Correct 12 ms 12152 KB Output is correct
13 Correct 12 ms 12152 KB Output is correct
14 Correct 12 ms 12152 KB Output is correct
15 Correct 12 ms 12152 KB Output is correct
16 Correct 12 ms 12156 KB Output is correct
17 Correct 12 ms 12152 KB Output is correct
18 Correct 12 ms 12152 KB Output is correct
19 Correct 12 ms 12152 KB Output is correct
20 Correct 12 ms 12152 KB Output is correct
21 Correct 12 ms 12228 KB Output is correct
22 Correct 13 ms 12152 KB Output is correct
23 Correct 12 ms 12152 KB Output is correct
24 Correct 12 ms 12152 KB Output is correct
25 Correct 15 ms 12536 KB Output is correct
26 Correct 16 ms 12536 KB Output is correct
27 Correct 15 ms 12536 KB Output is correct
28 Correct 15 ms 12536 KB Output is correct
29 Correct 14 ms 12536 KB Output is correct
30 Correct 15 ms 12536 KB Output is correct
31 Correct 15 ms 12536 KB Output is correct
32 Correct 15 ms 12536 KB Output is correct
33 Correct 14 ms 12536 KB Output is correct
34 Correct 15 ms 12536 KB Output is correct
35 Correct 15 ms 12536 KB Output is correct
36 Correct 15 ms 12536 KB Output is correct
37 Correct 15 ms 12664 KB Output is correct
38 Correct 16 ms 12536 KB Output is correct
39 Correct 15 ms 12536 KB Output is correct
40 Correct 15 ms 12408 KB Output is correct
41 Correct 15 ms 12408 KB Output is correct
42 Correct 15 ms 12408 KB Output is correct
43 Correct 15 ms 12408 KB Output is correct
44 Correct 14 ms 12408 KB Output is correct
45 Correct 15 ms 12408 KB Output is correct
46 Correct 14 ms 12152 KB Output is correct
47 Correct 15 ms 12408 KB Output is correct
48 Correct 15 ms 12408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12152 KB Output is correct
2 Correct 12 ms 12152 KB Output is correct
3 Correct 12 ms 12152 KB Output is correct
4 Correct 12 ms 12152 KB Output is correct
5 Correct 12 ms 12152 KB Output is correct
6 Correct 12 ms 12152 KB Output is correct
7 Correct 13 ms 12236 KB Output is correct
8 Correct 13 ms 12156 KB Output is correct
9 Correct 12 ms 12152 KB Output is correct
10 Correct 12 ms 12152 KB Output is correct
11 Correct 13 ms 12152 KB Output is correct
12 Correct 12 ms 12152 KB Output is correct
13 Correct 12 ms 12152 KB Output is correct
14 Correct 12 ms 12152 KB Output is correct
15 Correct 12 ms 12152 KB Output is correct
16 Correct 12 ms 12156 KB Output is correct
17 Correct 12 ms 12152 KB Output is correct
18 Correct 12 ms 12152 KB Output is correct
19 Correct 12 ms 12152 KB Output is correct
20 Correct 12 ms 12152 KB Output is correct
21 Correct 12 ms 12228 KB Output is correct
22 Correct 13 ms 12152 KB Output is correct
23 Correct 12 ms 12152 KB Output is correct
24 Correct 12 ms 12152 KB Output is correct
25 Correct 15 ms 12536 KB Output is correct
26 Correct 16 ms 12536 KB Output is correct
27 Correct 15 ms 12536 KB Output is correct
28 Correct 15 ms 12536 KB Output is correct
29 Correct 14 ms 12536 KB Output is correct
30 Correct 15 ms 12536 KB Output is correct
31 Correct 15 ms 12536 KB Output is correct
32 Correct 15 ms 12536 KB Output is correct
33 Correct 14 ms 12536 KB Output is correct
34 Correct 15 ms 12536 KB Output is correct
35 Correct 15 ms 12536 KB Output is correct
36 Correct 15 ms 12536 KB Output is correct
37 Correct 15 ms 12664 KB Output is correct
38 Correct 16 ms 12536 KB Output is correct
39 Correct 15 ms 12536 KB Output is correct
40 Correct 15 ms 12408 KB Output is correct
41 Correct 15 ms 12408 KB Output is correct
42 Correct 15 ms 12408 KB Output is correct
43 Correct 15 ms 12408 KB Output is correct
44 Correct 14 ms 12408 KB Output is correct
45 Correct 15 ms 12408 KB Output is correct
46 Correct 14 ms 12152 KB Output is correct
47 Correct 15 ms 12408 KB Output is correct
48 Correct 15 ms 12408 KB Output is correct
49 Correct 611 ms 53272 KB Output is correct
50 Correct 670 ms 53412 KB Output is correct
51 Correct 676 ms 53392 KB Output is correct
52 Correct 720 ms 53356 KB Output is correct
53 Correct 503 ms 53056 KB Output is correct
54 Correct 492 ms 53112 KB Output is correct
55 Correct 487 ms 53112 KB Output is correct
56 Correct 503 ms 53048 KB Output is correct
57 Correct 700 ms 58872 KB Output is correct
58 Correct 732 ms 59000 KB Output is correct
59 Correct 741 ms 58988 KB Output is correct
60 Correct 700 ms 59000 KB Output is correct
61 Correct 914 ms 64868 KB Output is correct
62 Correct 1036 ms 64888 KB Output is correct
63 Correct 855 ms 50268 KB Output is correct
64 Correct 817 ms 45532 KB Output is correct
65 Correct 836 ms 43256 KB Output is correct
66 Correct 984 ms 41804 KB Output is correct
67 Correct 953 ms 41304 KB Output is correct
68 Correct 855 ms 41080 KB Output is correct
69 Correct 898 ms 40952 KB Output is correct
70 Correct 921 ms 40824 KB Output is correct
71 Correct 813 ms 40928 KB Output is correct
72 Correct 790 ms 40976 KB Output is correct
73 Correct 838 ms 40864 KB Output is correct
74 Correct 785 ms 41104 KB Output is correct
75 Correct 764 ms 41076 KB Output is correct
76 Correct 760 ms 41072 KB Output is correct
77 Correct 559 ms 41608 KB Output is correct
78 Correct 352 ms 42864 KB Output is correct