답안 #445563

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
445563 2021-07-18T17:08:33 Z JerryLiu06 Islands (IOI08_islands) C++17
70 / 100
392 ms 131076 KB
#include <bits/stdc++.h>
 
using namespace std;
 
using ll = long long;
using ld = long double;
using db = double;
using str = string;
 
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
 
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
 
#define mp make_pair
#define f first
#define s second
 
#define sz(x) (int)(x).size()
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert 
#define ft front()
#define bk back()
#define pb push_back
#define pf push_front
 
#define lb lower_bound
#define ub upper_bound
 
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define R0F(i, a) ROF(i, 0, a)
#define EACH(a, x) for (auto& a : x)
 
ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); }
ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
 
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
 
template<class T> void remDup(vector<T>& v) { sor(v); v.erase(unique(all(v)), v.end()); }
 
const int MOD = 1e9 + 7;
const int MX = 1e6 + 10;
const ll INF = 1e18;

int N; vpi adj[MX]; pi nxt[MX]; ll ans = 0;

int vis[MX]; ll label[MX]; ll cycLen[MX];

int numCyc = 0; int comp[MX]; vi cyc[MX]; 

int depth[MX]; ll dist[MX]; int lift[MX][20];

int numTree = 0; vi tree[MX]; int compTree[MX]; ll val[MX];

// LCA with Binary Jump ~ Get the maximum diameter

ll DFS2(int X, int P, int curTree) {
    tree[curTree].pb(X);
    compTree[X] = curTree;
    ll maxDist = 0;

    EACH(Y, adj[X]) {
        if (Y.f != P && !comp[Y.f]) {
            depth[Y.f] = depth[X] + 1;
            dist[Y.f] = dist[X] + Y.s;
            lift[Y.f][0] = X;

            ckmax(maxDist, DFS2(Y.f, X, numTree) + Y.s);
        }
    }
    return maxDist;
}
int jump(int X, int len) {
    F0R(i, 20) {
        if (len & (1 << i)) {
            X = lift[X][i];
        }
    }
    return X;
}
int LCA(int A, int B) {
    if (depth[A] < depth[B]) {
        swap(A, B);
    }
    A = jump(A, depth[A] - depth[B]);
    if (A == B) return A;

    R0F(i, 20) {
        if (lift[A][i] != lift[B][i]) {
            A = lift[A][i]; B = lift[B][i];
        }
    }
    return lift[A][0];
}
ll DIST(int A, int B) {
    return dist[A] + dist[B] - 2 * dist[LCA(A, B)];
}

int furthestAway(int X, int ind) {
    pl res = {-1, -1}; 
    
    EACH(Y, tree[ind]) {
        ckmax(res, {DIST(X, Y), Y}); 
    }
    return res.s;
}

ll getDiam(int ind) {
    int ST = furthestAway(tree[ind][0], ind); 
    int EN = furthestAway(ST, ind); 
    
    ll res = 0; ckmax(res, DIST(ST, EN)); return res;
}

// DFS in Functional Graph ~ Generate cycles

void DFS1(int X) {
    int cur = X; 
    
    while (vis[X] == 0) { 
        vis[X] = cur; 
        X = nxt[X].f; 
    }

    ll curDist = 0; if (vis[X] == cur) numCyc++;
    
    while (vis[X] == cur) { 
        vis[X] = N + cur; 
        label[X] = curDist; 
        comp[X] = numCyc;
        cyc[numCyc].pb(X);

        curDist += nxt[X].s;
        
        X = nxt[X].f;
    }
    if (vis[X] == N + cur) {
        cycLen[numCyc] = curDist;
    }
    while (vis[X] == N + cur) {
        vis[X] = 2 * N + cur;
        
        val[X] = DFS2(X, 0, ++numTree);
        
        X = nxt[X].f;
    }
}

ll getMaxDist(int ind) {
    ll res = 0;
    
    EACH(X, cyc[ind]) {
        ckmax(res, getDiam(compTree[X]));
    }
    
    // case 1: i < j, val[j] + label[j] + val[i] - label[i]
    
    ll cumMax = -INF; 
    
    FOR(i, 1, sz(cyc[ind])) {
        int A = cyc[ind][i];
        
        int B = cyc[ind][i - 1];
        
        ckmax(cumMax, val[B] - label[B]);
        
        ckmax(res, val[A] + label[A] + cumMax);
    }
    
    // case 2: i < j, cyclen[ind] + val[j] - label[j] + val[i] + label[i]
    
    cumMax = -INF;
    
    FOR(i, 1, sz(cyc[ind])) {
        int A = cyc[ind][i];
        
        int B = cyc[ind][i - 1];
        
        ckmax(cumMax, val[B] + label[B]);
        
        ckmax(res, cycLen[ind] + val[A] - label[A] + cumMax);
    }
    return res;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);

    cin >> N; FOR(i, 1, N + 1) { int A, L; cin >> A >> L; adj[i].pb({A, L}), adj[A].pb({i, L}); nxt[i] = {A, L}; }

    FOR(i, 1, N + 1) {
        if (!vis[i]) {
            DFS1(i);
        }
    }

    FOR(j, 1, 20) {
        FOR(i, 1, N + 1) {
            lift[i][j] = lift[lift[i][j - 1]][j - 1];
        }
    }
    // FOR(i, 1, numTree + 1) {
    //     getDiam(i);
    // }
    FOR(i, 1, numCyc + 1) {
        ans += getMaxDist(i);
    }
    cout << ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 70812 KB Output is correct
2 Correct 44 ms 70852 KB Output is correct
3 Correct 44 ms 70872 KB Output is correct
4 Correct 44 ms 70816 KB Output is correct
5 Correct 45 ms 70748 KB Output is correct
6 Correct 44 ms 70724 KB Output is correct
7 Correct 42 ms 70732 KB Output is correct
8 Correct 43 ms 70808 KB Output is correct
9 Correct 43 ms 70832 KB Output is correct
10 Correct 46 ms 70800 KB Output is correct
11 Correct 43 ms 70852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 71020 KB Output is correct
2 Correct 43 ms 71096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 71196 KB Output is correct
2 Correct 45 ms 71620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 73696 KB Output is correct
2 Correct 70 ms 77408 KB Output is correct
3 Correct 60 ms 74560 KB Output is correct
4 Correct 51 ms 72700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 80336 KB Output is correct
2 Correct 109 ms 89988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 203 ms 107248 KB Output is correct
2 Correct 205 ms 111436 KB Output is correct
3 Correct 238 ms 120524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 201 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 344 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 392 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -