Submission #941890

# Submission time Handle Problem Language Result Execution time Memory
941890 2024-03-09T16:10:19 Z LonlyR Hard route (IZhO17_road) C++17
100 / 100
734 ms 229892 KB
#include<bits/stdc++.h>
#define int long long
#define ii pair<int,int>
#define ff first
#define ss second

using namespace std;
const int maxn = 5e5 + 5;
int n;
ii res, dp[maxn];
vector<int> adj[maxn];

inline ii upd(ii a, int x, int y)
{
    if (a.ff < x)
        a = {x, y};
    else if (a.ff == x)
        a.ss += y;
    return a;
}

void dfs(int x = 1, int p = 1)
{
    dp[x].ss = 1;
    for (int i : adj[x]) if (i != p)
    {
        dfs(i, x);
        dp[x] = upd(dp[x], dp[i].ff + 1, dp[i].ss);
    }
}

void reroot(int x = 1, int p = 1, ii fp = {0, 1})
{
    vector<ii> pre = {fp}, suf = {{0, 0}}, cand;
    if (x > 1 || adj[x].size() == 1)
        cand.emplace_back(fp);
    for (int i : adj[x]) if (i != p)
        cand.emplace_back(dp[i].ff + 1, dp[i].ss);
    sort(cand.begin(), cand.end(), greater<ii>());
    if (cand.size() >= 3)
    {
        int hardness = cand[0].ff * (cand[1].ff + cand[2].ff), ways = 0, tmp = 0;
        for (auto i : cand) if (i.ff == cand[2].ff) tmp += i.ss;
        if (cand[1].ff == cand[2].ff)
        {
            ways = tmp * tmp;
            for (auto i : cand) if (i.ff == cand[2].ff)
                ways -= i.ss * i.ss;
            ways /= 2;
        }
        else if (cand[0].ff == cand[1].ff)
        {
            ways = (cand[0].ss + cand[1].ss) * tmp;
        } else ways = cand[1].ss * tmp;
        res = upd(res, hardness, ways);
    }
    for (int i : adj[x]) if (i != p) pre.emplace_back(upd(pre.back(), dp[i].ff + 1, dp[i].ss));
    for (int j = (int)adj[x].size() - 1; j >= 0; j--)
    {
        int i = adj[x][j];
        if (i == p) continue;
        suf.emplace_back(upd(suf.back(), dp[i].ff + 1, dp[i].ss));
    }
    reverse(suf.begin(), suf.end());
    int cnt = 0;
    for (int i : adj[x]) if (i != p)
    {
        pre[cnt].ff++;
        ii xx = upd(pre[cnt], suf[cnt + 1].ff + 1, suf[cnt + 1].ss);
        reroot(i, x, xx);
        cnt++;
    }
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
//    freopen("test.inp", "r", stdin);
//    freopen("test.out", "w", stdout);
    cin >> n;
    for (int i = 1, u, v; i < n; i++)
        cin >> u >> v,
        adj[u].emplace_back(v),
        adj[v].emplace_back(u);
    dfs();
    reroot();
    cout << res.ff << " " << max(1ll, res.ss);
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 4 ms 12892 KB Output is correct
3 Correct 4 ms 12892 KB Output is correct
4 Correct 3 ms 12892 KB Output is correct
5 Correct 4 ms 12892 KB Output is correct
6 Correct 3 ms 12888 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 3 ms 12888 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
11 Correct 3 ms 12892 KB Output is correct
12 Correct 3 ms 12916 KB Output is correct
13 Correct 5 ms 13148 KB Output is correct
14 Correct 4 ms 12948 KB Output is correct
15 Correct 3 ms 12968 KB Output is correct
16 Correct 3 ms 12888 KB Output is correct
17 Correct 3 ms 12892 KB Output is correct
18 Correct 4 ms 12964 KB Output is correct
19 Correct 3 ms 12892 KB Output is correct
20 Correct 3 ms 12892 KB Output is correct
21 Correct 3 ms 12904 KB Output is correct
22 Correct 3 ms 12888 KB Output is correct
23 Correct 3 ms 12892 KB Output is correct
24 Correct 3 ms 12892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 4 ms 12892 KB Output is correct
3 Correct 4 ms 12892 KB Output is correct
4 Correct 3 ms 12892 KB Output is correct
5 Correct 4 ms 12892 KB Output is correct
6 Correct 3 ms 12888 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 3 ms 12888 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
11 Correct 3 ms 12892 KB Output is correct
12 Correct 3 ms 12916 KB Output is correct
13 Correct 5 ms 13148 KB Output is correct
14 Correct 4 ms 12948 KB Output is correct
15 Correct 3 ms 12968 KB Output is correct
16 Correct 3 ms 12888 KB Output is correct
17 Correct 3 ms 12892 KB Output is correct
18 Correct 4 ms 12964 KB Output is correct
19 Correct 3 ms 12892 KB Output is correct
20 Correct 3 ms 12892 KB Output is correct
21 Correct 3 ms 12904 KB Output is correct
22 Correct 3 ms 12888 KB Output is correct
23 Correct 3 ms 12892 KB Output is correct
24 Correct 3 ms 12892 KB Output is correct
25 Correct 5 ms 13660 KB Output is correct
26 Correct 5 ms 13916 KB Output is correct
27 Correct 5 ms 13916 KB Output is correct
28 Correct 5 ms 13932 KB Output is correct
29 Correct 6 ms 13828 KB Output is correct
30 Correct 5 ms 14172 KB Output is correct
31 Correct 5 ms 14172 KB Output is correct
32 Correct 5 ms 13916 KB Output is correct
33 Correct 6 ms 13860 KB Output is correct
34 Correct 5 ms 14168 KB Output is correct
35 Correct 5 ms 13928 KB Output is correct
36 Correct 6 ms 13916 KB Output is correct
37 Correct 6 ms 14172 KB Output is correct
38 Correct 6 ms 15020 KB Output is correct
39 Correct 5 ms 13660 KB Output is correct
40 Correct 5 ms 13404 KB Output is correct
41 Correct 5 ms 13148 KB Output is correct
42 Correct 5 ms 13344 KB Output is correct
43 Correct 6 ms 13148 KB Output is correct
44 Correct 5 ms 13148 KB Output is correct
45 Correct 4 ms 13144 KB Output is correct
46 Correct 4 ms 13148 KB Output is correct
47 Correct 5 ms 13148 KB Output is correct
48 Correct 4 ms 13404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 4 ms 12892 KB Output is correct
3 Correct 4 ms 12892 KB Output is correct
4 Correct 3 ms 12892 KB Output is correct
5 Correct 4 ms 12892 KB Output is correct
6 Correct 3 ms 12888 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 3 ms 12888 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
11 Correct 3 ms 12892 KB Output is correct
12 Correct 3 ms 12916 KB Output is correct
13 Correct 5 ms 13148 KB Output is correct
14 Correct 4 ms 12948 KB Output is correct
15 Correct 3 ms 12968 KB Output is correct
16 Correct 3 ms 12888 KB Output is correct
17 Correct 3 ms 12892 KB Output is correct
18 Correct 4 ms 12964 KB Output is correct
19 Correct 3 ms 12892 KB Output is correct
20 Correct 3 ms 12892 KB Output is correct
21 Correct 3 ms 12904 KB Output is correct
22 Correct 3 ms 12888 KB Output is correct
23 Correct 3 ms 12892 KB Output is correct
24 Correct 3 ms 12892 KB Output is correct
25 Correct 5 ms 13660 KB Output is correct
26 Correct 5 ms 13916 KB Output is correct
27 Correct 5 ms 13916 KB Output is correct
28 Correct 5 ms 13932 KB Output is correct
29 Correct 6 ms 13828 KB Output is correct
30 Correct 5 ms 14172 KB Output is correct
31 Correct 5 ms 14172 KB Output is correct
32 Correct 5 ms 13916 KB Output is correct
33 Correct 6 ms 13860 KB Output is correct
34 Correct 5 ms 14168 KB Output is correct
35 Correct 5 ms 13928 KB Output is correct
36 Correct 6 ms 13916 KB Output is correct
37 Correct 6 ms 14172 KB Output is correct
38 Correct 6 ms 15020 KB Output is correct
39 Correct 5 ms 13660 KB Output is correct
40 Correct 5 ms 13404 KB Output is correct
41 Correct 5 ms 13148 KB Output is correct
42 Correct 5 ms 13344 KB Output is correct
43 Correct 6 ms 13148 KB Output is correct
44 Correct 5 ms 13148 KB Output is correct
45 Correct 4 ms 13144 KB Output is correct
46 Correct 4 ms 13148 KB Output is correct
47 Correct 5 ms 13148 KB Output is correct
48 Correct 4 ms 13404 KB Output is correct
49 Correct 446 ms 113948 KB Output is correct
50 Correct 440 ms 126888 KB Output is correct
51 Correct 491 ms 138480 KB Output is correct
52 Correct 425 ms 98356 KB Output is correct
53 Correct 404 ms 142120 KB Output is correct
54 Correct 409 ms 157568 KB Output is correct
55 Correct 381 ms 119616 KB Output is correct
56 Correct 385 ms 136172 KB Output is correct
57 Correct 441 ms 145256 KB Output is correct
58 Correct 427 ms 130648 KB Output is correct
59 Correct 465 ms 130156 KB Output is correct
60 Correct 447 ms 123476 KB Output is correct
61 Correct 734 ms 229892 KB Output is correct
62 Correct 705 ms 195920 KB Output is correct
63 Correct 678 ms 96620 KB Output is correct
64 Correct 650 ms 73088 KB Output is correct
65 Correct 657 ms 58152 KB Output is correct
66 Correct 581 ms 50000 KB Output is correct
67 Correct 504 ms 45648 KB Output is correct
68 Correct 528 ms 43860 KB Output is correct
69 Correct 554 ms 43480 KB Output is correct
70 Correct 499 ms 42976 KB Output is correct
71 Correct 553 ms 42760 KB Output is correct
72 Correct 526 ms 43092 KB Output is correct
73 Correct 588 ms 43540 KB Output is correct
74 Correct 479 ms 44240 KB Output is correct
75 Correct 571 ms 46280 KB Output is correct
76 Correct 450 ms 49612 KB Output is correct
77 Correct 384 ms 60168 KB Output is correct
78 Correct 267 ms 76468 KB Output is correct