Submission #495525

# Submission time Handle Problem Language Result Execution time Memory
495525 2021-12-19T09:18:04 Z Karliver Hard route (IZhO17_road) C++17
100 / 100
861 ms 72108 KB
    
#include <bits/stdc++.h>

#define FIXED_FLOAT(x)  std::fixed <<std::setprecision(20) << (x)
#define all(v) (v).begin(), (v).end()
using namespace  std;
#define forn(i,n) for (int i = 0; i < (n); ++i)
#define rforn(i, n) for(int i = (n) - 1;i >= 0;--i)
#define sz(x) (int)x.size()
#define ff first
#define se second
#define mp make_pair
#define pb push_back
using ll = long long;
int mod = (ll)1e9 + 7;
const int INF = 1e9 + 1;
const int N = 5e5 + 100;
const double eps = 1e-7;

template <class T> using V = vector<T>;  
template <class T> using VV = V<V<T>>;  
template<class T, size_t SZ> using AR = array<T, SZ>;
template<class T> using PR = pair<T, T>;
template <typename XPAX>
bool ckma(XPAX &x, XPAX y) {
    return (x < y ? x = y, 1 : 0);
}
template <typename XPAX>
bool ckmi(XPAX &x, XPAX y) {
    return (x > y ? x = y, 1 : 0);
}

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
using pi = pair<int, int>;
using vpi = V<pi>;

int n, dep[N], pre[N], bdis[N];
PR<ll> ret{-1,0};
V<int> g[N];
void ck(PR<ll> x) {
    if(x.ff > ret.ff)ret = {x.ff, 0};
    if(x.ff==ret.ff)ret.se += x.se;
}

void dfs(int v) {
    for(auto c : g[v]) if(c != pre[v]) {
        dep[c] = dep[v]+1;
        pre[c]= v;
        dfs(c);
    }
}

void genDist(int v) {
    memset(dep, 0, sizeof(dep));
    pre[v]=-1;
    dfs(v);
}

int diam() {
    genDist(1);
    int w = 0;
    for(int i = 1;i <= n;++i) if(dep[w] < dep[i]) w = i;
    genDist(w);
    for(int i = 1;i <= n;++i) if(dep[w] < dep[i]) w = i;
    return dep[w];
}
V<int> C;
int t;


pi dfs(int a, int b, int c) {
    vpi v; 
    pi z = {0,0};
    for (int i: g[a]) if (i != b) {
        pi t = dfs(i,a,c+1); t.ff ++;
        v.pb(t);
        if (t.ff > z.ff) z.se = z.ff, z.ff = t.ff;
        else if (t.ff > z.se) z.se = t.ff;
    }
    int co = 0;
    for (int i: g[a]) if (i != b) {
        if (v[co].ff == z.ff) bdis[i] = z.se;
        else bdis[i] = z.ff;
        co ++;
    }
    if (sz(v) == 0) v.pb({0,1});
    if (sz(v) == 1) return v[0];
    sort(v.rbegin(),v.rend());
    int ind = 0, sum = v[0].se; 
    while (ind+1 < sz(v) && v[ind+1].ff == v[ind].ff) 
        sum += v[++ind].se;
    if (ind > 0) {
        ll t = (ll)sum*(sum-1)/2;
        forn(i,ind+1) t -= (ll)v[i].se*(v[i].se-1)/2;
        ck({(ll)v[0].ff*2*c,t});
    } else {
        ll sum2 = 0;
        int ind = 1;
        while (ind < sz(v) && v[ind].ff == v[1].ff) sum2 += v[ind++].se;
        ck({(ll)(v[0].ff+v[1].ff)*c,(ll)sum2*sum});
    }
    return {v[0].ff,sum};
}
 
V<int> genCenter() {
    t = diam();
    int w = 0;
    for(int i = 1;i <= n;++i) if(dep[w] < dep[i])w = i;

    forn(i,t/2)w = pre[w];
    if(t & 1) return {w, pre[w]};
    else return {w};
}
V<PR<int>> tmp;
void goNoCenter() {
    if(sz(C) == 2) {
        tmp.push_back(dfs(C[0],C[1],t/2+1));
        tmp.push_back(dfs(C[1], C[0], t/2+1));
    }
    else {
        for(auto c : g[C[0]]) tmp.push_back(dfs(c,C[0],t/2+1));
    }
}

V<PR<int>> z[2];

void dfs2(int v, int p, int cdis, int cmax, int i) {
    bool isleaf = 1;
    for(auto c : g[v]) {
        if(c != p) {
            isleaf=0;
            dfs2(c, v, cdis+1, max(cmax, bdis[c]),i);
        }
    }

    if(isleaf) z[i].push_back({cmax, cdis});
}

int S[2][500001];

void goCenter() {
    V<int> w;

    ll sm = 0, bad = 0;
    int mx = 0;
    if(sz(C)==2) {
        w = C;
    }
    else {
        forn(i, sz(tmp)) if(tmp[i].ff == t / 2 -1) {
            w.push_back(g[C[0]][i]);
            sm +=  tmp[i].se;
            bad += (ll)tmp[i].se * (tmp[i].se-1)/2;
        }else {
            ckma(mx, tmp[i].ff+1);
        }
    }
    //debug(w);
    if(sz(w)>2) {
        ck({1ll*t*t/2, sm*(sm-1)/2-bad});
        return;
    }

    if(sz(C)==2) {
        dfs2(C[0], C[1], 0, 0,0);
        dfs2(C[1], C[0], 1,0,1);
    }
    else {
        dfs2(w[0],C[0],1,mx,0);
        dfs2(w[1],C[0],1,mx,1);
    }
    sort(all(z[0]));sort(all(z[1]));
    //debug(ret);
    forn(k,2) for(auto c : z[k]) S[k][c.se]++;
    AR<int, 2> t = {500000, 500000};
    //debug(z[0], z[1]);
    while(sz(z[0]) && sz(z[1])) {
        forn(k,2) while(!S[k][t[k]])t[k]--;
        if(z[0].back().ff < z[1].back().ff) {
            ck({1ll*(t[0]+z[1].back().se)*z[1].back().ff, S[0][t[0]]});
            S[1][z[1].back().se]--;z[1].pop_back();
        }
        else {
            //debug(S[1][t[1]]);
            ck({1ll*(t[1]+z[0].back().se)*z[0].back().ff, S[1][t[1]]});
            S[0][z[0].back().se]--;z[0].pop_back();
        }
    }  
    //debug(ret);


}
            


void solve() {


    cin >> n;

    forn(i, n-1) {
        int a, b;
        cin >> a >> b;
        
        g[a].push_back(b);
        g[b].push_back(a);
    }
    C = genCenter();
    goNoCenter();
    //debug(ret, t);
    //debug(C);
    goCenter();
    //debug(tmp);
    cout << ret.ff << ' ' << ret.se << '\n';




}
void test_case() {
    int t;
    cin >> t;
    forn(p, t) {

        //cout << "Case #" << p + 1 << ": ";
        solve();
    }
}
int main() {

    ios::sync_with_stdio(false);
    cin.tie(0);

    solve();

}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14028 KB Output is correct
2 Correct 8 ms 14028 KB Output is correct
3 Correct 9 ms 14048 KB Output is correct
4 Correct 8 ms 14028 KB Output is correct
5 Correct 9 ms 14028 KB Output is correct
6 Correct 8 ms 14028 KB Output is correct
7 Correct 8 ms 14028 KB Output is correct
8 Correct 8 ms 13992 KB Output is correct
9 Correct 8 ms 14032 KB Output is correct
10 Correct 10 ms 13988 KB Output is correct
11 Correct 8 ms 14000 KB Output is correct
12 Correct 8 ms 14032 KB Output is correct
13 Correct 10 ms 14032 KB Output is correct
14 Correct 10 ms 14032 KB Output is correct
15 Correct 10 ms 14032 KB Output is correct
16 Correct 9 ms 13996 KB Output is correct
17 Correct 8 ms 14032 KB Output is correct
18 Correct 9 ms 14032 KB Output is correct
19 Correct 10 ms 14028 KB Output is correct
20 Correct 9 ms 14032 KB Output is correct
21 Correct 8 ms 13996 KB Output is correct
22 Correct 7 ms 14032 KB Output is correct
23 Correct 8 ms 13996 KB Output is correct
24 Correct 7 ms 14000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14028 KB Output is correct
2 Correct 8 ms 14028 KB Output is correct
3 Correct 9 ms 14048 KB Output is correct
4 Correct 8 ms 14028 KB Output is correct
5 Correct 9 ms 14028 KB Output is correct
6 Correct 8 ms 14028 KB Output is correct
7 Correct 8 ms 14028 KB Output is correct
8 Correct 8 ms 13992 KB Output is correct
9 Correct 8 ms 14032 KB Output is correct
10 Correct 10 ms 13988 KB Output is correct
11 Correct 8 ms 14000 KB Output is correct
12 Correct 8 ms 14032 KB Output is correct
13 Correct 10 ms 14032 KB Output is correct
14 Correct 10 ms 14032 KB Output is correct
15 Correct 10 ms 14032 KB Output is correct
16 Correct 9 ms 13996 KB Output is correct
17 Correct 8 ms 14032 KB Output is correct
18 Correct 9 ms 14032 KB Output is correct
19 Correct 10 ms 14028 KB Output is correct
20 Correct 9 ms 14032 KB Output is correct
21 Correct 8 ms 13996 KB Output is correct
22 Correct 7 ms 14032 KB Output is correct
23 Correct 8 ms 13996 KB Output is correct
24 Correct 7 ms 14000 KB Output is correct
25 Correct 10 ms 14416 KB Output is correct
26 Correct 10 ms 14416 KB Output is correct
27 Correct 11 ms 14492 KB Output is correct
28 Correct 11 ms 14420 KB Output is correct
29 Correct 10 ms 14400 KB Output is correct
30 Correct 10 ms 14408 KB Output is correct
31 Correct 10 ms 14452 KB Output is correct
32 Correct 11 ms 14512 KB Output is correct
33 Correct 15 ms 14416 KB Output is correct
34 Correct 10 ms 14532 KB Output is correct
35 Correct 10 ms 14520 KB Output is correct
36 Correct 11 ms 14416 KB Output is correct
37 Correct 11 ms 14604 KB Output is correct
38 Correct 11 ms 14528 KB Output is correct
39 Correct 9 ms 14304 KB Output is correct
40 Correct 9 ms 14288 KB Output is correct
41 Correct 11 ms 14292 KB Output is correct
42 Correct 10 ms 14272 KB Output is correct
43 Correct 11 ms 14268 KB Output is correct
44 Correct 10 ms 14264 KB Output is correct
45 Correct 10 ms 14264 KB Output is correct
46 Correct 9 ms 14160 KB Output is correct
47 Correct 11 ms 14440 KB Output is correct
48 Correct 9 ms 14288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14028 KB Output is correct
2 Correct 8 ms 14028 KB Output is correct
3 Correct 9 ms 14048 KB Output is correct
4 Correct 8 ms 14028 KB Output is correct
5 Correct 9 ms 14028 KB Output is correct
6 Correct 8 ms 14028 KB Output is correct
7 Correct 8 ms 14028 KB Output is correct
8 Correct 8 ms 13992 KB Output is correct
9 Correct 8 ms 14032 KB Output is correct
10 Correct 10 ms 13988 KB Output is correct
11 Correct 8 ms 14000 KB Output is correct
12 Correct 8 ms 14032 KB Output is correct
13 Correct 10 ms 14032 KB Output is correct
14 Correct 10 ms 14032 KB Output is correct
15 Correct 10 ms 14032 KB Output is correct
16 Correct 9 ms 13996 KB Output is correct
17 Correct 8 ms 14032 KB Output is correct
18 Correct 9 ms 14032 KB Output is correct
19 Correct 10 ms 14028 KB Output is correct
20 Correct 9 ms 14032 KB Output is correct
21 Correct 8 ms 13996 KB Output is correct
22 Correct 7 ms 14032 KB Output is correct
23 Correct 8 ms 13996 KB Output is correct
24 Correct 7 ms 14000 KB Output is correct
25 Correct 10 ms 14416 KB Output is correct
26 Correct 10 ms 14416 KB Output is correct
27 Correct 11 ms 14492 KB Output is correct
28 Correct 11 ms 14420 KB Output is correct
29 Correct 10 ms 14400 KB Output is correct
30 Correct 10 ms 14408 KB Output is correct
31 Correct 10 ms 14452 KB Output is correct
32 Correct 11 ms 14512 KB Output is correct
33 Correct 15 ms 14416 KB Output is correct
34 Correct 10 ms 14532 KB Output is correct
35 Correct 10 ms 14520 KB Output is correct
36 Correct 11 ms 14416 KB Output is correct
37 Correct 11 ms 14604 KB Output is correct
38 Correct 11 ms 14528 KB Output is correct
39 Correct 9 ms 14304 KB Output is correct
40 Correct 9 ms 14288 KB Output is correct
41 Correct 11 ms 14292 KB Output is correct
42 Correct 10 ms 14272 KB Output is correct
43 Correct 11 ms 14268 KB Output is correct
44 Correct 10 ms 14264 KB Output is correct
45 Correct 10 ms 14264 KB Output is correct
46 Correct 9 ms 14160 KB Output is correct
47 Correct 11 ms 14440 KB Output is correct
48 Correct 9 ms 14288 KB Output is correct
49 Correct 581 ms 57816 KB Output is correct
50 Correct 577 ms 57776 KB Output is correct
51 Correct 607 ms 57772 KB Output is correct
52 Correct 541 ms 57724 KB Output is correct
53 Correct 446 ms 61172 KB Output is correct
54 Correct 481 ms 61256 KB Output is correct
55 Correct 435 ms 61168 KB Output is correct
56 Correct 439 ms 61400 KB Output is correct
57 Correct 493 ms 64200 KB Output is correct
58 Correct 484 ms 64180 KB Output is correct
59 Correct 518 ms 64308 KB Output is correct
60 Correct 514 ms 64180 KB Output is correct
61 Correct 861 ms 72056 KB Output is correct
62 Correct 853 ms 72108 KB Output is correct
63 Correct 746 ms 53268 KB Output is correct
64 Correct 724 ms 46932 KB Output is correct
65 Correct 700 ms 43912 KB Output is correct
66 Correct 664 ms 41940 KB Output is correct
67 Correct 667 ms 41276 KB Output is correct
68 Correct 704 ms 40976 KB Output is correct
69 Correct 660 ms 40896 KB Output is correct
70 Correct 651 ms 40828 KB Output is correct
71 Correct 637 ms 40580 KB Output is correct
72 Correct 635 ms 40700 KB Output is correct
73 Correct 663 ms 40844 KB Output is correct
74 Correct 648 ms 41096 KB Output is correct
75 Correct 647 ms 41684 KB Output is correct
76 Correct 580 ms 42728 KB Output is correct
77 Correct 484 ms 45580 KB Output is correct
78 Correct 279 ms 48664 KB Output is correct