답안 #390264

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
390264 2021-04-15T17:30:56 Z parsabahrami Islands (IOI08_islands) C++17
30 / 100
1173 ms 131072 KB
/* There's someone in my head but it's not me */ 
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;

#define int                         unsigned int 
#define SZ(x)                       (int) x.size()
#define F                           first
#define S                           second

const int N = 1e6 + 1;
int n, bads[N / 2], rt[N], P[N], out[N], w[N], R, ptrb, ptrv, b; int A[N], ret[N], H[N], S;
vector<int> adj[N]; bitset<N> C;

int Find(int v) {
    return !rt[v] ? v : rt[v] = Find(rt[v]);
}

int Union(int v, int u) {
    u = Find(u), v = Find(v);
    if (u == v) return 1;
    rt[u] = v; 
    return 0;
}

void preDFS(int v, int tr, int p = -1) {
    if (!~b || H[b] < H[v]) b = v;
    adj[v].push_back(v);
    for (int &id : adj[v]) {
        int u = out[id] ^ id ^ v;
        if ((!C[u] || u == tr) && u != p) 
            H[u] = H[v] + w[id], preDFS(u, tr, v);
    }
    adj[v].pop_back();
}

void cycDFS(int v, int tr, int p = -1) {
    adj[v].push_back(v);
    for (int &id : adj[v]) {
        if (id != tr) {
            int u = out[id] ^ id ^ v;
            if (u != p) P[u] = id, cycDFS(u, tr, v);
        }
    }
    adj[v].pop_back();
}

ll CYC(int v, int p = -1) {
    int ans = -1; adj[v].push_back(v);
    for (int &id : adj[v]) {
        int u = out[id] ^ id ^ v;
        if (C[u] && u != p && u != R) {
            H[u] = H[v] + w[id], ans = CYC(u, v);
            break;
        }
    }
    adj[v].pop_back();
    if (!~ans) return A[v] + H[v];
    ret[Find(v)] =  max(ret[Find(v)], ans + A[v] - H[v]);
    return max(ans, A[v] + H[v]);
}

ll CYC2(int v, int p = -1) {
    int ans = -1; adj[v].push_back(v);
    for (int &id : adj[v]) {
        int u = out[id] ^ id ^ v;
        if (C[u] && u != p && u != R) {
            ans = CYC2(u, v);
            break;
        }
    }
    adj[v].pop_back();
    if (!~ans) return S - H[v] + A[v];
    ret[Find(v)] = max(ret[Find(v)], ans + A[v] + H[v]);
    return max(ans, S - H[v] + A[v]);
}

int32_t main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d%d", &out[i], &w[i]);
        adj[out[i]].push_back(i);
        if (Union(i, out[i])) 
            bads[ptrb++] = i;
    } 
    for (int i = 0; i < ptrb; i++) {
        int &id = bads[i];
        cycDFS(out[id], id); 
        int v = id; C[out[id]] = 1;
        while (P[v]) 
            C[v] = 1, v = out[P[v]] ^ P[v] ^ v;
    } 
    for (int i = 1; i <= n; i++) {
        if (!C[i]) continue;
        b = -1, preDFS(i, i); 
        A[i] = H[b]; int u = b;
        H[b] = 0, b = -1, preDFS(u, i);
        ret[Find(i)] = max(ret[Find(i)], H[b]);
    }
    ll res = 0; 
    for (int i = 0; i < ptrb; i++) {
        R = bads[i];
        H[R] = 0; S = w[R];
        int v = R; 
        while (P[v]) 
            S += w[P[v]], v = P[v] ^ v ^ out[P[v]];
        CYC(R), CYC2(R);
        res += ret[Find(R)];
    } 
    printf("%lld\n", res);
    return 0;
}

Compilation message

islands.cpp: In function 'int32_t main()':
islands.cpp:82:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   82 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
islands.cpp:84:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   84 |         scanf("%d%d", &out[i], &w[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23756 KB Output is correct
2 Correct 16 ms 23752 KB Output is correct
3 Incorrect 19 ms 23884 KB Output isn't correct
4 Correct 15 ms 23848 KB Output is correct
5 Correct 15 ms 23756 KB Output is correct
6 Correct 16 ms 23780 KB Output is correct
7 Correct 16 ms 23832 KB Output is correct
8 Correct 15 ms 23756 KB Output is correct
9 Correct 15 ms 23756 KB Output is correct
10 Correct 15 ms 23756 KB Output is correct
11 Correct 16 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 16 ms 23948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 23884 KB Output is correct
2 Incorrect 18 ms 24140 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 25 ms 24780 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 52 ms 28120 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 117 ms 36616 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 213 ms 47264 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 373 ms 89104 KB Output is correct
2 Incorrect 1173 ms 116684 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 398 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -