답안 #998243

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
998243 2024-06-13T12:27:15 Z Ghetto Islands (IOI08_islands) C++17
60 / 100
2000 ms 131072 KB
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pil = pair<int, lint>;
const int MAX_N = 1e6 + 5;

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

bool seen[MAX_N];
vector<int> cyc;
bool in_cyc[MAX_N]; // In ANY of the cycles
vector<int> ord;
void dfs1(int u, int par = -1) {
    seen[u] = true, ord.push_back(u);
    bool par_seen = false;
    for (pil x : adj[u]) {
        int v = x.first;
        if (v == par && !par_seen) { par_seen = true; continue; }
        if (!seen[v]) { dfs1(v, u); continue; }
        if (cyc.size()) continue;
        int i = ord.size() - 1;
        while (true) {
            cyc.push_back(ord[i]), in_cyc[ord[i]] = true;
            if (ord[i] == v) break;
            i--;
        }
    }
    ord.pop_back();
}

lint dp[MAX_N];
lint dfs2(int u, int par = -1) {
    lint ans = 0;
    priority_queue<lint> trans;
    for (pil x : adj[u]) {
        int v = x.first;
        if (v == par || in_cyc[v]) continue;
        ans = max(ans, dfs2(v, u));
        dp[u] = max(dp[u], dp[v] + x.second);
        trans.push(dp[v] + x.second);
    }
    
    lint new_ans = 0;
    for (int i = 1; i <= 2; i++)
        if (trans.size()) { new_ans += trans.top(); trans.pop(); }
    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() {
    vector<lint> pref_len = {0};
    for (int i = 1; i < cyc.size(); i++) 
        pref_len.push_back(pref_len.back() + lens[i - 1]);
    
    lint ans = 0;
    for (int i = 0; i < cyc.size(); i++) {
        for (int j = 0; j < cyc.size(); j++) {
            if (i == j) continue;
            int u = cyc[i], v = cyc[j];
            lint diff = pref_len[j] - pref_len[i];
            lint new_ans = (diff < 0) ? len_sum + diff + dp[u] + dp[v] : diff + dp[u] + dp[v];
            // cout << u << " " << v << ": " << diff << " " << new_ans << endl;
            ans = max(ans, new_ans);
        }
    }
    return ans;
}

int main() {
    // freopen("isl.in", "r", stdin), freopen("isl.out", "w", stdout);
    cin >> n;
    for (int u = 1; u <= n; u++) {
        int v; cin >> v;
        lint len; cin >> len;
        adj[u].push_back({v, len}), adj[v].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);
        for (int u : cyc) new_ans = max(new_ans, dfs2(u));
        precomp();
        // for (int i = 0; i < cyc.size(); i++) {
        //     cout << cyc[i] << " " << lens[i] << endl;
        // }
        
        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 precomp()':
islands.cpp:68:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |     for (int i = 0; i < cyc.size(); i++) {
      |                     ~~^~~~~~~~~~~~
islands.cpp: In function 'lint comp()':
islands.cpp:78:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |     for (int i = 1; i < cyc.size(); i++)
      |                     ~~^~~~~~~~~~~~
islands.cpp:82:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |     for (int i = 0; i < cyc.size(); i++) {
      |                     ~~^~~~~~~~~~~~
islands.cpp:83:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |         for (int j = 0; j < cyc.size(); j++) {
      |                         ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 27224 KB Output is correct
2 Correct 4 ms 27228 KB Output is correct
3 Correct 5 ms 27392 KB Output is correct
4 Correct 4 ms 27228 KB Output is correct
5 Correct 6 ms 27384 KB Output is correct
6 Correct 4 ms 27228 KB Output is correct
7 Correct 4 ms 27384 KB Output is correct
8 Correct 4 ms 27228 KB Output is correct
9 Correct 5 ms 27228 KB Output is correct
10 Correct 4 ms 27228 KB Output is correct
11 Correct 5 ms 27228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 27484 KB Output is correct
2 Correct 5 ms 27344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 27484 KB Output is correct
2 Correct 18 ms 27856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 28900 KB Output is correct
2 Correct 859 ms 31784 KB Output is correct
3 Correct 17 ms 28760 KB Output is correct
4 Correct 11 ms 27996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1319 ms 33328 KB Output is correct
2 Correct 1559 ms 36320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1599 ms 43932 KB Output is correct
2 Execution timed out 2035 ms 49052 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2032 ms 55692 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 511 ms 105740 KB Output is correct
2 Runtime error 889 ms 131072 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 529 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -