답안 #58813

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
58813 2018-07-19T14:26:20 Z Benq Hard route (IZhO17_road) C++14
100 / 100
1537 ms 255532 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 500001;

int n, dist[MX], pre[MX], besDist[MX];
vi adj[MX];
pl bes = {-1,0};

void tri(pl x) {
    if (x.f > bes.f) bes = {x.f,0};
    if (x.f == bes.f) bes.s += x.s;
}

void dfs(int cur) {
    for (int i: adj[cur]) if (i != pre[cur]) {
        pre[i] = cur;
        dist[i] = dist[cur]+1;
        dfs(i);
    }
}

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

int treeDiameter() {
    genDist(1);
    int bes = 0; FOR(i,1,n+1) if (dist[i] > dist[bes]) bes = i;
    genDist(bes); FOR(i,1,n+1) if (dist[i] > dist[bes]) bes = i;
    return dist[bes];
}

int t;
vi x;

vi genCenter() {
    t = treeDiameter();
    int bes = 0; FOR(i,1,n+1) if (dist[i] > dist[bes]) bes = i;
    
    F0R(i,t/2) bes = pre[bes];
    if (t&1) return {bes,pre[bes]};
    return {bes};
}

pi dfs(int a, int b, int c) {
    vpi v; 
    pi z = {0,0};
    for (int i: adj[a]) if (i != b) {
        pi t = dfs(i,a,c+1); t.f ++;
        v.pb(t);
        if (t.f > z.f) z.s = z.f, z.f = t.f;
        else if (t.f > z.s) z.s = t.f;
    }
    int co = 0;
    for (int i: adj[a]) if (i != b) {
        if (v[co].f == z.f) besDist[i] = z.s;
        else besDist[i] = z.f;
        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].s; 
    while (ind+1 < sz(v) && v[ind+1].f == v[ind].f) 
        sum += v[++ind].s;
    if (ind > 0) {
        ll t = (ll)sum*(sum-1)/2;
        F0R(i,ind+1) t -= (ll)v[i].s*(v[i].s-1)/2;
        tri({(ll)v[0].f*2*c,t});
    } else {
        ll sum2 = 0;
        int ind = 1;
        while (ind < sz(v) && v[ind].f == v[1].f) sum2 += v[ind++].s;
        tri({(ll)(v[0].f+v[1].f)*c,(ll)sum2*sum});
    }
    return {v[0].f,sum};
}

vpi tmp;

void solveNoCenter() {
    if (sz(x) == 2) {
        tmp.pb(dfs(x[1],x[0],t/2+1));
        tmp.pb(dfs(x[0],x[1],t/2+1));
    } else {
        for (int i: adj[x[0]]) tmp.pb(dfs(i,x[0],t/2+1));    
    }
}

vpi z[2];

void dfs2(int cur, int pre, int cdist, int cmx, int ind) {
    bool isLeaf = 1;
    for (int i: adj[cur]) if (i != pre) {
        dfs2(i,cur,cdist+1,max(cmx,besDist[i]),ind);
        isLeaf = 0;
    }
    if (isLeaf) z[ind].pb({cmx,cdist});
}

int stor[2][500001];

void solveCenter() {
    vi maj;
    ll sum = 0, bad = 0;
    int mx = 0;
    if (sz(x) == 2) maj = x;
    else {
        F0R(i,sz(tmp)) if (tmp[i].f == t/2-1) {
            maj.pb(adj[x[0]][i]);
            sum += tmp[i].s;
            bad += (ll)tmp[i].s*(tmp[i].s-1)/2;
        } else {
            mx = max(mx,tmp[i].f+1);
        }
    }
    if (sz(maj) > 2) {
        tri({(ll)t*t/2,(ll)sum*(sum-1)/2-bad});
        return;
    }
    // cout << "OH " << sz(x) << " " << maj[0] << " " << maj[1] << "\n";
    //cout << sz(maj) << " " << x[0] << "\n";
    //exit(0);
    //cout << maj[0] << " " << maj[1] << " " << x[0] << " " << mx << "\n";
    if (sz(x) == 2) {
        dfs2(x[0],x[1],0,0,0);
        dfs2(x[1],x[0],1,0,1);
    } else {
        dfs2(maj[0],x[0],1,mx,0);
        dfs2(maj[1],x[0],1,mx,1);
    }
    
    sort(all(z[0])), sort(all(z[1]));
    
    F0R(k,2) for (auto a: z[k]) stor[k][a.s] ++;
    array<int,2> t = {500000,500000};
    while (sz(z[0]) && sz(z[1])) {
        F0R(k,2) while (!stor[k][t[k]]) t[k] --;
        if (z[0].back().f < z[1].back().f) {
            tri({(ll)(t[0]+z[1].back().s)*z[1].back().f,stor[0][t[0]]});
            stor[1][z[1].back().s] --; z[1].pop_back();
        } else {
            tri({(ll)(t[1]+z[0].back().s)*z[0].back().f,stor[1][t[1]]});
            stor[0][z[0].back().s] --; z[0].pop_back();
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n;
    F0R(i,n-1) {
        int a, b; cin >> a >> b;
        adj[a].pb(b), adj[b].pb(a);
    }
    x = genCenter();
    solveNoCenter();
    // cout << bes.f << " " << bes.s << "\n";
    solveCenter();
    cout << bes.f << " " << bes.s;
}

/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( ) 
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 14072 KB Output is correct
2 Correct 19 ms 14072 KB Output is correct
3 Correct 15 ms 14088 KB Output is correct
4 Correct 14 ms 14164 KB Output is correct
5 Correct 15 ms 14292 KB Output is correct
6 Correct 16 ms 14320 KB Output is correct
7 Correct 15 ms 14320 KB Output is correct
8 Correct 15 ms 14380 KB Output is correct
9 Correct 15 ms 14388 KB Output is correct
10 Correct 15 ms 14388 KB Output is correct
11 Correct 14 ms 14388 KB Output is correct
12 Correct 15 ms 14440 KB Output is correct
13 Correct 19 ms 14440 KB Output is correct
14 Correct 18 ms 14440 KB Output is correct
15 Correct 19 ms 14440 KB Output is correct
16 Correct 18 ms 14476 KB Output is correct
17 Correct 16 ms 14476 KB Output is correct
18 Correct 20 ms 14476 KB Output is correct
19 Correct 15 ms 14476 KB Output is correct
20 Correct 18 ms 14476 KB Output is correct
21 Correct 14 ms 14508 KB Output is correct
22 Correct 14 ms 14508 KB Output is correct
23 Correct 14 ms 14508 KB Output is correct
24 Correct 20 ms 14572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 14072 KB Output is correct
2 Correct 19 ms 14072 KB Output is correct
3 Correct 15 ms 14088 KB Output is correct
4 Correct 14 ms 14164 KB Output is correct
5 Correct 15 ms 14292 KB Output is correct
6 Correct 16 ms 14320 KB Output is correct
7 Correct 15 ms 14320 KB Output is correct
8 Correct 15 ms 14380 KB Output is correct
9 Correct 15 ms 14388 KB Output is correct
10 Correct 15 ms 14388 KB Output is correct
11 Correct 14 ms 14388 KB Output is correct
12 Correct 15 ms 14440 KB Output is correct
13 Correct 19 ms 14440 KB Output is correct
14 Correct 18 ms 14440 KB Output is correct
15 Correct 19 ms 14440 KB Output is correct
16 Correct 18 ms 14476 KB Output is correct
17 Correct 16 ms 14476 KB Output is correct
18 Correct 20 ms 14476 KB Output is correct
19 Correct 15 ms 14476 KB Output is correct
20 Correct 18 ms 14476 KB Output is correct
21 Correct 14 ms 14508 KB Output is correct
22 Correct 14 ms 14508 KB Output is correct
23 Correct 14 ms 14508 KB Output is correct
24 Correct 20 ms 14572 KB Output is correct
25 Correct 23 ms 14836 KB Output is correct
26 Correct 25 ms 15016 KB Output is correct
27 Correct 24 ms 15084 KB Output is correct
28 Correct 26 ms 15084 KB Output is correct
29 Correct 24 ms 15204 KB Output is correct
30 Correct 22 ms 15272 KB Output is correct
31 Correct 22 ms 15324 KB Output is correct
32 Correct 21 ms 15376 KB Output is correct
33 Correct 19 ms 15536 KB Output is correct
34 Correct 18 ms 15536 KB Output is correct
35 Correct 18 ms 15548 KB Output is correct
36 Correct 21 ms 15636 KB Output is correct
37 Correct 23 ms 15764 KB Output is correct
38 Correct 25 ms 15864 KB Output is correct
39 Correct 20 ms 15864 KB Output is correct
40 Correct 20 ms 15864 KB Output is correct
41 Correct 20 ms 15864 KB Output is correct
42 Correct 17 ms 15864 KB Output is correct
43 Correct 17 ms 15864 KB Output is correct
44 Correct 20 ms 15864 KB Output is correct
45 Correct 18 ms 15864 KB Output is correct
46 Correct 15 ms 15864 KB Output is correct
47 Correct 16 ms 15936 KB Output is correct
48 Correct 21 ms 16116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 14072 KB Output is correct
2 Correct 19 ms 14072 KB Output is correct
3 Correct 15 ms 14088 KB Output is correct
4 Correct 14 ms 14164 KB Output is correct
5 Correct 15 ms 14292 KB Output is correct
6 Correct 16 ms 14320 KB Output is correct
7 Correct 15 ms 14320 KB Output is correct
8 Correct 15 ms 14380 KB Output is correct
9 Correct 15 ms 14388 KB Output is correct
10 Correct 15 ms 14388 KB Output is correct
11 Correct 14 ms 14388 KB Output is correct
12 Correct 15 ms 14440 KB Output is correct
13 Correct 19 ms 14440 KB Output is correct
14 Correct 18 ms 14440 KB Output is correct
15 Correct 19 ms 14440 KB Output is correct
16 Correct 18 ms 14476 KB Output is correct
17 Correct 16 ms 14476 KB Output is correct
18 Correct 20 ms 14476 KB Output is correct
19 Correct 15 ms 14476 KB Output is correct
20 Correct 18 ms 14476 KB Output is correct
21 Correct 14 ms 14508 KB Output is correct
22 Correct 14 ms 14508 KB Output is correct
23 Correct 14 ms 14508 KB Output is correct
24 Correct 20 ms 14572 KB Output is correct
25 Correct 23 ms 14836 KB Output is correct
26 Correct 25 ms 15016 KB Output is correct
27 Correct 24 ms 15084 KB Output is correct
28 Correct 26 ms 15084 KB Output is correct
29 Correct 24 ms 15204 KB Output is correct
30 Correct 22 ms 15272 KB Output is correct
31 Correct 22 ms 15324 KB Output is correct
32 Correct 21 ms 15376 KB Output is correct
33 Correct 19 ms 15536 KB Output is correct
34 Correct 18 ms 15536 KB Output is correct
35 Correct 18 ms 15548 KB Output is correct
36 Correct 21 ms 15636 KB Output is correct
37 Correct 23 ms 15764 KB Output is correct
38 Correct 25 ms 15864 KB Output is correct
39 Correct 20 ms 15864 KB Output is correct
40 Correct 20 ms 15864 KB Output is correct
41 Correct 20 ms 15864 KB Output is correct
42 Correct 17 ms 15864 KB Output is correct
43 Correct 17 ms 15864 KB Output is correct
44 Correct 20 ms 15864 KB Output is correct
45 Correct 18 ms 15864 KB Output is correct
46 Correct 15 ms 15864 KB Output is correct
47 Correct 16 ms 15936 KB Output is correct
48 Correct 21 ms 16116 KB Output is correct
49 Correct 977 ms 61724 KB Output is correct
50 Correct 958 ms 68700 KB Output is correct
51 Correct 888 ms 75832 KB Output is correct
52 Correct 1059 ms 83088 KB Output is correct
53 Correct 786 ms 93584 KB Output is correct
54 Correct 746 ms 100672 KB Output is correct
55 Correct 842 ms 107856 KB Output is correct
56 Correct 863 ms 114820 KB Output is correct
57 Correct 875 ms 126056 KB Output is correct
58 Correct 892 ms 133044 KB Output is correct
59 Correct 865 ms 140164 KB Output is correct
60 Correct 996 ms 147328 KB Output is correct
61 Correct 1537 ms 163240 KB Output is correct
62 Correct 1291 ms 170428 KB Output is correct
63 Correct 1114 ms 170428 KB Output is correct
64 Correct 1221 ms 170428 KB Output is correct
65 Correct 1306 ms 170428 KB Output is correct
66 Correct 1303 ms 170428 KB Output is correct
67 Correct 1095 ms 171564 KB Output is correct
68 Correct 1052 ms 178392 KB Output is correct
69 Correct 1119 ms 185136 KB Output is correct
70 Correct 1103 ms 192188 KB Output is correct
71 Correct 1018 ms 199372 KB Output is correct
72 Correct 1026 ms 206600 KB Output is correct
73 Correct 1229 ms 213836 KB Output is correct
74 Correct 957 ms 221104 KB Output is correct
75 Correct 1148 ms 228740 KB Output is correct
76 Correct 946 ms 236912 KB Output is correct
77 Correct 1058 ms 245028 KB Output is correct
78 Correct 712 ms 255532 KB Output is correct