Submission #702909

# Submission time Handle Problem Language Result Execution time Memory
702909 2023-02-25T09:58:59 Z finn__ Hard route (IZhO17_road) C++17
52 / 100
524 ms 82032 KB
#include <bits/stdc++.h>
using namespace std;

#define N 500000

vector<unsigned> g[N];
uint64_t d[N], c[N];
uint64_t max_hardness = 0, path_count = 1;

void calc_subtree_height(unsigned u, unsigned p)
{
    d[u] = 0, c[u] = 1;
    for (unsigned const &v : g[u])
    {
        if (v != p)
        {
            calc_subtree_height(v, u);
            if (d[v] + 1 > d[u])
                d[u] = d[v] + 1, c[u] = c[v];
            else if (d[v] + 1 == d[u])
                c[u] += c[v];
        }
    }
}

void calc_maximum_hardness(unsigned u, unsigned p, unsigned pdis, unsigned pcount)
{
    vector<pair<unsigned, unsigned>> leafs;
    if (u || g[u].size() == 1)
        leafs.emplace_back(pdis, pcount);
    for (unsigned const &v : g[u])
        if (v != p)
            leafs.emplace_back(d[v] + 1, c[v]);
    sort(leafs.begin(), leafs.end(), greater<pair<unsigned, unsigned>>());

    if (leafs.size() >= 3 &&
        leafs[0].first * (leafs[1].first + leafs[2].first) >= max_hardness)
    {
        uint64_t n_eq_to_third = 0,
                 curr_hardness = leafs[0].first * (leafs[1].first + leafs[2].first),
                 curr_paths = 0;
        for (auto const &[dis, num] : leafs)
            if (dis == leafs[2].first)
                n_eq_to_third += num;

        if (leafs[1].first == leafs[2].first)
        {
            curr_paths = n_eq_to_third * n_eq_to_third;
            for (auto const &[dis, num] : leafs) // We cannot pair two paths
                if (dis == leafs[2].first)       // going to the same adj. node.
                    curr_paths -= num * num;
            curr_paths >>= 1;
        }
        else if (leafs[0].first == leafs[1].first)
        {
            curr_paths = (leafs[0].second + leafs[1].second) * n_eq_to_third;
        }
        else
        {
            curr_paths = leafs[1].second * n_eq_to_third;
        }

        if (curr_hardness == max_hardness)
            path_count += curr_paths;
        else
            max_hardness = curr_hardness, path_count = curr_paths;
    }

    uint64_t dis1 = leafs[0].first + 1, num1 = 0, dis2 = 0, num2 = 1;
    if (leafs.size() >= 2)
        dis2 = leafs[1].first + 1, num2 = 0;
    for (auto const &[dis, num] : leafs)
        num1 += num * (dis + 1 == dis1), num2 += num * (dis + 1 == dis2);

    for (unsigned const &v : g[u])
    {
        if (v != p)
        {
            if (d[v] + 2 == dis1 && c[v] == num1)
                calc_maximum_hardness(v, u, dis2, num2);
            else if (d[v] + 2 == dis1)
                calc_maximum_hardness(v, u, dis1, num1 - c[v]);
            else
                calc_maximum_hardness(v, u, dis1, num1);
        }
    }
}

int main()
{
    size_t n;
    scanf("%zu", &n);

    for (size_t i = 0; i < n - 1; i++)
    {
        unsigned u, v;
        scanf("%u %u", &u, &v);
        g[u - 1].push_back(v - 1);
        g[v - 1].push_back(u - 1);
    }

    calc_subtree_height(0, UINT_MAX);
    calc_maximum_hardness(0, UINT_MAX, 0, 1);

    printf("%" PRIu64 " %" PRIu64 "\n", max_hardness, path_count);
}

Compilation message

road.cpp: In function 'int main()':
road.cpp:92:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |     scanf("%zu", &n);
      |     ~~~~~^~~~~~~~~~~
road.cpp:97:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |         scanf("%u %u", &u, &v);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 6 ms 12028 KB Output is correct
3 Correct 6 ms 11960 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 7 ms 12060 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 7 ms 12052 KB Output is correct
9 Correct 7 ms 12052 KB Output is correct
10 Correct 6 ms 12056 KB Output is correct
11 Correct 6 ms 11988 KB Output is correct
12 Correct 6 ms 12052 KB Output is correct
13 Correct 7 ms 12052 KB Output is correct
14 Correct 6 ms 11988 KB Output is correct
15 Correct 6 ms 12060 KB Output is correct
16 Correct 7 ms 12056 KB Output is correct
17 Correct 6 ms 11988 KB Output is correct
18 Correct 6 ms 12012 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 6 ms 11988 KB Output is correct
21 Correct 6 ms 11988 KB Output is correct
22 Correct 7 ms 11988 KB Output is correct
23 Correct 6 ms 11988 KB Output is correct
24 Correct 6 ms 11988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 6 ms 12028 KB Output is correct
3 Correct 6 ms 11960 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 7 ms 12060 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 7 ms 12052 KB Output is correct
9 Correct 7 ms 12052 KB Output is correct
10 Correct 6 ms 12056 KB Output is correct
11 Correct 6 ms 11988 KB Output is correct
12 Correct 6 ms 12052 KB Output is correct
13 Correct 7 ms 12052 KB Output is correct
14 Correct 6 ms 11988 KB Output is correct
15 Correct 6 ms 12060 KB Output is correct
16 Correct 7 ms 12056 KB Output is correct
17 Correct 6 ms 11988 KB Output is correct
18 Correct 6 ms 12012 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 6 ms 11988 KB Output is correct
21 Correct 6 ms 11988 KB Output is correct
22 Correct 7 ms 11988 KB Output is correct
23 Correct 6 ms 11988 KB Output is correct
24 Correct 6 ms 11988 KB Output is correct
25 Correct 8 ms 12448 KB Output is correct
26 Correct 9 ms 12628 KB Output is correct
27 Correct 9 ms 12660 KB Output is correct
28 Correct 10 ms 12628 KB Output is correct
29 Correct 8 ms 12628 KB Output is correct
30 Correct 9 ms 12708 KB Output is correct
31 Correct 9 ms 12712 KB Output is correct
32 Correct 9 ms 12580 KB Output is correct
33 Correct 10 ms 12628 KB Output is correct
34 Correct 9 ms 12652 KB Output is correct
35 Correct 10 ms 12708 KB Output is correct
36 Correct 8 ms 12628 KB Output is correct
37 Correct 9 ms 12756 KB Output is correct
38 Correct 9 ms 13012 KB Output is correct
39 Correct 9 ms 12500 KB Output is correct
40 Correct 9 ms 12424 KB Output is correct
41 Correct 9 ms 12404 KB Output is correct
42 Correct 11 ms 12324 KB Output is correct
43 Correct 8 ms 12324 KB Output is correct
44 Correct 8 ms 12244 KB Output is correct
45 Correct 8 ms 12320 KB Output is correct
46 Correct 8 ms 12244 KB Output is correct
47 Correct 8 ms 12324 KB Output is correct
48 Correct 8 ms 12500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 6 ms 12028 KB Output is correct
3 Correct 6 ms 11960 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 7 ms 12060 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 7 ms 12052 KB Output is correct
9 Correct 7 ms 12052 KB Output is correct
10 Correct 6 ms 12056 KB Output is correct
11 Correct 6 ms 11988 KB Output is correct
12 Correct 6 ms 12052 KB Output is correct
13 Correct 7 ms 12052 KB Output is correct
14 Correct 6 ms 11988 KB Output is correct
15 Correct 6 ms 12060 KB Output is correct
16 Correct 7 ms 12056 KB Output is correct
17 Correct 6 ms 11988 KB Output is correct
18 Correct 6 ms 12012 KB Output is correct
19 Correct 6 ms 11988 KB Output is correct
20 Correct 6 ms 11988 KB Output is correct
21 Correct 6 ms 11988 KB Output is correct
22 Correct 7 ms 11988 KB Output is correct
23 Correct 6 ms 11988 KB Output is correct
24 Correct 6 ms 11988 KB Output is correct
25 Correct 8 ms 12448 KB Output is correct
26 Correct 9 ms 12628 KB Output is correct
27 Correct 9 ms 12660 KB Output is correct
28 Correct 10 ms 12628 KB Output is correct
29 Correct 8 ms 12628 KB Output is correct
30 Correct 9 ms 12708 KB Output is correct
31 Correct 9 ms 12712 KB Output is correct
32 Correct 9 ms 12580 KB Output is correct
33 Correct 10 ms 12628 KB Output is correct
34 Correct 9 ms 12652 KB Output is correct
35 Correct 10 ms 12708 KB Output is correct
36 Correct 8 ms 12628 KB Output is correct
37 Correct 9 ms 12756 KB Output is correct
38 Correct 9 ms 13012 KB Output is correct
39 Correct 9 ms 12500 KB Output is correct
40 Correct 9 ms 12424 KB Output is correct
41 Correct 9 ms 12404 KB Output is correct
42 Correct 11 ms 12324 KB Output is correct
43 Correct 8 ms 12324 KB Output is correct
44 Correct 8 ms 12244 KB Output is correct
45 Correct 8 ms 12320 KB Output is correct
46 Correct 8 ms 12244 KB Output is correct
47 Correct 8 ms 12324 KB Output is correct
48 Correct 8 ms 12500 KB Output is correct
49 Correct 512 ms 71908 KB Output is correct
50 Correct 498 ms 77340 KB Output is correct
51 Correct 520 ms 82032 KB Output is correct
52 Correct 524 ms 65468 KB Output is correct
53 Incorrect 424 ms 77516 KB Output isn't correct
54 Halted 0 ms 0 KB -