Submission #488921

# Submission time Handle Problem Language Result Execution time Memory
488921 2021-11-20T19:43:35 Z dxz05 Islands (IOI08_islands) C++14
21 / 100
929 ms 131076 KB
//#pragma GCC optimize("Ofast,O2,O3,unroll-loops")
//#pragma GCC target("avx2")

#include <iostream>
#include <vector>
#include <bitset>
#include <stack>
#include <algorithm>
#include <chrono>
#include <random>

using namespace std;

#define SZ(s) ((int)s.size())
#define all(x) (x).begin(), (x).end()
#define lla(x) (x).rbegin(), (x).rend()

#define MP make_pair

typedef long long ll;
const double eps = 0.000001;
const int MOD = 998244353;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 1e6 + 5;
const int M = 222;

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

int f[N]; int w[N];
bitset<N> used;
stack<int> st;

vector<int> cycle;

void dfs0(int v){
    if (!cycle.empty()) return;
    used[v] = true;
    st.push(v);

    if (used[f[v]]){
        while (!st.empty()){
            int x = st.top(); st.pop();
            cycle.push_back(x);
            if (x == f[v]) break;
        }
        return;
    }
    if (!cycle.empty()) return;

    dfs0(f[v]);

    if (!st.empty()) st.pop();
}

int parent[N];
int find(int x){
    return (x == parent[x] ? x : parent[x] = find(parent[x]));
}

vector<int> g[N];

bitset<N> incycle;

int A(int u, int v){
    if (f[u] == v && f[v] != u) return w[u];
    if (f[u] != v && f[v] == u) return w[v];
    return max(w[u], w[v]);
}

void dfs(int v, int p, ll d, int root, vector<int> &tree, vector<ll> &dist, bool flag){
    tree.push_back(v);
    dist.push_back(d);
    for (int u : g[v]){
        if (u != p){
            if (u == root || !incycle[u]) dfs(u, v, d + A(u, v), root, tree, dist, flag);
        }
    }
    if (flag) g[v].clear();
}

ll arr[N];

ll component(int root){
    cycle.clear();
    while (!st.empty()) st.pop();

    dfs0(root);

    for (int v : cycle){
        incycle[v] = true;
    }

    ll ans = 0;
    vector<int> tree; vector<ll> dist;
    for (int v : cycle){
        dfs(v, v, 0, v, tree, dist, false);

        int i = max_element(all(dist)) - dist.begin();
        ans = max(ans, dist[i]);
        arr[v] = dist[i];
        int u = tree[i];

        tree.clear(); dist.clear();
        dfs(u, u, 0, v, tree, dist, true);

        ans = max(ans, *max_element(all(dist)));
        tree.clear(), dist.clear();
    }

    int n = cycle.size();

    if (n == 2){
        int u = cycle[0], v = cycle[1];
        ans = max(ans, arr[u] + arr[v] + A(u, v));
        return ans;
    }

    vector<ll> a(n);
    a[0] = {arr[cycle[0]]};
    ll sum = 0;
    for (int i = 1; i < n; i++){
        sum += A(cycle[i - 1], cycle[i]);
        a[i] = arr[cycle[i]] + sum;
    }
    sum += A(cycle[0], cycle.back());

//    for (int x : cycle) cout << x << ' '; cout << endl;
//    for (ll x : a) cout << x << ' '; cout << endl;

    ans = max(ans, arr[cycle[0]] + *max_element(a.begin() + 1, a.end()));

    vector<ll> sf(n, a.back());
    for (int i = n - 2; i >= 0; i--) sf[i] = max(sf[i + 1], a[i]);

    ll add = 0;
    int v, u;
    ll mx = 0;
    for (int i = 1; i < n; i++){
        v = cycle[i], u = cycle[i - 1];
        add -= A(v, u);
        mx = max(mx, a[i - 1] + sum);

        ans = max(ans, arr[v] + add + mx);
        if (i + 1 < n) ans = max(ans, arr[v] + add + sf[i + 1]);
        mx = max(mx, a[i]);

//        for (int j = 1; j <= n; j++) cout << ST.get(j, j) << ' '; cout << endl;
    }

    return ans;
}

void solve(int TC) {
    int n;
    scanf("%d", &n);

    for (int i = 1; i <= n; i++) parent[i] = i;

    for (int i = 1; i <= n; i++){
        scanf("%d %d", f + i, w + i);
        if (rng() & 1) parent[find(i)] = find(f[i]); else
            parent[find(f[i])] = find(i);
        if (f[f[i]] != i){
            g[i].emplace_back(f[i]);
            g[f[i]].emplace_back(i);
        }
    }

    bitset<N> comp;

    ll ans = 0;
    for (int i = 1; i <= n; i++){
        if (!comp[find(i)]){
            comp[find(i)] = true;
            ll cur = component(i);
            ans += cur;
        }
    }

    printf("%I64d", ans);

}

int main() {
//    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

#ifdef dddxxz
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    int TC = 1;
//    cin >> TC;

    for (int test = 1; test <= TC; test++) {
        //debug(test);
        //cout << "Case #" << test << ": ";
        solve(test);
    }

    return 0;
}

Compilation message

islands.cpp: In function 'void solve(int)':
islands.cpp:181:17: warning: format '%d' expects argument of type 'int', but argument 2 has type 'll' {aka 'long long int'} [-Wformat=]
  181 |     printf("%I64d", ans);
      |             ~~~~^   ~~~
      |                 |   |
      |                 int ll {aka long long int}
      |             %I64lld
islands.cpp:156:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  156 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
islands.cpp:161:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  161 |         scanf("%d %d", f + i, w + i);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23884 KB Output is correct
2 Correct 13 ms 23960 KB Output is correct
3 Incorrect 12 ms 23936 KB Output isn't correct
4 Correct 13 ms 23836 KB Output is correct
5 Correct 13 ms 23844 KB Output is correct
6 Correct 12 ms 23852 KB Output is correct
7 Correct 15 ms 23832 KB Output is correct
8 Incorrect 13 ms 23856 KB Output isn't correct
9 Incorrect 12 ms 23924 KB Output isn't correct
10 Correct 13 ms 23884 KB Output is correct
11 Incorrect 13 ms 23952 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 24012 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 23992 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 20 ms 24912 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 36 ms 28900 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 91 ms 36776 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 184 ms 46220 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 348 ms 84652 KB Output is correct
2 Incorrect 929 ms 131072 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 392 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -