답안 #1091409

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1091409 2024-09-20T19:21:23 Z Zicrus Islands (IOI08_islands) C++17
100 / 100
636 ms 131072 KB
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
 
struct node {
    int nxt;
    int first, second;
};
 
int n;
vector<int> adj; // length, node
vector<bool> vst;
vector<int> comps;
vector<bool> cyc;
vector<pair<int, int>> nxt; // length, node
vector<ll> treeSz;
 
vector<node> nd;
 
ll val;
 
int cycRoot;
ll dfsCyc(int &cur, int &par) {
    vst[cur] = true;
    {
        ll numECnt = 0;
        int e = adj[cur];
        while (e != -1) {
            if (nd[e].second == par) {
                numECnt++;
            }
            e = nd[e].nxt;
        }
        if (numECnt == 2) {
            pair<int, int> numE[2];
            e = adj[cur];
            while (e != -1) {
                if (nd[e].second == par) {
                    numE[--numECnt] = {nd[e].first, nd[e].second};
                }
                e = nd[e].nxt;
            }
            vst[numE[0].second] = true;
            cyc[cur] = cyc[numE[0].second] = true;
            cycRoot = cur;
            nxt[cur] = numE[0];
            nxt[numE[0].second] = {numE[1].first, cur};
            return -1;
        }
    }
    int e = adj[cur];
    while (e != -1) {
        if (nd[e].second == par) { e = nd[e].nxt; continue; }
        if (vst[nd[e].second] && !cyc[nd[e].second]) {
            cyc[cur] = true;
            nxt[cur] = {nd[e].first, nd[e].second};
            cycRoot = nd[e].second;
            return nd[e].second;
        }
        else {
            ll val = dfsCyc(nd[e].second, cur);
            if (val == -1) { e = nd[e].nxt; continue; }
            cyc[cur] = true;
            nxt[cur] = {nd[e].first, nd[e].second};
            return cur == val ? -1 : val;
        }
        e = nd[e].nxt;
    }
    return -1;
}
 
ll mxLen = 0;
ll dfsTreeSz(int &cur, int &par) {
    ll mx = 0;
    ll nxtMx = 0;
    int e = adj[cur];
    while (e != -1) {
        if (nd[e].second == par) { e = nd[e].nxt; continue; }
        if (cyc[nd[e].second]) { e = nd[e].nxt; continue; }
        val = nd[e].first + dfsTreeSz(nd[e].second, cur);
        if (val > mx) {
            nxtMx = mx;
            mx = val;
        }
        else if (val > nxtMx) {
            nxtMx = val;
        }
        e = nd[e].nxt;
    }
    mxLen = max(mxLen, nxtMx + mx);
    return mx;
}
 
ll solveComp(int &root) {
    mxLen = 0;
    int minusOne = -1;
    dfsCyc(root, minusOne);
    int x = cycRoot;
    vector<int> cycle;
    do {
        cycle.push_back(x);
        x = nxt[x].second;
    } while (x != cycRoot);
    for (int i = 0; i < cycle.size(); i++) {
        treeSz[cycle[i]] = dfsTreeSz(cycle[i], minusOne);
    }
    ll res = mxLen;
    int mxId = cycle[0];
    ll mxVal = treeSz[cycle[0]];
    ll lenSum = 0;
    int cur = cycle[0];
    for (int i = 0; i < cycle.size(); i++) {
        if (cur == cycle[i] || mxId == cycle[i]) {
            cur = cycle[i];
            lenSum = nxt[cur].first;
            cur = nxt[cur].second;
            mxId = cur;
            mxVal = lenSum + treeSz[cur];
        }
        while (cur != cycle[i]) {
            val = lenSum + treeSz[cur];
            if (val >= mxVal) {
                mxVal = val;
                mxId = cur;
            }
            lenSum += nxt[cur].first;
            cur = nxt[cur].second;
        }
        res = max(res, treeSz[cycle[i]] + mxVal);
        lenSum -= nxt[cycle[i]].first;
        mxVal -= nxt[cycle[i]].first;
    }
    return res;
}
 
void dfs1(int &cur) {
    vst[cur] = true;
    int e = adj[cur];
    while (e != -1) {
        if (vst[nd[e].second]) { e = nd[e].nxt; continue; }
        dfs1(nd[e].second);
        e = nd[e].nxt;
    }
}
 
int main() {
    cin >> n;
    adj = vector<int>(n, -1);
    cyc = vector<bool>(n);
    nxt = vector<pair<int, int>>(n);
    treeSz = vector<ll>(n);
    nd = vector<node>(2*n);
    for (auto &e : nd) e.nxt = -1;
    ll ptr = 0;
    ll a, l;
    for (int i = 0; i < n; i++) {
        cin >> a >> l;
        nd[2*i].first = l;
        nd[2*i].second = a-1;
        nd[2*i].nxt = adj[i];
        adj[i] = 2*i;
        nd[2*i+1].first = l;
        nd[2*i+1].second = i;
        nd[2*i+1].nxt = adj[a-1];
        adj[a-1] = 2*i+1;
    }
    vst = vector<bool>(n);
    for (int i = 0; i < n; i++) {
        if (vst[i]) continue;
        dfs1(i);
        comps.push_back(i);
    }
    vst = vector<bool>(n);
 
    ll res = 0;
    for (auto &e : comps) {
        res += solveComp(e);
    }
    cout << res;
}

Compilation message

islands.cpp: In function 'll solveComp(int&)':
islands.cpp:105:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |     for (int i = 0; i < cycle.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
islands.cpp:113:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |     for (int i = 0; i < cycle.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:155:8: warning: unused variable 'ptr' [-Wunused-variable]
  155 |     ll ptr = 0;
      |        ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 2 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 1368 KB Output is correct
2 Correct 16 ms 3644 KB Output is correct
3 Correct 10 ms 1476 KB Output is correct
4 Correct 5 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 4952 KB Output is correct
2 Correct 39 ms 7508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 13140 KB Output is correct
2 Correct 82 ms 19280 KB Output is correct
3 Correct 115 ms 27864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 163 ms 24148 KB Output is correct
2 Correct 200 ms 44520 KB Output is correct
3 Correct 211 ms 54236 KB Output is correct
4 Correct 297 ms 69956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 436 ms 59220 KB Output is correct
2 Correct 603 ms 100272 KB Output is correct
3 Correct 315 ms 46984 KB Output is correct
4 Correct 412 ms 81872 KB Output is correct
5 Correct 411 ms 81608 KB Output is correct
6 Correct 636 ms 57192 KB Output is correct
7 Correct 500 ms 109036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 521 ms 131072 KB Output is correct
2 Correct 494 ms 99944 KB Output is correct
3 Correct 531 ms 131072 KB Output is correct
4 Correct 479 ms 60868 KB Output is correct
5 Correct 420 ms 82884 KB Output is correct
6 Correct 378 ms 64972 KB Output is correct
7 Correct 629 ms 57940 KB Output is correct