답안 #488737

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
488737 2021-11-20T09:50:48 Z dxz05 Islands (IOI08_islands) C++14
24 / 100
243 ms 131076 KB
#pragma GCC optimize("Ofast,O2,O3,unroll-loops")
#pragma GCC target("avx2")

#include <bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }

template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << "[" << H << "]";
    debug_out(T...);
}

#ifdef dddxxz
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

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

clock_t startTime;

double getCurrentTime() {
    return (double) (clock() - startTime) / CLOCKS_PER_SEC;
}

#define MP make_pair

typedef long long ll;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const double eps = 0.000001;
const int MOD = 998244353;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 1e6 + 3e2;
const int M = 222;

int f[N], w[N];
bool used[N];
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;
    }

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

void unite(int x, int y){
    x = find(x);
    y = find(y);
    if (rng() & 1) swap(x, y);
    parent[x] = y;
}

vector<int> comp[N];
vector<pair<int, int>> g[N];

bool incycle[N];

void dfs(int v, int p, ll d, int root, vector<int> &tree, vector<ll> &dist){
    tree.push_back(v);
    dist.push_back(d);
    for (auto now : g[v]){
        int u = now.first, W = now.second;
        if (u != p){
            if (u == root || !incycle[u]) dfs(u, v, d + W, root, tree, dist);
        }
    }
}

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){
        size = a.size();
        tree.assign(size * 6, 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;

map<int, int> A[N];

ll component(vector<int> vec){
    cycle.clear();
    while (!st.empty()) st.pop();

    dfs0(vec.front());

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

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

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

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

    int n = cycle.size();

    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;

    debug(sum);

    ST.init(a);

    ans = max(ans, ST.get());

    for (int i = 1; i < n; i++){
        int 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;

        ST.add(i, i, A[v][u]);
    }

    return ans;
}

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

    iota(parent + 1, parent + n + 1, 1);

    for (int i = 1; i <= n; i++){
        cin >> f[i] >> w[i];
        unite(i, f[i]);
        g[i].emplace_back(f[i], w[i]);
        g[f[i]].emplace_back(i, w[i]);
        A[i][f[i]] = max(A[i][f[i]], w[i]);
        A[f[i]][i] = max(A[f[i]][i], w[i]);
    }

    for (int i = 1; i <= n; i++){
        comp[find(i)].push_back(i);
    }

    ll ans = 0;
    for (int i = 1; i <= n; i++){
        if (!comp[i].empty()){
            ll cur = component(comp[i]);
            debug(i, cur);
            ans += cur;
        }
    }

    cout << ans;

}

int main() {
    startTime = clock();
    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);
    }

    cerr << endl << "Time: " << int(getCurrentTime() * 1000) << " ms" << endl;

    return 0;
}

Compilation message

islands.cpp: In function 'll component(std::vector<int>)':
islands.cpp:19:20: warning: statement has no effect [-Wunused-value]
   19 | #define debug(...) 42
      |                    ^~
islands.cpp:218:5: note: in expansion of macro 'debug'
  218 |     debug(sum);
      |     ^~~~~
islands.cpp: In function 'void solve(int)':
islands.cpp:19:20: warning: statement has no effect [-Wunused-value]
   19 | #define debug(...) 42
      |                    ^~
islands.cpp:262:13: note: in expansion of macro 'debug'
  262 |             debug(i, cur);
      |             ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 94208 KB Output is correct
2 Incorrect 49 ms 94336 KB Output isn't correct
3 Correct 47 ms 94444 KB Output is correct
4 Correct 46 ms 94188 KB Output is correct
5 Incorrect 45 ms 94236 KB Output isn't correct
6 Correct 46 ms 94240 KB Output is correct
7 Correct 45 ms 94276 KB Output is correct
8 Incorrect 45 ms 94308 KB Output isn't correct
9 Correct 45 ms 94284 KB Output is correct
10 Correct 49 ms 94356 KB Output is correct
11 Correct 45 ms 94292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 49 ms 94584 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 50 ms 94532 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 61 ms 97132 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 103 ms 106152 KB Output is correct
2 Incorrect 131 ms 114220 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 243 ms 131036 KB Output is correct
2 Runtime error 127 ms 131076 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 136 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 200 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 143 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -