Submission #962336

# Submission time Handle Problem Language Result Execution time Memory
962336 2024-04-13T10:47:04 Z Cookie Hard route (IZhO17_road) C++14
100 / 100
555 ms 105284 KB
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359, prec = 1e-9;;
//using u128 = __uint128_t;
//const int x[4] = {1, 0, -1, 0};
//const int y[4] = {0, -1, 0, 1};
const ll mod = 1e9 + 7, pr = 31;
const int mxn = 5e5 + 5, mxq = 1e5 + 5, sq = 450, mxv = 5e4 + 1;
//const int base = (1 <<18);
const ll inf = 1e9 + 5, neg = -69420, inf2 = 1e14;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// have fun!
struct DP{
    ll mx, cnt;
    DP(){
        mx = 0; cnt = 1;
    }
    DP(ll _mx, ll _cnt){
        mx = _mx; cnt = _cnt;
    }
};
DP comb(DP a, DP b){
    if(a.mx > b.mx)return(a);
    if(a.mx < b.mx)return(b);
    return(DP(a.mx, a.cnt + b.cnt));
}
int n;
DP dp[mxn + 1];
vt<int>adj[mxn + 1];
void dfs(int s, int pre){
    for(auto i: adj[s]){
        if(i != pre){
            dfs(i, s);
            dp[s] = comb(dp[s], DP(dp[i].mx + 1, dp[i].cnt));
        }
    }
    
}
DP res;
bool cmp(int a, int b){
    return(dp[a].mx > dp[b].mx);
}
void dfs2(int s, int pre){
    sort(ALL(adj[s]), cmp);
    if(sz(adj[s]) >= 3){
        ll mx0 = dp[adj[s][0]].mx + 1, mx1 = dp[adj[s][1]].mx + 1, mx2 = dp[adj[s][2]].mx + 1;
        ll pref = 0;
        DP cand = DP(mx0 * (mx1 + mx2), 0);
        for(auto i: adj[s]){
            if(dp[i].mx + 1 == mx2)cand.cnt += pref * dp[i].cnt;
            if(dp[i].mx + 1 == mx1)pref += dp[i].cnt;
        }
        res = comb(res, cand);
    }
    DP fir, sec = DP(0, 0);
    for(auto i: adj[s]){
        if(dp[i].mx + 1 > fir.mx){
            sec = fir;
            fir = DP(dp[i].mx + 1, dp[i].cnt);
        }
        else if(dp[i].mx + 1 == fir.mx)fir.cnt += dp[i].cnt;
        else if(dp[i].mx + 1 > sec.mx)sec = DP(dp[i].mx + 1, dp[i].cnt);
        else if(dp[i].mx + 1 == sec.mx)sec.cnt += dp[i].cnt;
    }
    DP rem = dp[s];
    for(auto i: adj[s]){
        if(i != pre){
            dp[s] = fir;
            if(dp[s].mx == dp[i].mx + 1){
                if(dp[s].cnt == dp[i].cnt){
                    dp[s] = sec;
                }else{
                    dp[s].cnt -= dp[i].cnt;
                }
            }
            dfs2(i, s);
        }
    }
    dp[s] = rem;
}
void solve(){
    cin >> n;
    for(int i = 1; i < n; i++){
        int u, v; cin >> u >> v;
        adj[u].pb(v); adj[v].pb(u); 
    }
    dfs(1, -1); dfs2(1, -1);
    cout << res.mx << " " << res.cnt;
}
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen("netw.inp", "r", stdin);
    //freopen("netw.out", "w", stdout)
    int tt; tt = 1;
    while(tt--){
        solve();
 
    }
    return(0);
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 19804 KB Output is correct
2 Correct 4 ms 19804 KB Output is correct
3 Correct 4 ms 19804 KB Output is correct
4 Correct 5 ms 19804 KB Output is correct
5 Correct 4 ms 19804 KB Output is correct
6 Correct 4 ms 19804 KB Output is correct
7 Correct 4 ms 19804 KB Output is correct
8 Correct 4 ms 19804 KB Output is correct
9 Correct 4 ms 19804 KB Output is correct
10 Correct 4 ms 19804 KB Output is correct
11 Correct 5 ms 19804 KB Output is correct
12 Correct 5 ms 19804 KB Output is correct
13 Correct 5 ms 19800 KB Output is correct
14 Correct 4 ms 19804 KB Output is correct
15 Correct 5 ms 19804 KB Output is correct
16 Correct 4 ms 19804 KB Output is correct
17 Correct 5 ms 19800 KB Output is correct
18 Correct 5 ms 19808 KB Output is correct
19 Correct 4 ms 19804 KB Output is correct
20 Correct 5 ms 19804 KB Output is correct
21 Correct 5 ms 19800 KB Output is correct
22 Correct 4 ms 19804 KB Output is correct
23 Correct 5 ms 19804 KB Output is correct
24 Correct 4 ms 19804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 19804 KB Output is correct
2 Correct 4 ms 19804 KB Output is correct
3 Correct 4 ms 19804 KB Output is correct
4 Correct 5 ms 19804 KB Output is correct
5 Correct 4 ms 19804 KB Output is correct
6 Correct 4 ms 19804 KB Output is correct
7 Correct 4 ms 19804 KB Output is correct
8 Correct 4 ms 19804 KB Output is correct
9 Correct 4 ms 19804 KB Output is correct
10 Correct 4 ms 19804 KB Output is correct
11 Correct 5 ms 19804 KB Output is correct
12 Correct 5 ms 19804 KB Output is correct
13 Correct 5 ms 19800 KB Output is correct
14 Correct 4 ms 19804 KB Output is correct
15 Correct 5 ms 19804 KB Output is correct
16 Correct 4 ms 19804 KB Output is correct
17 Correct 5 ms 19800 KB Output is correct
18 Correct 5 ms 19808 KB Output is correct
19 Correct 4 ms 19804 KB Output is correct
20 Correct 5 ms 19804 KB Output is correct
21 Correct 5 ms 19800 KB Output is correct
22 Correct 4 ms 19804 KB Output is correct
23 Correct 5 ms 19804 KB Output is correct
24 Correct 4 ms 19804 KB Output is correct
25 Correct 5 ms 20316 KB Output is correct
26 Correct 6 ms 20312 KB Output is correct
27 Correct 5 ms 20316 KB Output is correct
28 Correct 5 ms 20316 KB Output is correct
29 Correct 6 ms 20316 KB Output is correct
30 Correct 5 ms 20316 KB Output is correct
31 Correct 6 ms 20304 KB Output is correct
32 Correct 6 ms 20316 KB Output is correct
33 Correct 6 ms 20492 KB Output is correct
34 Correct 6 ms 20316 KB Output is correct
35 Correct 6 ms 20316 KB Output is correct
36 Correct 7 ms 20312 KB Output is correct
37 Correct 6 ms 20572 KB Output is correct
38 Correct 6 ms 20828 KB Output is correct
39 Correct 6 ms 20572 KB Output is correct
40 Correct 6 ms 20316 KB Output is correct
41 Correct 5 ms 20060 KB Output is correct
42 Correct 6 ms 20060 KB Output is correct
43 Correct 6 ms 20060 KB Output is correct
44 Correct 5 ms 20060 KB Output is correct
45 Correct 5 ms 20060 KB Output is correct
46 Correct 6 ms 20060 KB Output is correct
47 Correct 7 ms 20152 KB Output is correct
48 Correct 6 ms 20040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 19804 KB Output is correct
2 Correct 4 ms 19804 KB Output is correct
3 Correct 4 ms 19804 KB Output is correct
4 Correct 5 ms 19804 KB Output is correct
5 Correct 4 ms 19804 KB Output is correct
6 Correct 4 ms 19804 KB Output is correct
7 Correct 4 ms 19804 KB Output is correct
8 Correct 4 ms 19804 KB Output is correct
9 Correct 4 ms 19804 KB Output is correct
10 Correct 4 ms 19804 KB Output is correct
11 Correct 5 ms 19804 KB Output is correct
12 Correct 5 ms 19804 KB Output is correct
13 Correct 5 ms 19800 KB Output is correct
14 Correct 4 ms 19804 KB Output is correct
15 Correct 5 ms 19804 KB Output is correct
16 Correct 4 ms 19804 KB Output is correct
17 Correct 5 ms 19800 KB Output is correct
18 Correct 5 ms 19808 KB Output is correct
19 Correct 4 ms 19804 KB Output is correct
20 Correct 5 ms 19804 KB Output is correct
21 Correct 5 ms 19800 KB Output is correct
22 Correct 4 ms 19804 KB Output is correct
23 Correct 5 ms 19804 KB Output is correct
24 Correct 4 ms 19804 KB Output is correct
25 Correct 5 ms 20316 KB Output is correct
26 Correct 6 ms 20312 KB Output is correct
27 Correct 5 ms 20316 KB Output is correct
28 Correct 5 ms 20316 KB Output is correct
29 Correct 6 ms 20316 KB Output is correct
30 Correct 5 ms 20316 KB Output is correct
31 Correct 6 ms 20304 KB Output is correct
32 Correct 6 ms 20316 KB Output is correct
33 Correct 6 ms 20492 KB Output is correct
34 Correct 6 ms 20316 KB Output is correct
35 Correct 6 ms 20316 KB Output is correct
36 Correct 7 ms 20312 KB Output is correct
37 Correct 6 ms 20572 KB Output is correct
38 Correct 6 ms 20828 KB Output is correct
39 Correct 6 ms 20572 KB Output is correct
40 Correct 6 ms 20316 KB Output is correct
41 Correct 5 ms 20060 KB Output is correct
42 Correct 6 ms 20060 KB Output is correct
43 Correct 6 ms 20060 KB Output is correct
44 Correct 5 ms 20060 KB Output is correct
45 Correct 5 ms 20060 KB Output is correct
46 Correct 6 ms 20060 KB Output is correct
47 Correct 7 ms 20152 KB Output is correct
48 Correct 6 ms 20040 KB Output is correct
49 Correct 361 ms 66264 KB Output is correct
50 Correct 371 ms 70468 KB Output is correct
51 Correct 333 ms 74216 KB Output is correct
52 Correct 362 ms 61020 KB Output is correct
53 Correct 277 ms 68164 KB Output is correct
54 Correct 302 ms 72408 KB Output is correct
55 Correct 291 ms 62176 KB Output is correct
56 Correct 294 ms 66388 KB Output is correct
57 Correct 335 ms 76780 KB Output is correct
58 Correct 290 ms 71948 KB Output is correct
59 Correct 324 ms 71764 KB Output is correct
60 Correct 314 ms 69692 KB Output is correct
61 Correct 555 ms 105284 KB Output is correct
62 Correct 502 ms 93772 KB Output is correct
63 Correct 534 ms 60712 KB Output is correct
64 Correct 509 ms 52788 KB Output is correct
65 Correct 519 ms 47820 KB Output is correct
66 Correct 484 ms 45068 KB Output is correct
67 Correct 512 ms 43732 KB Output is correct
68 Correct 519 ms 43172 KB Output is correct
69 Correct 503 ms 43092 KB Output is correct
70 Correct 484 ms 42992 KB Output is correct
71 Correct 465 ms 42572 KB Output is correct
72 Correct 465 ms 42716 KB Output is correct
73 Correct 546 ms 42580 KB Output is correct
74 Correct 474 ms 42836 KB Output is correct
75 Correct 469 ms 42944 KB Output is correct
76 Correct 422 ms 42884 KB Output is correct
77 Correct 371 ms 43460 KB Output is correct
78 Correct 253 ms 44708 KB Output is correct