Submission #343925

# Submission time Handle Problem Language Result Execution time Memory
343925 2021-01-04T18:39:09 Z Sprdalo Hard route (IZhO17_road) C++17
100 / 100
933 ms 144200 KB
#include <bits/stdc++.h>

using namespace std;

#define int ll
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pi> vp;
typedef vector<pl> vpl;

const int maxn = 5e5+5;
vi e[maxn], p(maxn);
vp dp[maxn], d(maxn, {0,1});

void update_pi(pi& a, pi b){
    if (a.first == b.first)
        a.second += b.second;
    if (a.first < b.first)
        a = b;
}

pi dfs(int x, int st){
    pi q = {0, 1};
    for (auto& i : e[x]){
        if (i == st){
            p[x] = i;
            continue;
        }

        pi y = dfs(i, x);
        ++y.first;

        update_pi(q, y);

        dp[x].push_back(y);
    }

    if (q.first == 0)
        dp[x].push_back(q);

    return q;
}

void calc(int x){
    pi q = *max_element(dp[x].begin(), dp[x].end());

    pi t = q; t.second = 0;
    for (int i = 0; i < dp[x].size(); ++i){
        update_pi(t, dp[x][i]);
    }

    pi s = {0,1};
    for (int i = 0; i < dp[x].size(); ++i){
        if (dp[x][i].first == t.first) continue;
        update_pi(s, dp[x][i]);
    }

    int cur = 0;
    for (auto& i : e[x]){
        if (p[x] == i) continue;

        if (dp[x][cur].first == t.first && dp[x][cur].second == t.second){
            d[i] = d[x]; d[i].first++;
            if (s.first>0)
            update_pi(d[i], {s.first+1, s.second});
        } else {
            pi tmp = t;
            if (dp[x][cur].first == t.first)
                tmp.second -= dp[x][cur].second;
            d[i] = d[x]; d[i].first++;
            update_pi(d[i], {tmp.first+1, tmp.second});
        }

        calc(i);
        ++cur;
    }
}

pi sol = {0, 1};
void solve(int x){
    int len = dp[x].size();
    if ((len >= 2 && d[x].first > 0) || len >= 3){
        pi a = {0, 1}, b = {0,1}, c = {0, 1};
        dp[x].push_back(d[x]);

       // sort(dp[x].rbegin(), dp[x].rend());
        //a = dp[x][0]; b = dp[x][1]; c = dp[x][2];
        for (pi t : dp[x]){
            if (t.first > a.first){
                swap(b, c);
                swap(a, b);
                a = t;
                continue;
            }

            if (t.first > b.first){
                swap(b, c);
                b = t;
                continue;
            }

            if (t.first > c.first)
                c = t;
        }    

        int cnt = 0, cc = 0;
        for (pi t : dp[x]){
            if (t.first == c.first){
                ++cc;
                cnt += t.second;
            }
        }

        //cout << x << ' ' << a.first << ' ' << b.first << ' ' << c.first << ' ';
        pi res = {a.first * (b.first + c.first), 0};
        //if (res.first == 1980) cout << "BRATE " << a.first << ' ' << b.first << ' ' << c.first << ' ' << d[x].first << '\n';
        //cout << res.first << ' ';
        if (a.first == b.first && a.first == c.first){
            //if (res.first == 1152) cout << "PRVI\n";
            for (pi t : dp[x]){
                if (t.first != c.first) continue;
                res.second += t.second * (cnt - t.second);
            }
            //res.second *=2;
          //  res.second /= 3;
        }

        if (a.first == b.first && b.first != c.first){
            //if (res.first == 1152) cout << "DRUGI\n";
            res.second = (b.second+a.second) * cnt;
        }

        if (a.first != b.first && b.first == c.first){
            //if (res.first == 1152) cout << "TRECI\n";
            for (pi t : dp[x])
                if (t.first == c.first)
                    res.second += t.second * (cnt - t.second);
        }

        if (a.first != b.first && b.first != c.first){
            //if (res.first == 1152) cout << "CETRI\n";
            res.second = b.second * cnt;
            res.second *= 2;
        }

        res.second /= 2;
        //if (res.first == 2)
        //    cout <<res.second << '\n';
        //cout << res.second << '\n';
        update_pi(sol, res);
    }

    for (auto& i : e[x]){
        if (i != p[x])
            solve(i);
    }
}

signed main()
{
    ios_base::sync_with_stdio(false); 
    cin.tie(nullptr); 
    cout.tie(nullptr); 
    cerr.tie(nullptr);    

    int n;
    cin >> n;

    for (int i = 1; i < n; ++i){
        int u, v;
        cin >> u >> v;

        e[u].push_back(v);
        e[v].push_back(u);
    }

    dfs(1, 1);
    calc(1);
    solve(1);

    cout << sol.first << ' ' << sol.second << '\n';
}
/*
7
1 2
1 3
2 4
2 5
3 6
3 7
*/

Compilation message

road.cpp: In function 'void calc(ll)':
road.cpp:56:23: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |     for (int i = 0; i < dp[x].size(); ++i){
      |                     ~~^~~~~~~~~~~~~~
road.cpp:61:23: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |     for (int i = 0; i < dp[x].size(); ++i){
      |                     ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 21 ms 35564 KB Output is correct
2 Correct 20 ms 35564 KB Output is correct
3 Correct 20 ms 35564 KB Output is correct
4 Correct 21 ms 35564 KB Output is correct
5 Correct 21 ms 35564 KB Output is correct
6 Correct 21 ms 35564 KB Output is correct
7 Correct 20 ms 35564 KB Output is correct
8 Correct 20 ms 35564 KB Output is correct
9 Correct 21 ms 35564 KB Output is correct
10 Correct 21 ms 35692 KB Output is correct
11 Correct 20 ms 35564 KB Output is correct
12 Correct 20 ms 35564 KB Output is correct
13 Correct 21 ms 35564 KB Output is correct
14 Correct 21 ms 35564 KB Output is correct
15 Correct 20 ms 35564 KB Output is correct
16 Correct 20 ms 35564 KB Output is correct
17 Correct 20 ms 35564 KB Output is correct
18 Correct 21 ms 35564 KB Output is correct
19 Correct 20 ms 35692 KB Output is correct
20 Correct 20 ms 35564 KB Output is correct
21 Correct 22 ms 35564 KB Output is correct
22 Correct 21 ms 35564 KB Output is correct
23 Correct 23 ms 35564 KB Output is correct
24 Correct 20 ms 35564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 35564 KB Output is correct
2 Correct 20 ms 35564 KB Output is correct
3 Correct 20 ms 35564 KB Output is correct
4 Correct 21 ms 35564 KB Output is correct
5 Correct 21 ms 35564 KB Output is correct
6 Correct 21 ms 35564 KB Output is correct
7 Correct 20 ms 35564 KB Output is correct
8 Correct 20 ms 35564 KB Output is correct
9 Correct 21 ms 35564 KB Output is correct
10 Correct 21 ms 35692 KB Output is correct
11 Correct 20 ms 35564 KB Output is correct
12 Correct 20 ms 35564 KB Output is correct
13 Correct 21 ms 35564 KB Output is correct
14 Correct 21 ms 35564 KB Output is correct
15 Correct 20 ms 35564 KB Output is correct
16 Correct 20 ms 35564 KB Output is correct
17 Correct 20 ms 35564 KB Output is correct
18 Correct 21 ms 35564 KB Output is correct
19 Correct 20 ms 35692 KB Output is correct
20 Correct 20 ms 35564 KB Output is correct
21 Correct 22 ms 35564 KB Output is correct
22 Correct 21 ms 35564 KB Output is correct
23 Correct 23 ms 35564 KB Output is correct
24 Correct 20 ms 35564 KB Output is correct
25 Correct 22 ms 36204 KB Output is correct
26 Correct 23 ms 36352 KB Output is correct
27 Correct 24 ms 36332 KB Output is correct
28 Correct 23 ms 36332 KB Output is correct
29 Correct 23 ms 36460 KB Output is correct
30 Correct 23 ms 36460 KB Output is correct
31 Correct 28 ms 36460 KB Output is correct
32 Correct 23 ms 36460 KB Output is correct
33 Correct 23 ms 36204 KB Output is correct
34 Correct 23 ms 36332 KB Output is correct
35 Correct 23 ms 36332 KB Output is correct
36 Correct 23 ms 36204 KB Output is correct
37 Correct 23 ms 36332 KB Output is correct
38 Correct 23 ms 36652 KB Output is correct
39 Correct 25 ms 36204 KB Output is correct
40 Correct 23 ms 36076 KB Output is correct
41 Correct 23 ms 35948 KB Output is correct
42 Correct 23 ms 35948 KB Output is correct
43 Correct 24 ms 35948 KB Output is correct
44 Correct 23 ms 35948 KB Output is correct
45 Correct 24 ms 35948 KB Output is correct
46 Correct 22 ms 36076 KB Output is correct
47 Correct 23 ms 35968 KB Output is correct
48 Correct 23 ms 36076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 35564 KB Output is correct
2 Correct 20 ms 35564 KB Output is correct
3 Correct 20 ms 35564 KB Output is correct
4 Correct 21 ms 35564 KB Output is correct
5 Correct 21 ms 35564 KB Output is correct
6 Correct 21 ms 35564 KB Output is correct
7 Correct 20 ms 35564 KB Output is correct
8 Correct 20 ms 35564 KB Output is correct
9 Correct 21 ms 35564 KB Output is correct
10 Correct 21 ms 35692 KB Output is correct
11 Correct 20 ms 35564 KB Output is correct
12 Correct 20 ms 35564 KB Output is correct
13 Correct 21 ms 35564 KB Output is correct
14 Correct 21 ms 35564 KB Output is correct
15 Correct 20 ms 35564 KB Output is correct
16 Correct 20 ms 35564 KB Output is correct
17 Correct 20 ms 35564 KB Output is correct
18 Correct 21 ms 35564 KB Output is correct
19 Correct 20 ms 35692 KB Output is correct
20 Correct 20 ms 35564 KB Output is correct
21 Correct 22 ms 35564 KB Output is correct
22 Correct 21 ms 35564 KB Output is correct
23 Correct 23 ms 35564 KB Output is correct
24 Correct 20 ms 35564 KB Output is correct
25 Correct 22 ms 36204 KB Output is correct
26 Correct 23 ms 36352 KB Output is correct
27 Correct 24 ms 36332 KB Output is correct
28 Correct 23 ms 36332 KB Output is correct
29 Correct 23 ms 36460 KB Output is correct
30 Correct 23 ms 36460 KB Output is correct
31 Correct 28 ms 36460 KB Output is correct
32 Correct 23 ms 36460 KB Output is correct
33 Correct 23 ms 36204 KB Output is correct
34 Correct 23 ms 36332 KB Output is correct
35 Correct 23 ms 36332 KB Output is correct
36 Correct 23 ms 36204 KB Output is correct
37 Correct 23 ms 36332 KB Output is correct
38 Correct 23 ms 36652 KB Output is correct
39 Correct 25 ms 36204 KB Output is correct
40 Correct 23 ms 36076 KB Output is correct
41 Correct 23 ms 35948 KB Output is correct
42 Correct 23 ms 35948 KB Output is correct
43 Correct 24 ms 35948 KB Output is correct
44 Correct 23 ms 35948 KB Output is correct
45 Correct 24 ms 35948 KB Output is correct
46 Correct 22 ms 36076 KB Output is correct
47 Correct 23 ms 35968 KB Output is correct
48 Correct 23 ms 36076 KB Output is correct
49 Correct 719 ms 106932 KB Output is correct
50 Correct 702 ms 111852 KB Output is correct
51 Correct 753 ms 115948 KB Output is correct
52 Correct 767 ms 100972 KB Output is correct
53 Correct 691 ms 126956 KB Output is correct
54 Correct 675 ms 134764 KB Output is correct
55 Correct 674 ms 121928 KB Output is correct
56 Correct 691 ms 127340 KB Output is correct
57 Correct 616 ms 112492 KB Output is correct
58 Correct 604 ms 106988 KB Output is correct
59 Correct 611 ms 106996 KB Output is correct
60 Correct 610 ms 104324 KB Output is correct
61 Correct 933 ms 144200 KB Output is correct
62 Correct 916 ms 131564 KB Output is correct
63 Correct 905 ms 94316 KB Output is correct
64 Correct 893 ms 85612 KB Output is correct
65 Correct 887 ms 80108 KB Output is correct
66 Correct 879 ms 76996 KB Output is correct
67 Correct 870 ms 75336 KB Output is correct
68 Correct 869 ms 74732 KB Output is correct
69 Correct 869 ms 74476 KB Output is correct
70 Correct 871 ms 74092 KB Output is correct
71 Correct 864 ms 74092 KB Output is correct
72 Correct 910 ms 74220 KB Output is correct
73 Correct 857 ms 74348 KB Output is correct
74 Correct 856 ms 74596 KB Output is correct
75 Correct 846 ms 75268 KB Output is correct
76 Correct 805 ms 76520 KB Output is correct
77 Correct 695 ms 79704 KB Output is correct
78 Correct 492 ms 85964 KB Output is correct