답안 #998713

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
998713 2024-06-14T14:42:32 Z Ghetto Islands (IOI08_islands) C++17
100 / 100
1095 ms 131072 KB
#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC traget("avx2")
#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> from[MAX_N];
  
vector<int> cyc; // Reverse order btw
bool in_cyc[MAX_N];
bool vis[MAX_N];
void dfs2(int s) {
    vector<int> bef;
    stack<int> ord; ord.push(s);
    while (ord.size()) {
        int u = ord.top(); ord.pop();
        vis[u] = true, bef.push_back(u);
        if (!vis[to[u]]) { ord.push(to[u]); continue; }
        int i = bef.size() - 1;
        while (true) {
            cyc.push_back(bef[i]), in_cyc[bef[i]] = true;
            if (bef[i] == to[u]) break;
            i--;
        }
        break;
    }
}
 
bool seen[MAX_N];
lint dp[MAX_N];
lint dfs3(int s) {
    vector<int> preord;
    stack<int> ord; ord.push(s);
    while (ord.size()) {
        int u = ord.top(); ord.pop();
        seen[u] = true;
        preord.push_back(u);
        for (pil x : from[u]) {
            if (in_cyc[x.first]) continue;
            ord.push(x.first);
        }
    }

    lint max_path = 0;
    for (int i = preord.size() - 1; i >= 0; i--) {
        int u = preord[i];
        lint max1 = 0, max2 = 0;
        for (pil x : from[u]) {
            if (in_cyc[x.first]) continue;
            dp[u] = max(dp[u], dp[x.first] + x.second);
            if (dp[x.first] + x.second > max1) max2 = max1, max1 = dp[x.first] + x.second;
            else if (dp[x.first] + x.second > max2) max2 = dp[x.first] + x.second;
        }
        max_path = max(max_path, max1 + max2);
    }
    return max_path;
}
 
vector<lint> lens;
lint len_sum;
void precomp() {
    if (cyc.size() == 2) {
        set<int> lens_set;
        for (int u : cyc) {
            for (pil x : from[u]) {
                if (!in_cyc[x.first]) 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;
        for (pil x : from[cyc[i]]) 
            if (x.first == cyc[aft]) 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.sync_with_stdio(false), cin.tie(0);
    cin >> n;
    for (int u = 1; u <= n; u++) {
        cin >> to[u];
        lint len; cin >> len;
        from[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;
        
        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:2: warning: ignoring '#pragma GCC traget' [-Wunknown-pragmas]
    2 | #pragma GCC traget("avx2")
      | 
islands.cpp: In function 'void precomp()':
islands.cpp:81:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |     for (int i = 0; i < cyc.size(); i++) {
      |                     ~~^~~~~~~~~~~~
islands.cpp: In function 'lint comp()':
islands.cpp:91:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |     for (int i = 1; i < cyc.size(); i++)
      |                     ~~^~~~~~~~~~~~
islands.cpp:93:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |     for (int i = 1; i < cyc.size(); i++)
      |                     ~~^~~~~~~~~~~~
islands.cpp:98:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |     for (int i = 0; i < cyc.size(); i++) {
      |                     ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 26972 KB Output is correct
2 Correct 6 ms 27084 KB Output is correct
3 Correct 7 ms 26972 KB Output is correct
4 Correct 7 ms 27140 KB Output is correct
5 Correct 6 ms 26972 KB Output is correct
6 Correct 7 ms 26968 KB Output is correct
7 Correct 6 ms 27088 KB Output is correct
8 Correct 7 ms 26972 KB Output is correct
9 Correct 6 ms 27080 KB Output is correct
10 Correct 7 ms 26972 KB Output is correct
11 Correct 7 ms 26972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 27228 KB Output is correct
2 Correct 7 ms 26972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 27228 KB Output is correct
2 Correct 8 ms 27484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 27996 KB Output is correct
2 Correct 24 ms 29604 KB Output is correct
3 Correct 12 ms 28252 KB Output is correct
4 Correct 9 ms 27484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 30736 KB Output is correct
2 Correct 47 ms 32580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 37372 KB Output is correct
2 Correct 123 ms 43908 KB Output is correct
3 Correct 161 ms 49940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 162 ms 46636 KB Output is correct
2 Correct 216 ms 62936 KB Output is correct
3 Correct 446 ms 69568 KB Output is correct
4 Correct 435 ms 81972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 151 ms 65376 KB Output is correct
2 Correct 647 ms 105532 KB Output is correct
3 Correct 269 ms 62796 KB Output is correct
4 Correct 422 ms 91116 KB Output is correct
5 Correct 436 ms 90744 KB Output is correct
6 Correct 448 ms 77764 KB Output is correct
7 Correct 692 ms 112856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 201 ms 85892 KB Output is correct
2 Correct 182 ms 82032 KB Output is correct
3 Correct 1095 ms 131072 KB Output is correct
4 Correct 356 ms 76980 KB Output is correct
5 Correct 533 ms 95128 KB Output is correct
6 Correct 263 ms 83500 KB Output is correct
7 Correct 451 ms 81500 KB Output is correct