답안 #966323

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
966323 2024-04-19T17:30:33 Z eysbutno Hard route (IZhO17_road) C++17
100 / 100
777 ms 140660 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int N = 5e5 + 1;
int max_length[N], path_count[N];
vector<int> adj[N];
ll max_hardness = 0, hardest_path_count = 1;
void dfs(int u, int p) {
    /**
     * Calculates the longest path from vertex u,
     * and the number of such paths.
    */
    max_length[u] = 0;
    path_count[u] = 1;
    for (int v : adj[u]) if (v != p) {
        dfs(v, u);
        if (max_length[u] < max_length[v] + 1) {
            max_length[u] = max_length[v] + 1;
            path_count[u] = path_count[v];
        } else if (max_length[v] + 1 == max_length[u]) {
            path_count[u] += path_count[v];
        }
    }
}
void dfs2(int u, int p, ll parDist, ll parCnt) {
    /**
     * Performs the rerooting, to count the hardest
     * path and the # of such paths at this vertex.
    */
    vector<array<ll, 2>> paths; // {distance, count}
    if (u > 0 || (int) adj[u].size() == 1) {
        paths.push_back({parDist, parCnt});
    }
    for (int v : adj[u]) if (v != p) {
        paths.push_back({max_length[v] + 1, path_count[v]});
    }
    sort(paths.begin(), paths.end(), greater<>());
    if ((int) adj[u].size() >= 3) { // can form a nonzero hard route
        /**
         * Let the 3 longest path lengths be a, b, c, with a > b > c.
         * The optimal hard route "hardness" is a * (b + c).
        */
        ll a = paths[0][0];
        ll b = paths[1][0];
        ll c = paths[2][0];
        ll cur = a * (b + c);
        ll num = 0;
        ll ties = 0;
        for (auto [k, v] : paths) {
            if (k == c) ties += v;
        }

        if (a != b && b != c) {
            // case 1: all are distinct.
            num = paths[1][1] * ties;
        } else if (a == b && b == c) {
            // case 2: all are the same.
            num = ties * ties;
            for (auto [k, v] : paths) {
                if (k == a) num -= v * v;
            }
            num /= 2; // avoiding double counting
        } else if (a == b) {
            // case 3: first two are the same.
            num = (paths[0][1] + paths[1][1]) * ties;
        } else {
            // case 4: last two are the same.
            num = ties * ties;
            for (auto [k, v] : paths) {
                if (k == c) num -= v * v;
            }
            num /= 2; // avoiding double counting
        }

        if (max_hardness < cur) {
            max_hardness = cur;
            hardest_path_count = num;
        } else if (max_hardness == cur) {
            hardest_path_count += num;
        }
    }
    // processing parent dist and parent count.
    ll longest1 = 0;
    ll longest2 = 0;
    ll count1 = 0;
    ll count2 = 0;
    for (auto [k, v] : paths) {
        if (k + 1 > longest1) {
            swap(longest1, longest2);
            swap(count1, count2);
            longest1 = k + 1, count1 = v;
        } else if (k + 1 == longest1) {
            count1 += v;
        } else if (k + 1 > longest2) {
            longest2 = k + 1, count2 = v;
        } else if (k + 1 == longest2) {
            count2 += v;
        }
    }
    for (int v : adj[u]) if (v != p) {
        // using the best parent hardness and parent count possible.
        if (max_length[v] + 2 == longest1) {
            (path_count[v] == count1) ? dfs2(v, u, longest2, count2) :
                                        dfs2(v, u, longest1, count1 - path_count[v]);
        } else {
            dfs2(v, u, longest1, count1);
        }
    }
}
int main() {
    int n; 
    cin >> n;
    for (int i = 1; i < n; i++) {
        int x, y; 
        cin >> x >> y;
        --x, --y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

    dfs(0, -1);
    dfs2(0, -1, 0, 1);

    cout << max_hardness << ' ' << hardest_path_count << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14944 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 4 ms 14940 KB Output is correct
4 Correct 3 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 14936 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 4 ms 14960 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 4 ms 14940 KB Output is correct
11 Correct 4 ms 14940 KB Output is correct
12 Correct 4 ms 14940 KB Output is correct
13 Correct 3 ms 14988 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 3 ms 14944 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
18 Correct 4 ms 14940 KB Output is correct
19 Correct 3 ms 14940 KB Output is correct
20 Correct 3 ms 14940 KB Output is correct
21 Correct 3 ms 14940 KB Output is correct
22 Correct 3 ms 14940 KB Output is correct
23 Correct 3 ms 14940 KB Output is correct
24 Correct 3 ms 14940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14944 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 4 ms 14940 KB Output is correct
4 Correct 3 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 14936 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 4 ms 14960 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 4 ms 14940 KB Output is correct
11 Correct 4 ms 14940 KB Output is correct
12 Correct 4 ms 14940 KB Output is correct
13 Correct 3 ms 14988 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 3 ms 14944 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
18 Correct 4 ms 14940 KB Output is correct
19 Correct 3 ms 14940 KB Output is correct
20 Correct 3 ms 14940 KB Output is correct
21 Correct 3 ms 14940 KB Output is correct
22 Correct 3 ms 14940 KB Output is correct
23 Correct 3 ms 14940 KB Output is correct
24 Correct 3 ms 14940 KB Output is correct
25 Correct 6 ms 15192 KB Output is correct
26 Correct 6 ms 15452 KB Output is correct
27 Correct 6 ms 15452 KB Output is correct
28 Correct 6 ms 15600 KB Output is correct
29 Correct 6 ms 15452 KB Output is correct
30 Correct 6 ms 15452 KB Output is correct
31 Correct 6 ms 15452 KB Output is correct
32 Correct 7 ms 15452 KB Output is correct
33 Correct 6 ms 15448 KB Output is correct
34 Correct 7 ms 15452 KB Output is correct
35 Correct 6 ms 15452 KB Output is correct
36 Correct 6 ms 15452 KB Output is correct
37 Correct 6 ms 15708 KB Output is correct
38 Correct 7 ms 15964 KB Output is correct
39 Correct 6 ms 15452 KB Output is correct
40 Correct 6 ms 15196 KB Output is correct
41 Correct 6 ms 14988 KB Output is correct
42 Correct 6 ms 14936 KB Output is correct
43 Correct 6 ms 14940 KB Output is correct
44 Correct 5 ms 14940 KB Output is correct
45 Correct 6 ms 14936 KB Output is correct
46 Correct 5 ms 14940 KB Output is correct
47 Correct 7 ms 15196 KB Output is correct
48 Correct 6 ms 15196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14944 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 4 ms 14940 KB Output is correct
4 Correct 3 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 14936 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 4 ms 14960 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 4 ms 14940 KB Output is correct
11 Correct 4 ms 14940 KB Output is correct
12 Correct 4 ms 14940 KB Output is correct
13 Correct 3 ms 14988 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 3 ms 14944 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
18 Correct 4 ms 14940 KB Output is correct
19 Correct 3 ms 14940 KB Output is correct
20 Correct 3 ms 14940 KB Output is correct
21 Correct 3 ms 14940 KB Output is correct
22 Correct 3 ms 14940 KB Output is correct
23 Correct 3 ms 14940 KB Output is correct
24 Correct 3 ms 14940 KB Output is correct
25 Correct 6 ms 15192 KB Output is correct
26 Correct 6 ms 15452 KB Output is correct
27 Correct 6 ms 15452 KB Output is correct
28 Correct 6 ms 15600 KB Output is correct
29 Correct 6 ms 15452 KB Output is correct
30 Correct 6 ms 15452 KB Output is correct
31 Correct 6 ms 15452 KB Output is correct
32 Correct 7 ms 15452 KB Output is correct
33 Correct 6 ms 15448 KB Output is correct
34 Correct 7 ms 15452 KB Output is correct
35 Correct 6 ms 15452 KB Output is correct
36 Correct 6 ms 15452 KB Output is correct
37 Correct 6 ms 15708 KB Output is correct
38 Correct 7 ms 15964 KB Output is correct
39 Correct 6 ms 15452 KB Output is correct
40 Correct 6 ms 15196 KB Output is correct
41 Correct 6 ms 14988 KB Output is correct
42 Correct 6 ms 14936 KB Output is correct
43 Correct 6 ms 14940 KB Output is correct
44 Correct 5 ms 14940 KB Output is correct
45 Correct 6 ms 14936 KB Output is correct
46 Correct 5 ms 14940 KB Output is correct
47 Correct 7 ms 15196 KB Output is correct
48 Correct 6 ms 15196 KB Output is correct
49 Correct 524 ms 72612 KB Output is correct
50 Correct 521 ms 80256 KB Output is correct
51 Correct 565 ms 86912 KB Output is correct
52 Correct 496 ms 63584 KB Output is correct
53 Correct 483 ms 82768 KB Output is correct
54 Correct 463 ms 90680 KB Output is correct
55 Correct 450 ms 70604 KB Output is correct
56 Correct 462 ms 79412 KB Output is correct
57 Correct 547 ms 91592 KB Output is correct
58 Correct 510 ms 82784 KB Output is correct
59 Correct 521 ms 82916 KB Output is correct
60 Correct 489 ms 78816 KB Output is correct
61 Correct 777 ms 140660 KB Output is correct
62 Correct 769 ms 120768 KB Output is correct
63 Correct 739 ms 63208 KB Output is correct
64 Correct 746 ms 49236 KB Output is correct
65 Correct 711 ms 40532 KB Output is correct
66 Correct 629 ms 36176 KB Output is correct
67 Correct 660 ms 33360 KB Output is correct
68 Correct 647 ms 32256 KB Output is correct
69 Correct 638 ms 32076 KB Output is correct
70 Correct 613 ms 31948 KB Output is correct
71 Correct 613 ms 31568 KB Output is correct
72 Correct 625 ms 31724 KB Output is correct
73 Correct 695 ms 32080 KB Output is correct
74 Correct 659 ms 32316 KB Output is correct
75 Correct 605 ms 32848 KB Output is correct
76 Correct 583 ms 34120 KB Output is correct
77 Correct 521 ms 37816 KB Output is correct
78 Correct 396 ms 43584 KB Output is correct