Submission #488814

# Submission time Handle Problem Language Result Execution time Memory
488814 2021-11-20T14:36:55 Z dxz05 Islands (IOI08_islands) C++14
80 / 100
578 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>

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;

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];

struct SegTree{
    struct node{
        ll mx;
        ll add;
        node(){
            mx = 0;
            add = 0;
        }
    };

    vector<node> tree;

    int size = 1;

    void build(int v, int tl, int tr, vector<ll> &a){
        if (tl == tr){
            tree[v].mx = a[tl - 1];
            tree[v].add = 0;
            return;
        }
        int tm = (tl + tr) >> 1;
        build(v + v, tl, tm, a);
        build(v + v + 1, tm + 1, tr, a);
        tree[v].mx = max(tree[v + v].mx, tree[v + v + 1].mx);
    }

    void init(vector<ll> &a){
        while (size < a.size()) size *= 2;
        tree.assign(size * 2 + 5, node());
        build(1, 1, size, a);
    }

    void push(int v){
        if (tree[v].add == 0) return;
        tree[v + v].mx += tree[v].add;
        tree[v + v + 1].mx += tree[v].add;
        tree[v + v].add += tree[v].add;
        tree[v + v + 1].add += tree[v].add;
        tree[v].add = 0;
    }

    void add(int l, int r, ll val, int v, int tl, int tr){
        if (l <= tl && tr <= r){
            tree[v].mx += val;
            tree[v].add += val;
            return;
        }
        if (tl > r || tr < l) return;

        push(v);

        int tm = (tl + tr) >> 1;
        add(l, r, val, v + v, tl, tm);
        add(l, r, val, v + v + 1, tm + 1, tr);
        tree[v].mx = max(tree[v + v].mx, tree[v + v + 1].mx);
    }

    void add(int l, int r, ll val){
        add(l, r, val, 1, 1, size);
    }

    ll get(int l, int r, int v, int tl, int tr){
        if (l <= tl && tr <= r) return tree[v].mx;
        if (l > tr || r < tl) return 0;
        push(v);
        int tm = (tl + tr) >> 1;
        return max(get(l, r, v + v, tl, tm), get(l, r, v + v + 1, tm + 1, tr));
    }

    ll get(int l, int r){
        return get(l, r, 1, 1, size);
    }

    ll get(){
        return tree[1].mx;
    }

} ST;

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){
        tree.clear();
        dist.clear();
        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)));
    }

    int n = cycle.size();

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

    vector<ll> a = {arr[cycle[0]]};
    ll sum = 0;
    for (int i = 1; i < n; i++){
        sum += A(cycle[i - 1], cycle[i]);
        a.push_back(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;

    ST.init(a);

    ans = max(ans, arr[cycle[0]] + ST.get(2, n));

    int v, u;
    for (int i = 1; i < n; i++){
        v = cycle[i], u = cycle[i - 1];
        ST.add(1, n, -A(v, u));
        ST.add(i, i, sum);

        ans = max(ans, arr[v] + max(ST.get(1, i), ST.get(i + 2, n)));

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

    return ans;
}

void solve(int TC) {
    int n;
    cin >> n;

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

    for (int i = 1; i <= n; i++){
        cin >> f[i] >> w[i];
        parent[find(i)] = find(f[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;
        }
    }

    cout << 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 member function 'void SegTree::init(std::vector<long long int>&)':
islands.cpp:107:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |         while (size < a.size()) size *= 2;
      |                ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23884 KB Output is correct
2 Correct 14 ms 23940 KB Output is correct
3 Correct 14 ms 24012 KB Output is correct
4 Correct 14 ms 23936 KB Output is correct
5 Correct 14 ms 23940 KB Output is correct
6 Correct 14 ms 23940 KB Output is correct
7 Correct 14 ms 23884 KB Output is correct
8 Correct 14 ms 23884 KB Output is correct
9 Correct 14 ms 23924 KB Output is correct
10 Correct 14 ms 23876 KB Output is correct
11 Correct 13 ms 23952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 24012 KB Output is correct
2 Correct 14 ms 24020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23980 KB Output is correct
2 Correct 16 ms 24464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 25140 KB Output is correct
2 Correct 35 ms 27972 KB Output is correct
3 Correct 22 ms 25308 KB Output is correct
4 Correct 18 ms 24524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 29904 KB Output is correct
2 Correct 80 ms 32576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 37748 KB Output is correct
2 Correct 165 ms 48532 KB Output is correct
3 Correct 191 ms 62164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 209 ms 46740 KB Output is correct
2 Correct 267 ms 80224 KB Output is correct
3 Correct 452 ms 92256 KB Output is correct
4 Correct 443 ms 116804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 285 ms 82900 KB Output is correct
2 Runtime error 578 ms 131076 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 312 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -