답안 #495519

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
495519 2021-12-19T08:50:02 Z Karliver Hard route (IZhO17_road) C++17
0 / 100
5 ms 5804 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
using ll = long long;
int mod = (ll)1e9 + 7;
const int INF = 1e9 + 1;
const int N = 2e5 + 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)


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;
    else 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;


PR<int> dfs(int a, int b, int c) {
    V<PR<int>> v;
    PR<int> z{0,0};

    for(auto i : g[a]) if(i!=b) {
        PR<int> t = dfs(i, a, c+1);
        t.ff++;
        v.push_back(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 o = 0;
    for(auto i : g[a]) if(i != b) {
        if(v[o].ff==z.ff)bdis[i]=z.se;
        else bdis[i]=z.ff;
        o++;
    }
    if(sz(v)==0)v.push_back({0,1});
    if(sz(v)==1)return v[0];
    sort(v.rbegin(), v.rend());

    int id = 0, sm =v[0].se;
    while(id+1 < sz(v) && v[id+1].ff==v[id].ff) 
        sm += v[++id].se;

    if(id > 0) {
        ll t = sm * 1ll * (sm - 1) / 2;
        forn(i,id+1)sm -= v[i].se *1ll * (v[i].se - 1) / 2;
        ck({1ll*v[0].ff*2*c, t});
    }
    else {
        id = 1;
        ll sm2 = 0;
        while(id < sz(v) && v[id].ff==v[1].ff) sm2 += v[id++].se;
        ck({1ll*(v[0].ff+v[1].ff), sm2*sm});
    }
    return {v[0].ff, sm};
}

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[v]),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();

}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5804 KB Output is correct
2 Correct 3 ms 5792 KB Output is correct
3 Correct 5 ms 5708 KB Output is correct
4 Correct 4 ms 5804 KB Output is correct
5 Correct 5 ms 5708 KB Output is correct
6 Correct 4 ms 5708 KB Output is correct
7 Incorrect 4 ms 5804 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5804 KB Output is correct
2 Correct 3 ms 5792 KB Output is correct
3 Correct 5 ms 5708 KB Output is correct
4 Correct 4 ms 5804 KB Output is correct
5 Correct 5 ms 5708 KB Output is correct
6 Correct 4 ms 5708 KB Output is correct
7 Incorrect 4 ms 5804 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5804 KB Output is correct
2 Correct 3 ms 5792 KB Output is correct
3 Correct 5 ms 5708 KB Output is correct
4 Correct 4 ms 5804 KB Output is correct
5 Correct 5 ms 5708 KB Output is correct
6 Correct 4 ms 5708 KB Output is correct
7 Incorrect 4 ms 5804 KB Output isn't correct
8 Halted 0 ms 0 KB -