답안 #945512

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
945512 2024-03-14T02:43:04 Z GrandTiger1729 Hard route (IZhO17_road) C++17
100 / 100
854 ms 237908 KB
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    cin.tie(0)->sync_with_stdio(0);
    int n;
    cin >> n;
    vector<vector<int>> g(n);
    for (int i = 0; i < n - 1; i++)
    {
        int u, v;
        cin >> u >> v;
        u--, v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    vector<vector<pair<int, int>>> res(n);
    {
        vector<bool> vis(n);
        function<pair<int, int>(int)> dfs = [&](int u) -> pair<int, int>
        {
            res[u].resize(g[u].size());
            vis[u] = 1;
            pair<int, int> ret(0, 1);
            for (int i = 0; i < g[u].size(); i++)
            {
                int v = g[u][i];
                if (vis[v])
                {
                    continue;
                }
                res[u][i] = dfs(v);
                if (res[u][i].first > ret.first)
                {
                    ret = res[u][i];
                }
                else if (res[u][i].first == ret.first)
                {
                    ret.second += res[u][i].second;
                }
            }
            ret.first++;
            return ret;
        };
        dfs(0);
    }
    long long ans = 0, cnt = 1;
    auto update = [&](long long x, long long y)
    {
        if (y == 0)
        {
            return;
        }
        if (ans < x)
        {
            ans = x;
            cnt = y;
        }
        else if (ans == x)
        {
            cnt += y;
        }
    };
    {
        vector<bool> vis(n);
        function<void(int, pair<int, int>)> dfs = [&](int u, pair<int, int> cur)
        {
            vis[u] = 1;
            multiset<pair<int, int>> st;
            for (int i = 0; i < g[u].size(); i++)
            {
                int v = g[u][i];
                if (vis[v])
                {
                    continue;
                }
                st.insert(res[u][i]);
            }
            if (u != 0)
            {
                st.insert(cur);
            }
            if (st.size() >= 3)
            {
                auto it = --st.end();
                auto it3 = it--;
                auto it2 = it--;
                if (it->first == it3->first)
                {
                    int tot = it2->second + it3->second;
                    long long tot2 = 1ll * it2->second * it2->second + 1ll * it3->second * it3->second;
                    auto it4 = it;
                    while (it4->first == it->first)
                    {
                        tot += it4->second;
                        tot2 += 1ll * it4->second * it4->second;
                      	if (it4 == st.begin())
                        {
                            break;
                        }
                      	it4--;
                    }
                    update(1ll * (it->first + it2->first) * it3->first, (1ll * tot * tot - tot2) / 2);
                }
                else if (it2->first == it3->first)
                {
                    int tot = 0;
                    auto it4 = it;
                    while (it4->first == it->first)
                    {
                        tot += it4->second;
                      	if (it4 == st.begin())
                        {
                            break;
                        }
                      	it4--;
                    }
                    update(1ll * (it->first + it2->first) * it3->first, 1ll * tot * (it2->second + it3->second));
                }
                else if (it->first == it2->first)
                {
                    int tot = it2->second;
                    long long tot2 = 1ll * it2->second * it2->second;
                    auto it4 = it;
                    while (it4->first == it->first)
                    {
                        tot += it4->second;
                        tot2 += 1ll * it4->second * it4->second;
                      	if (it4 == st.begin())
                        {
                            break;
                        }
                      	it4--;
                    }
                    update(1ll * (it->first + it2->first) * it3->first, (1ll * tot * tot - tot2) / 2);
                }
                else
                {
                    int tot = 0;
                    auto it4 = it;
                    while (it4->first == it->first)
                    {
                        tot += it4->second;
                      	if (it4 == st.begin())
                        {
                          	break;
                        }
                      	it4--;
                    }
                    update(1ll * (it->first + it2->first) * it3->first, 1ll * tot * it2->second);
                }
            }
          	multiset<pair<int, int>>().swap(st);
            map<int, int> mp;
            for (int i = 0; i < g[u].size(); i++)
            {
                int v = g[u][i];
                if (vis[v])
                {
                    continue;
                }
                mp[res[u][i].first] += res[u][i].second;
            }
            mp[cur.first] += cur.second;
            for (int i = 0; i < g[u].size(); i++)
            {
                int v = g[u][i];
                if (vis[v])
                {
                    continue;
                }
                mp[res[u][i].first] -= res[u][i].second;
                if ((--mp.end())->second == 0)
                {
                    mp.erase(--mp.end());
                }
                pair<int, int> x = *(--mp.end());
                x.first++;
                dfs(v, x);
                mp[res[u][i].first] += res[u][i].second;
            }
        };
        dfs(0, make_pair(0, 1));
    }
    cout << ans << ' ' << cnt << '\n';
    return 0;
}

Compilation message

road.cpp: In lambda function:
road.cpp:26:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |             for (int i = 0; i < g[u].size(); i++)
      |                             ~~^~~~~~~~~~~~~
road.cpp: In lambda function:
road.cpp:71:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |             for (int i = 0; i < g[u].size(); i++)
      |                             ~~^~~~~~~~~~~~~
road.cpp:156:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  156 |             for (int i = 0; i < g[u].size(); i++)
      |                             ~~^~~~~~~~~~~~~
road.cpp:166:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  166 |             for (int i = 0; i < g[u].size(); i++)
      |                             ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 404 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 404 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 3 ms 1372 KB Output is correct
26 Correct 3 ms 1628 KB Output is correct
27 Correct 4 ms 1880 KB Output is correct
28 Correct 3 ms 1884 KB Output is correct
29 Correct 3 ms 1628 KB Output is correct
30 Correct 3 ms 1704 KB Output is correct
31 Correct 3 ms 1628 KB Output is correct
32 Correct 3 ms 1628 KB Output is correct
33 Correct 3 ms 1628 KB Output is correct
34 Correct 3 ms 1884 KB Output is correct
35 Correct 3 ms 1884 KB Output is correct
36 Correct 3 ms 1632 KB Output is correct
37 Correct 3 ms 1884 KB Output is correct
38 Correct 4 ms 2652 KB Output is correct
39 Correct 3 ms 1372 KB Output is correct
40 Correct 3 ms 1116 KB Output is correct
41 Correct 3 ms 1116 KB Output is correct
42 Correct 3 ms 860 KB Output is correct
43 Correct 3 ms 1056 KB Output is correct
44 Correct 3 ms 860 KB Output is correct
45 Correct 2 ms 860 KB Output is correct
46 Correct 2 ms 860 KB Output is correct
47 Correct 2 ms 1116 KB Output is correct
48 Correct 3 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 404 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 3 ms 1372 KB Output is correct
26 Correct 3 ms 1628 KB Output is correct
27 Correct 4 ms 1880 KB Output is correct
28 Correct 3 ms 1884 KB Output is correct
29 Correct 3 ms 1628 KB Output is correct
30 Correct 3 ms 1704 KB Output is correct
31 Correct 3 ms 1628 KB Output is correct
32 Correct 3 ms 1628 KB Output is correct
33 Correct 3 ms 1628 KB Output is correct
34 Correct 3 ms 1884 KB Output is correct
35 Correct 3 ms 1884 KB Output is correct
36 Correct 3 ms 1632 KB Output is correct
37 Correct 3 ms 1884 KB Output is correct
38 Correct 4 ms 2652 KB Output is correct
39 Correct 3 ms 1372 KB Output is correct
40 Correct 3 ms 1116 KB Output is correct
41 Correct 3 ms 1116 KB Output is correct
42 Correct 3 ms 860 KB Output is correct
43 Correct 3 ms 1056 KB Output is correct
44 Correct 3 ms 860 KB Output is correct
45 Correct 2 ms 860 KB Output is correct
46 Correct 2 ms 860 KB Output is correct
47 Correct 2 ms 1116 KB Output is correct
48 Correct 3 ms 1116 KB Output is correct
49 Correct 534 ms 122932 KB Output is correct
50 Correct 596 ms 134480 KB Output is correct
51 Correct 552 ms 144144 KB Output is correct
52 Correct 573 ms 109476 KB Output is correct
53 Correct 481 ms 137340 KB Output is correct
54 Correct 508 ms 149944 KB Output is correct
55 Correct 460 ms 119376 KB Output is correct
56 Correct 483 ms 132588 KB Output is correct
57 Correct 592 ms 154068 KB Output is correct
58 Correct 550 ms 140884 KB Output is correct
59 Correct 538 ms 140528 KB Output is correct
60 Correct 578 ms 134640 KB Output is correct
61 Correct 854 ms 237908 KB Output is correct
62 Correct 811 ms 207968 KB Output is correct
63 Correct 740 ms 114000 KB Output is correct
64 Correct 729 ms 91260 KB Output is correct
65 Correct 669 ms 76824 KB Output is correct
66 Correct 608 ms 69288 KB Output is correct
67 Correct 608 ms 65364 KB Output is correct
68 Correct 616 ms 63324 KB Output is correct
69 Correct 584 ms 62972 KB Output is correct
70 Correct 607 ms 62644 KB Output is correct
71 Correct 559 ms 62552 KB Output is correct
72 Correct 539 ms 62648 KB Output is correct
73 Correct 625 ms 63028 KB Output is correct
74 Correct 626 ms 63616 KB Output is correct
75 Correct 546 ms 65212 KB Output is correct
76 Correct 539 ms 68128 KB Output is correct
77 Correct 518 ms 77084 KB Output is correct
78 Correct 387 ms 91844 KB Output is correct