답안 #629738

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
629738 2022-08-15T02:21:38 Z dooompy Islands (IOI08_islands) C++17
0 / 100
14 ms 23788 KB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

struct edge {
    int to; ll cost; int idx;
};

vector<edge> adj[1000005];
bitset<1000005> seen, seenedge, inloop;
int par[1000005];

int cyclestart, cycleend;

void dfs(int node) {
    seen[node] = true;
    for (auto nxt : adj[node]) {
        if (seenedge[nxt.idx]) continue;
        seenedge[nxt.idx] = true;
        if (seen[nxt.to]) {
            // cycle found
            cyclestart = node, cycleend = nxt.to;
        } else {
            par[nxt.to] = node;
            dfs(nxt.to);
        }
    }
}

ll globmax = 0;

ll dfs_tree(int node, int par = -1) {
    ll ans = 0;

    for (auto nxt : adj[node]) {
        if (nxt.to == par) continue;
        if (inloop[nxt.to]) continue;

        ll temp = dfs_tree(nxt.to, node);

        globmax = max(globmax, ans + temp + nxt.cost);
        ans = max(ans, temp + nxt.cost);
    }

    return ans;
}

int main() {
    freopen("isl.in", "r", stdin); freopen("isl.out", "w", stdout);
    int n; cin >> n;
    for (int i = 1; i <= n; i++) {
        int to; ll c; cin >> to >> c;
        adj[i].push_back({to, c, i});
        adj[to].push_back({i, c, i});
    }

    ll total = 0;

    for (int i = 1; i <= n; i++) {
        if (seen[i]) continue;
        dfs(i);

        vector<int> loop;

        while (cyclestart != cycleend) {
            loop.push_back(cyclestart);
            cyclestart = par[cyclestart];
        }

        loop.push_back(cyclestart);

        for (auto cur : loop) {
            inloop[cur] = true;
        }

        ll ans = 0;

        ll prevInMax = 0;
        ll prevOutMax = 0;

        map<int, ll> cursum;
        ll totalsum = 0;
        map<int, bool> seensecond;
        for (int i = 0; i <= loop.size(); i++) {
            if (i == 0) continue;
            if (i == loop.size()) {
                for (auto nxt : adj[loop[0]]) {
                    if (nxt.to == loop.back() && !seensecond[nxt.idx]) {
                        totalsum += nxt.cost;
                        break;
                    }
                }
                break;
            }
            for (auto nxt : adj[loop[i]]) {
                if (nxt.to == loop[i-1] && !seensecond[nxt.idx]) {
                    seensecond[nxt.idx] = true;
                    cursum[loop[i]] += nxt.cost;
                    totalsum += nxt.cost;
                    break;
                }
            }
            cursum[loop[i]] += cursum[loop[i-1]];
        }

        for (int i = 0; i < loop.size(); i++) {
            globmax = 0;
            ll tree = dfs_tree(loop[i]);

            if (i > 0) {
                ans = max({ans, tree + cursum[loop[i]] + prevInMax, totalsum + prevOutMax - cursum[loop[i]] + tree});
            }
            ans = max(ans, globmax);

            prevInMax = max(prevInMax, tree - cursum[loop[i]]);
            prevOutMax = max(prevOutMax, tree + cursum[loop[i]]);



        }

        total += ans;

    }

    cout << total << endl;

}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:86:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |         for (int i = 0; i <= loop.size(); i++) {
      |                         ~~^~~~~~~~~~~~~~
islands.cpp:88:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |             if (i == loop.size()) {
      |                 ~~^~~~~~~~~~~~~~
islands.cpp:108:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |         for (int i = 0; i < loop.size(); i++) {
      |                         ~~^~~~~~~~~~~~~
islands.cpp:51:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |     freopen("isl.in", "r", stdin); freopen("isl.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
islands.cpp:51:43: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |     freopen("isl.in", "r", stdin); freopen("isl.out", "w", stdout);
      |                                    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 12 ms 23764 KB Output isn't correct
2 Incorrect 13 ms 23768 KB Output isn't correct
3 Incorrect 12 ms 23732 KB Output isn't correct
4 Incorrect 12 ms 23728 KB Output isn't correct
5 Incorrect 12 ms 23764 KB Output isn't correct
6 Incorrect 12 ms 23724 KB Output isn't correct
7 Incorrect 12 ms 23764 KB Output isn't correct
8 Incorrect 12 ms 23788 KB Output isn't correct
9 Incorrect 12 ms 23764 KB Output isn't correct
10 Incorrect 13 ms 23764 KB Output isn't correct
11 Incorrect 12 ms 23748 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 12 ms 23764 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 12 ms 23764 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 11 ms 23764 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 12 ms 23764 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 14 ms 23712 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 13 ms 23764 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 12 ms 23764 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 12 ms 23764 KB Output isn't correct
2 Halted 0 ms 0 KB -