답안 #984993

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
984993 2024-05-17T09:06:43 Z a_l_i_r_e_z_a Hard route (IZhO17_road) C++17
100 / 100
725 ms 114232 KB
// In the name of God
// Hope is last to die

#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC optimize("avx2")

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define pb push_back
// #define int long long
#define S second
#define F first
#define mp make_pair
#define smax(xyxy, yxy) (xyxy) = max((xyxy), (yxy))
#define smin(xyxy, yxy) (xyxy) = min((xyxy), (yxy))
#define all(xyxy) (xyxy).begin(), (xyxy).end()
#define len(xyxy) ((int)(xyxy).size())

const int maxn = 5e5 + 5;
const int inf = 1e9 + 7;
ll n, cnt[maxn];
pll dpu[maxn], dpd[maxn];
pll ans = mp(0, 1);
vector<int> adj[maxn];

void smaxp(pll &x, pll y, int v){
    if(y.F + v > x.F) x = mp(y.F + v, y.S);
    else if(y.F + v == x.F) x.S += y.S;
}

void dfsd(int v = 1, int p = 0){
    dpd[v] = mp(0, 1);
    for(auto u: adj[v]) if(u != p){
        dfsd(u, v);
        smaxp(dpd[v], dpd[u], 1);
    }
}

void dfsu(int v = 1, int p = 0){
    int mx1 = -1, mx2 = -1;
    for(auto u: adj[v]) if(u != p){
        if(mx1 == -1 || dpd[u].F >= dpd[mx1].F){
            mx2 = mx1;
            mx1 = u;
        }
        else if(mx2 == -1 || dpd[u].F > dpd[mx2].F) mx2 = u;
        smaxp(dpu[u], dpu[v], 1);
    }
    for(auto u: adj[v]) if(u != p){
        if(u == mx1){
            if(mx2 != -1){
                smaxp(dpu[u], dpd[mx2], 2);
            }
        }
        else smaxp(dpu[u], dpd[mx1], 2);
        dfsu(u, v);
    }
}

void dfs(int v = 1, int p = 0){
    vector<pll> vec;
    for(auto u: adj[v]) if(u != p){
        dfs(u, v);
        vec.pb(mp(dpd[u].F + 1, dpd[u].S));
    }
    if(v > 1) vec.pb(dpu[v]);
    if(vec.size() < 3) return;
    sort(all(vec), greater<>());
    ll val = vec[1].F + vec[2].F;
    ll res = 0;
    for(int i = 1; i < vec.size(); i++){
        res += vec[i].S * cnt[val - vec[i].F];
        cnt[vec[i].F] += vec[i].S;
    }
    if(vec[2].F == vec[0].F){
        for(int i = 1; i < vec.size() && vec[i].F == vec[0].F; i++){
            res += vec[i].S * vec[0].S;
        }
    }
    else if(vec[1].F == vec[0].F){
        res += (res / vec[1].S) * vec[0].S;
    }
    smaxp(ans, mp(vec[0].F * val, res), 0);
    for(auto u: vec) cnt[u.F] = 0;
}

int32_t main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    
    cin >> n;
    for(int i = 0; i < n - 1; i++){
        int u, v; cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfsd();
    for(int i = 1; i <= n; i++) dpu[i] = mp(0, 1);
    dfsu();
    // for(int i = 1; i <= n; i++){
    //     cout << i << ' ' << dpd[i].F << ' ' << dpd[i].S << ' ' << dpu[i].F << ' ' << dpu[i].S << '\n';
    // }
    dfs();
    cout << ans.F << ' ' << ans.S << '\n';

    return 0;
}

Compilation message

road.cpp: In function 'void dfs(int, int)':
road.cpp:77:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |     for(int i = 1; i < vec.size(); i++){
      |                    ~~^~~~~~~~~~~~
road.cpp:82:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |         for(int i = 1; i < vec.size() && vec[i].F == vec[0].F; i++){
      |                        ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14936 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 3 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 15012 KB Output is correct
8 Correct 3 ms 14940 KB Output is correct
9 Correct 3 ms 14936 KB Output is correct
10 Correct 3 ms 14940 KB Output is correct
11 Correct 3 ms 14940 KB Output is correct
12 Correct 3 ms 14940 KB Output is correct
13 Correct 3 ms 14940 KB Output is correct
14 Correct 4 ms 14940 KB Output is correct
15 Correct 3 ms 15248 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
18 Correct 3 ms 14940 KB Output is correct
19 Correct 3 ms 14936 KB Output is correct
20 Correct 3 ms 14936 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 14944 KB Output is correct
24 Correct 3 ms 14956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14936 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 3 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 15012 KB Output is correct
8 Correct 3 ms 14940 KB Output is correct
9 Correct 3 ms 14936 KB Output is correct
10 Correct 3 ms 14940 KB Output is correct
11 Correct 3 ms 14940 KB Output is correct
12 Correct 3 ms 14940 KB Output is correct
13 Correct 3 ms 14940 KB Output is correct
14 Correct 4 ms 14940 KB Output is correct
15 Correct 3 ms 15248 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
18 Correct 3 ms 14940 KB Output is correct
19 Correct 3 ms 14936 KB Output is correct
20 Correct 3 ms 14936 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 14944 KB Output is correct
24 Correct 3 ms 14956 KB Output is correct
25 Correct 5 ms 15196 KB Output is correct
26 Correct 5 ms 15452 KB Output is correct
27 Correct 5 ms 15452 KB Output is correct
28 Correct 5 ms 15452 KB Output is correct
29 Correct 5 ms 15448 KB Output is correct
30 Correct 6 ms 15464 KB Output is correct
31 Correct 5 ms 15452 KB Output is correct
32 Correct 5 ms 15452 KB Output is correct
33 Correct 5 ms 15452 KB Output is correct
34 Correct 5 ms 15648 KB Output is correct
35 Correct 4 ms 15452 KB Output is correct
36 Correct 5 ms 15448 KB Output is correct
37 Correct 5 ms 15452 KB Output is correct
38 Correct 5 ms 15708 KB Output is correct
39 Correct 5 ms 15192 KB Output is correct
40 Correct 5 ms 15196 KB Output is correct
41 Correct 5 ms 15192 KB Output is correct
42 Correct 4 ms 15196 KB Output is correct
43 Correct 4 ms 15192 KB Output is correct
44 Correct 5 ms 15196 KB Output is correct
45 Correct 4 ms 15196 KB Output is correct
46 Correct 4 ms 15192 KB Output is correct
47 Correct 5 ms 15192 KB Output is correct
48 Correct 4 ms 15448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14936 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 3 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 15012 KB Output is correct
8 Correct 3 ms 14940 KB Output is correct
9 Correct 3 ms 14936 KB Output is correct
10 Correct 3 ms 14940 KB Output is correct
11 Correct 3 ms 14940 KB Output is correct
12 Correct 3 ms 14940 KB Output is correct
13 Correct 3 ms 14940 KB Output is correct
14 Correct 4 ms 14940 KB Output is correct
15 Correct 3 ms 15248 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
18 Correct 3 ms 14940 KB Output is correct
19 Correct 3 ms 14936 KB Output is correct
20 Correct 3 ms 14936 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 14944 KB Output is correct
24 Correct 3 ms 14956 KB Output is correct
25 Correct 5 ms 15196 KB Output is correct
26 Correct 5 ms 15452 KB Output is correct
27 Correct 5 ms 15452 KB Output is correct
28 Correct 5 ms 15452 KB Output is correct
29 Correct 5 ms 15448 KB Output is correct
30 Correct 6 ms 15464 KB Output is correct
31 Correct 5 ms 15452 KB Output is correct
32 Correct 5 ms 15452 KB Output is correct
33 Correct 5 ms 15452 KB Output is correct
34 Correct 5 ms 15648 KB Output is correct
35 Correct 4 ms 15452 KB Output is correct
36 Correct 5 ms 15448 KB Output is correct
37 Correct 5 ms 15452 KB Output is correct
38 Correct 5 ms 15708 KB Output is correct
39 Correct 5 ms 15192 KB Output is correct
40 Correct 5 ms 15196 KB Output is correct
41 Correct 5 ms 15192 KB Output is correct
42 Correct 4 ms 15196 KB Output is correct
43 Correct 4 ms 15192 KB Output is correct
44 Correct 5 ms 15196 KB Output is correct
45 Correct 4 ms 15196 KB Output is correct
46 Correct 4 ms 15192 KB Output is correct
47 Correct 5 ms 15192 KB Output is correct
48 Correct 4 ms 15448 KB Output is correct
49 Correct 456 ms 77532 KB Output is correct
50 Correct 479 ms 81908 KB Output is correct
51 Correct 508 ms 85864 KB Output is correct
52 Correct 431 ms 72276 KB Output is correct
53 Correct 399 ms 85860 KB Output is correct
54 Correct 391 ms 83988 KB Output is correct
55 Correct 380 ms 76336 KB Output is correct
56 Correct 364 ms 79848 KB Output is correct
57 Correct 420 ms 88392 KB Output is correct
58 Correct 406 ms 83472 KB Output is correct
59 Correct 405 ms 83284 KB Output is correct
60 Correct 381 ms 81232 KB Output is correct
61 Correct 661 ms 114232 KB Output is correct
62 Correct 699 ms 103144 KB Output is correct
63 Correct 725 ms 70180 KB Output is correct
64 Correct 676 ms 62276 KB Output is correct
65 Correct 682 ms 57028 KB Output is correct
66 Correct 645 ms 54352 KB Output is correct
67 Correct 622 ms 53072 KB Output is correct
68 Correct 622 ms 52684 KB Output is correct
69 Correct 653 ms 52584 KB Output is correct
70 Correct 625 ms 52308 KB Output is correct
71 Correct 667 ms 52308 KB Output is correct
72 Correct 598 ms 52048 KB Output is correct
73 Correct 680 ms 52448 KB Output is correct
74 Correct 662 ms 52856 KB Output is correct
75 Correct 634 ms 53708 KB Output is correct
76 Correct 619 ms 54644 KB Output is correct
77 Correct 462 ms 57148 KB Output is correct
78 Correct 271 ms 62528 KB Output is correct