답안 #998671

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
998671 2024-06-14T13:41:45 Z Ghetto Islands (IOI08_islands) C++17
컴파일 오류
0 ms 0 KB
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pii = pair<int, int>;
using pil = pair<int, lint>;
using pli = pair<lint, int>;
const int MAX_N = 1e6 + 5;

int n;
int to[MAX_N];
vector<pil> adj[MAX_N];

bool seen[MAX_N];
void dfs1(int s) {
    stack<int> ord = {s};
    while (ord.size()) {
        int u = ord.top(); ord.pop();
        seen[u] = true;
        for (pil x : adj[u])
            if (!seen[x.first]) ord.push(x.first);
    }
}

vector<int> cyc;
bool in_cyc[MAX_N];
bool vis[MAX_N];
void dfs2(int s) {
    vector<int> bef;
    stack<int> ord = {s};
    while (ord.size()) {
        int u = ord.top(); ord.pop();
        vis[u] = true, bef.push_back(u);
        int v = to[u];
        if (!vis[v]) { ord.push(v); continue; }
        int i = ord.size() - 1;
        while (true) {
            cyc.push_back(ord[i]), in_cyc[ord[i]] = true;
            if (ord[i] == v) break;
            i--;
        }
        break;
    }
}

lint dp[MAX_N];
int par[MAX_N];
lint dfs3(int u) {
    lint ans = 0;
    multiset<lint> trans;
    for (pil x : adj[u]) {
        int v = x.first;
        if (v == par[u] || in_cyc[v]) continue;
        par[v] = u, ans = max(ans, dfs3(v));
        dp[u] = max(dp[u], dp[v] + x.second);
        trans.insert(dp[v] + x.second);
        if (trans.size() > 2) trans.erase(trans.begin());
    }
    
    lint new_ans = 0;
    for (int i = 1; i <= 2; i++)
        if (trans.size()) { new_ans += *trans.rbegin(); trans.erase(next(trans.end(), -1)); }
    ans = max(ans, new_ans);
    return ans;
}

vector<lint> lens;
lint len_sum;
void precomp() {
    if (cyc.size() == 2) {
        set<int> lens_set;
        for (int u : cyc) {
            for (pil x : adj[u]) {
                int v = x.first;
                if (!in_cyc[v]) continue;
                lens_set.insert(x.second);
            }
        }
        lens.insert(lens.begin(), lens_set.begin(), lens_set.end());
        len_sum = lens[0] + lens[1];
        return;       
    }

    for (int i = 0; i < cyc.size(); i++) {
        int aft = (i + 1 == (int) cyc.size()) ? 0 : i + 1;
        int u = cyc[i], v = cyc[aft];
        for (pil x : adj[u]) 
            if (x.first == v) lens.push_back(x.second), len_sum += x.second;
    }
}

lint comp() {
    multiset<lint> trans;
    vector<lint> val = {0};
    for (int i = 1; i < cyc.size(); i++) 
        val.push_back(val.back() + lens[i - 1]);
    for (int i = 1; i < cyc.size(); i++)
        val[i] = dp[cyc[i]] - val[i], trans.insert(val[i]);
    lint shift = 0;
        
    lint ans = 0;
    for (int i = 0; i < cyc.size(); i++) {
        ans = max(ans, dp[cyc[i]] + len_sum + *trans.rbegin() + shift);
        if (i + 1 == (int) cyc.size()) continue;
        trans.erase(trans.find(val[i + 1]));
        val[i] = dp[cyc[i]] - len_sum - shift;
        trans.insert(val[i]);
        shift += lens[i];
    }
    return ans;
}

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

    lint ans = 0;
    for (int u = 1; u <= n; u++) {
        if (seen[u]) continue;
        cyc.clear(), lens.clear(), len_sum = 0;
        lint new_ans = 0;
        
        dfs1(u), dfs2(u);
        for (int u : cyc) new_ans = max(new_ans, dfs3(u));
        precomp();
        
        new_ans = max(new_ans, comp());
        reverse(cyc.begin(), cyc.end()), reverse(lens.begin(), next(lens.end(), -1));
        new_ans = max(new_ans, comp());
        ans += new_ans;
    }
    cout << ans << '\n';
}

Compilation message

islands.cpp: In function 'void dfs1(int)':
islands.cpp:15:24: error: could not convert '{s}' from '<brace-enclosed initializer list>' to 'std::stack<int>'
   15 |     stack<int> ord = {s};
      |                        ^
      |                        |
      |                        <brace-enclosed initializer list>
islands.cpp: In function 'void dfs2(int)':
islands.cpp:29:24: error: could not convert '{s}' from '<brace-enclosed initializer list>' to 'std::stack<int>'
   29 |     stack<int> ord = {s};
      |                        ^
      |                        |
      |                        <brace-enclosed initializer list>
islands.cpp:37:30: error: no match for 'operator[]' (operand types are 'std::stack<int>' and 'int')
   37 |             cyc.push_back(ord[i]), in_cyc[ord[i]] = true;
      |                              ^
islands.cpp:37:46: error: no match for 'operator[]' (operand types are 'std::stack<int>' and 'int')
   37 |             cyc.push_back(ord[i]), in_cyc[ord[i]] = true;
      |                                              ^
islands.cpp:38:20: error: no match for 'operator[]' (operand types are 'std::stack<int>' and 'int')
   38 |             if (ord[i] == v) break;
      |                    ^
islands.cpp: In function 'void precomp()':
islands.cpp:83:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |     for (int i = 0; i < cyc.size(); i++) {
      |                     ~~^~~~~~~~~~~~
islands.cpp: In function 'lint comp()':
islands.cpp:94:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |     for (int i = 1; i < cyc.size(); i++)
      |                     ~~^~~~~~~~~~~~
islands.cpp:96:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |     for (int i = 1; i < cyc.size(); i++)
      |                     ~~^~~~~~~~~~~~
islands.cpp:101:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |     for (int i = 0; i < cyc.size(); i++) {
      |                     ~~^~~~~~~~~~~~