답안 #174510

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
174510 2020-01-06T12:02:47 Z emil_physmath Hard route (IZhO17_road) C++17
100 / 100
1208 ms 163964 KB
// #define DEBUG
// #define STRESS_TESTING
#define ALL_LL
#include <algorithm>
#include <vector>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <map>
using namespace std;
typedef long long llong;
#ifdef ALL_LL
#define int long long
#endif
const int maxN = 500005;

vector<int> nei[maxN];
pair<int, int> ray[maxN][3];
map<llong, llong> ans;
vector<pair<int, int>> dists[maxN];

namespace AC
{
    int n;
    pair<int, int> maxdist[maxN];
    bool used[maxN];

    void MaxDist(int u);
    pair<int, int> DFS(int u, int dep, int dist);
    void Solve(int n_)
    {
        n = n_;
        pair<int, int> ans(0, 0);
        for (int u=1; u<=n; u++)
            if (nei[u].size()==1)
            {
                memset(used+1, 0, n*sizeof(bool));
                MaxDist(u);
                memset(used+1, 0, n*sizeof(bool));
                pair<int, int> curAns=DFS(u, 0, 0);
                if (curAns.first>ans.first) ans=curAns;
                else if (curAns.first==ans.first) ans.second+=curAns.second;
            }
        cout<<ans.first<<' '<<ans.second/2<<'\n';
    }

    pair<int, int> DFS(int u, int dep, int dist)
    {
        used[u]=true;
        if (nei[u].size()==1 && used[nei[u][0]])
            return make_pair(dep*dist, 1);
        pair<int, int> ans(0, 0);
        for (int i=0; i<nei[u].size(); i++)
        {
            int to=nei[u][i];
            if (!used[to])
            {
                pair<int, int> curAns;
                if (maxdist[u].first==maxdist[to].first+1)
                    curAns=DFS(to, dep+1, max(dist, maxdist[u].second));
                else
                    curAns=DFS(to, dep+1, max(dist, maxdist[u].first));

                if (curAns.first>ans.first) ans=curAns;
                else if (curAns.first==ans.first) ans.second+=curAns.second;
            }
        }
        return ans;
    }

    void MaxDist(int u)
    {
        used[u]=true;
        maxdist[u]=make_pair(0, 0);
        if (nei[u].size()==1 && used[nei[u][0]]) return;
        for (int i=0; i<nei[u].size(); i++)
        {
            int to=nei[u][i];
            if (!used[to])
            {
                MaxDist(to);
                int temp=maxdist[to].first+1;
                if (temp>maxdist[u].first)
                {
                    swap(maxdist[u].first, maxdist[u].second);
                    maxdist[u].first=temp;
                }
                else if (temp>maxdist[u].second)
                    maxdist[u].second=temp;
            }
        }
    }
};
inline ostream& operator<<(ostream& ostr, const pair<int, int>& a)
{
    return ostr << "{" << a.first << ", " << a.second << "}";
}
void Maximize(pair<int, int>* a, pair<int, int> x)
{
    if (a[0].first == x.first)
        a[0].second += x.second;
    else if (a[1].first == x.first)
        a[1].second += x.second;
    else if (a[2].first == x.first)
        a[2].second += x.second;
    else if (x.first > a[2].first)
    {
        a[2] = x;
        sort(a, a + 3, greater<pair<int, int>>());
    }
}
void SetDown(int v, int par)
{
    fill(ray[v], ray[v] + 3, make_pair(0, 0));
    ray[v][0] = {0, 1};
    for (int to: nei[v])
        if (to != par)
        {
            SetDown(to, v);
            int val, cnt;
            Maximize(ray[v], {(val = ray[to][0].first + 1), (cnt = ray[to][0].second/*1*/)});
            if (val) dists[v].push_back({val, cnt});
        }
}
void SetUp(int v, int par)
{
    for (int to: nei[v])
        if (to != par)
        {
            // Set up[to].
            int val = 0, cnt = 0;
            if (ray[v][0].first == ray[to][0].first + 1 && ray[v][0].second == ray[to][0].second)
                Maximize(ray[to], {(val = ray[v][1].first + 1), (cnt = ray[v][1].second/*1*/)});
            else if (ray[v][0].first == ray[to][0].first + 1)
                Maximize(ray[to], {(val = ray[v][0].first + 1), (cnt = ray[v][0].second - ray[to][0].second/*1*/)});
            else
                Maximize(ray[to], {(val = ray[v][0].first + 1), (cnt = ray[v][0].second)});
            if (val) dists[to].push_back({val, cnt});
            SetUp(to, v);
        }
    sort(dists[v].begin(), dists[v].end(), greater<pair<int, int>>());
#ifdef DEBUG
    cerr << "v: " << v << "\tdists: " << dists[v][0] << ' ';
    if (dists[v].size() >= 2) cout << dists[v][1];
    if (dists[v].size() >= 3) cout << ' ' << dists[v][2];
    cout << endl;
#endif
    if (dists[v].size() < 3) return;
    bool eq = (dists[v][1].first == dists[v][2].first);
    int cnt[2];
    for (int i = 0; i < 3; ++i)
    {
        if (ray[v][i].first == dists[v][1].first)
            cnt[0] = ray[v][i].second;
        if (ray[v][i].first == dists[v][2].first)
            cnt[1] = ray[v][i].second;
    }
    llong delta;
    if (eq)
        ans[dists[v][0].first * (dists[v][1].first + dists[v][2].first)] += (delta = (cnt[0] * (cnt[0] - 1LL)) / 2LL);
    else
        ans[dists[v][0].first * (dists[v][1].first + dists[v][2].first)] += (delta = (llong)cnt[0] * cnt[1]);
    // Subtract x * (x - 1) / 2 for x = rays going to the same `to`
    for (auto p: dists[v])
    {
        llong dlt;
        if ((p.first == dists[v][1].first || p.first == dists[v][2].first) && dists[v][1].first == dists[v][2].first) // erkrord paymany petqa?
        {
#ifdef DEBUG
            if (v == 3)
            {
                cerr << "Good.\tp = " << p << endl;
            }
#endif
            bool f = (p.first == dists[v][0].first);
            // if (f) --p.second;
            ans[dists[v][0].first * (dists[v][1].first + dists[v][2].first)] -=
            (dlt = (p.second * (p.second - 1LL)) / 2LL);
            // if (f) ++p.second;
#ifdef DEBUG
            cerr << "v: " << v << "\tdelta = -" << dlt << endl;
#endif
        }
    }
#ifdef DEBUG
    cerr << "v: " << v << "\tdelta = " << delta << endl;
#endif
}
#ifdef ALL_LL
#undef int
#endif
int main(int argc, char** argv)
#ifdef ALL_LL
#define int long long
#endif
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n;
    // cin >> n;
    // n = rand() % 20 + 1;
#ifndef STRESS_TESTING
    cin >> n;
#endif
#ifdef STRESS_TESTING
    n = 14;
    int seed;
    if (argc == 1)
        cin >> seed;
    else
        seed = std::atoi(argv[1]);
    srand(seed);
#endif
    // cerr << n << endl;
    for (int i = 1; i < n; ++i)
    {
        int u, v;
#ifndef STRESS_TESTING
        cin >> u >> v;
#else
        v = i + 1;
        u = rand() % i + 1;
        // cerr << u << ' ' << v << endl;
#endif
        nei[u].push_back(v);
        nei[v].push_back(u);
    }
    SetDown(1, -1);
    /*for (int v = 1; v <= n; ++v)
        cerr << v << ": " << ray[v][0] << ' ' << ray[v][1] << ' ' << ray[v][2] << endl;*/
    SetUp(1, -1);
#ifdef DEBUG
    for (int v = 1; v <= n; ++v)
    {
        cerr << v << ": " << ray[v][0] << ' ' << ray[v][1] << ' ' << ray[v][2] << endl;
    }
    for (auto it: ans)
        cerr << it << endl;
#endif
    if (ans.size())
        cout << ans.rbegin()->first << ' ' << ans.rbegin()->second;
    else
        cout << "0 1";
#ifdef STRESS_TESTING
    cout << endl;
    AC::Solve(n);
#endif
}
/*
seed: 78587235
1 2
1 3
3 4
4 5
3 6
6 7
4 8
1 9

8 10
8 8 / right answer
*/

Compilation message

road.cpp: In function 'std::pair<long long int, long long int> AC::DFS(long long int, long long int, long long int)':
road.cpp:53:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i=0; i<nei[u].size(); i++)
                       ~^~~~~~~~~~~~~~
road.cpp: In function 'void AC::MaxDist(long long int)':
road.cpp:76:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i=0; i<nei[u].size(); i++)
                       ~^~~~~~~~~~~~~~
road.cpp: In function 'void SetUp(long long int, long long int)':
road.cpp:175:18: warning: unused variable 'f' [-Wunused-variable]
             bool f = (p.first == dists[v][0].first);
                  ^
road.cpp:162:84: warning: 'cnt[1]' may be used uninitialized in this function [-Wmaybe-uninitialized]
         ans[dists[v][0].first * (dists[v][1].first + dists[v][2].first)] += (delta = (llong)cnt[0] * cnt[1]);
                                                                             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
road.cpp:160:104: warning: 'cnt[0]' may be used uninitialized in this function [-Wmaybe-uninitialized]
         ans[dists[v][0].first * (dists[v][1].first + dists[v][2].first)] += (delta = (cnt[0] * (cnt[0] - 1LL)) / 2LL);
                                                                                                ~~~~~~~~^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23772 KB Output is correct
2 Correct 24 ms 23800 KB Output is correct
3 Correct 24 ms 23800 KB Output is correct
4 Correct 24 ms 23860 KB Output is correct
5 Correct 24 ms 23800 KB Output is correct
6 Correct 25 ms 23800 KB Output is correct
7 Correct 24 ms 23800 KB Output is correct
8 Correct 24 ms 23800 KB Output is correct
9 Correct 24 ms 23800 KB Output is correct
10 Correct 24 ms 23800 KB Output is correct
11 Correct 24 ms 23804 KB Output is correct
12 Correct 24 ms 23800 KB Output is correct
13 Correct 26 ms 24056 KB Output is correct
14 Correct 18 ms 23672 KB Output is correct
15 Correct 26 ms 23924 KB Output is correct
16 Correct 24 ms 23804 KB Output is correct
17 Correct 24 ms 23800 KB Output is correct
18 Correct 29 ms 23928 KB Output is correct
19 Correct 29 ms 23800 KB Output is correct
20 Correct 29 ms 23800 KB Output is correct
21 Correct 29 ms 23868 KB Output is correct
22 Correct 29 ms 23800 KB Output is correct
23 Correct 24 ms 23800 KB Output is correct
24 Correct 23 ms 23800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23772 KB Output is correct
2 Correct 24 ms 23800 KB Output is correct
3 Correct 24 ms 23800 KB Output is correct
4 Correct 24 ms 23860 KB Output is correct
5 Correct 24 ms 23800 KB Output is correct
6 Correct 25 ms 23800 KB Output is correct
7 Correct 24 ms 23800 KB Output is correct
8 Correct 24 ms 23800 KB Output is correct
9 Correct 24 ms 23800 KB Output is correct
10 Correct 24 ms 23800 KB Output is correct
11 Correct 24 ms 23804 KB Output is correct
12 Correct 24 ms 23800 KB Output is correct
13 Correct 26 ms 24056 KB Output is correct
14 Correct 18 ms 23672 KB Output is correct
15 Correct 26 ms 23924 KB Output is correct
16 Correct 24 ms 23804 KB Output is correct
17 Correct 24 ms 23800 KB Output is correct
18 Correct 29 ms 23928 KB Output is correct
19 Correct 29 ms 23800 KB Output is correct
20 Correct 29 ms 23800 KB Output is correct
21 Correct 29 ms 23868 KB Output is correct
22 Correct 29 ms 23800 KB Output is correct
23 Correct 24 ms 23800 KB Output is correct
24 Correct 23 ms 23800 KB Output is correct
25 Correct 17 ms 24952 KB Output is correct
26 Correct 29 ms 24944 KB Output is correct
27 Correct 34 ms 24928 KB Output is correct
28 Correct 33 ms 24952 KB Output is correct
29 Correct 31 ms 25052 KB Output is correct
30 Correct 28 ms 24952 KB Output is correct
31 Correct 34 ms 24952 KB Output is correct
32 Correct 28 ms 24952 KB Output is correct
33 Correct 33 ms 24824 KB Output is correct
34 Correct 29 ms 24824 KB Output is correct
35 Correct 29 ms 24952 KB Output is correct
36 Correct 34 ms 24952 KB Output is correct
37 Correct 34 ms 24968 KB Output is correct
38 Correct 30 ms 25384 KB Output is correct
39 Correct 32 ms 24824 KB Output is correct
40 Correct 32 ms 24904 KB Output is correct
41 Correct 33 ms 24696 KB Output is correct
42 Correct 32 ms 24568 KB Output is correct
43 Correct 33 ms 24568 KB Output is correct
44 Correct 33 ms 24568 KB Output is correct
45 Correct 33 ms 24440 KB Output is correct
46 Correct 36 ms 24444 KB Output is correct
47 Correct 33 ms 24568 KB Output is correct
48 Correct 33 ms 24568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23772 KB Output is correct
2 Correct 24 ms 23800 KB Output is correct
3 Correct 24 ms 23800 KB Output is correct
4 Correct 24 ms 23860 KB Output is correct
5 Correct 24 ms 23800 KB Output is correct
6 Correct 25 ms 23800 KB Output is correct
7 Correct 24 ms 23800 KB Output is correct
8 Correct 24 ms 23800 KB Output is correct
9 Correct 24 ms 23800 KB Output is correct
10 Correct 24 ms 23800 KB Output is correct
11 Correct 24 ms 23804 KB Output is correct
12 Correct 24 ms 23800 KB Output is correct
13 Correct 26 ms 24056 KB Output is correct
14 Correct 18 ms 23672 KB Output is correct
15 Correct 26 ms 23924 KB Output is correct
16 Correct 24 ms 23804 KB Output is correct
17 Correct 24 ms 23800 KB Output is correct
18 Correct 29 ms 23928 KB Output is correct
19 Correct 29 ms 23800 KB Output is correct
20 Correct 29 ms 23800 KB Output is correct
21 Correct 29 ms 23868 KB Output is correct
22 Correct 29 ms 23800 KB Output is correct
23 Correct 24 ms 23800 KB Output is correct
24 Correct 23 ms 23800 KB Output is correct
25 Correct 17 ms 24952 KB Output is correct
26 Correct 29 ms 24944 KB Output is correct
27 Correct 34 ms 24928 KB Output is correct
28 Correct 33 ms 24952 KB Output is correct
29 Correct 31 ms 25052 KB Output is correct
30 Correct 28 ms 24952 KB Output is correct
31 Correct 34 ms 24952 KB Output is correct
32 Correct 28 ms 24952 KB Output is correct
33 Correct 33 ms 24824 KB Output is correct
34 Correct 29 ms 24824 KB Output is correct
35 Correct 29 ms 24952 KB Output is correct
36 Correct 34 ms 24952 KB Output is correct
37 Correct 34 ms 24968 KB Output is correct
38 Correct 30 ms 25384 KB Output is correct
39 Correct 32 ms 24824 KB Output is correct
40 Correct 32 ms 24904 KB Output is correct
41 Correct 33 ms 24696 KB Output is correct
42 Correct 32 ms 24568 KB Output is correct
43 Correct 33 ms 24568 KB Output is correct
44 Correct 33 ms 24568 KB Output is correct
45 Correct 33 ms 24440 KB Output is correct
46 Correct 36 ms 24444 KB Output is correct
47 Correct 33 ms 24568 KB Output is correct
48 Correct 33 ms 24568 KB Output is correct
49 Correct 1010 ms 118048 KB Output is correct
50 Correct 1161 ms 123124 KB Output is correct
51 Correct 961 ms 126936 KB Output is correct
52 Correct 1044 ms 111992 KB Output is correct
53 Correct 965 ms 133312 KB Output is correct
54 Correct 988 ms 142640 KB Output is correct
55 Correct 1193 ms 131312 KB Output is correct
56 Correct 1058 ms 136172 KB Output is correct
57 Correct 940 ms 132244 KB Output is correct
58 Correct 965 ms 126588 KB Output is correct
59 Correct 936 ms 126708 KB Output is correct
60 Correct 936 ms 124024 KB Output is correct
61 Correct 1208 ms 163964 KB Output is correct
62 Correct 1175 ms 151208 KB Output is correct
63 Correct 1155 ms 113912 KB Output is correct
64 Correct 1136 ms 105036 KB Output is correct
65 Correct 1106 ms 99500 KB Output is correct
66 Correct 1090 ms 96432 KB Output is correct
67 Correct 1049 ms 94776 KB Output is correct
68 Correct 1044 ms 94172 KB Output is correct
69 Correct 1037 ms 93984 KB Output is correct
70 Correct 1026 ms 93712 KB Output is correct
71 Correct 1012 ms 93584 KB Output is correct
72 Correct 1074 ms 93816 KB Output is correct
73 Correct 1002 ms 93732 KB Output is correct
74 Correct 988 ms 94164 KB Output is correct
75 Correct 1042 ms 94884 KB Output is correct
76 Correct 1000 ms 94524 KB Output is correct
77 Correct 863 ms 95368 KB Output is correct
78 Correct 679 ms 97540 KB Output is correct