답안 #401587

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
401587 2021-05-10T14:07:13 Z timmyfeng Collapse (JOI18_collapse) C++17
0 / 100
125 ms 10552 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 100000, B = 256;

vector<array<int, 3>> queries[N];
vector<int> adj[N];
bool visited[N];
int dsu[N];

int find(int u) {
    if (dsu[u] < 0) {
        return u;
    } else {
        dsu[u] = find(dsu[u]);
        return dsu[u];
    }
}

bool unite(int u, int v) {
    u = find(u), v = find(v);
    if (u == v) {
        return false;
    }

    if (-dsu[u] > -dsu[v]) {
        swap(u, v);
    }

    dsu[v] += dsu[u];
    dsu[u] = v;
    return true;
}

void dfs(int u) {
    visited[u] = true;
    for (auto c : adj[u]) {
        if (!visited[c]) {
            dfs(c);
        }
    }
}

vector<int> simulateCollapse(int n, vector<int> t, vector<int> x, vector<int> y, vector<int> w, vector<int> p) {
    int m = t.size(), q = w.size();
    vector<int> ans(q, n);

    for (int k = 0; k < 2; ++k) {
        map<array<int, 2>, int> in;
        vector<array<int, 4>> edges;
        for (int i = 0; i < m; ++i) {
            if (x[i] < y[i]) {
                swap(x[i], y[i]);
            }

            if (t[i] == 0) {
                in[{x[i], y[i]}] = i;
            } else {
                edges.push_back({x[i], y[i], in[{x[i], y[i]}], i});
                in.erase({x[i], y[i]});
            }
        }

        for (auto [e, i] : in) {
            edges.push_back({e[0], e[1], i, m});
        }

        sort(edges.begin(), edges.end());

        for (int i = 0; i < q; ++i) {
            queries[w[i] - w[i] % B].push_back({p[i], w[i], i});
        }

        for (int i = 0; i < m; i += B) {
            sort(queries[i].begin(), queries[i].end());

            vector<array<int, 4>> partial;
            fill(dsu, dsu + n, -1);
            int count = 0;

            int j = 0;
            for (auto [l, s, id] : queries[i]) {
                while (j < (int) edges.size() && edges[j][0] <= l) {
                    auto [u, v, a, b] = edges[j++];
                    if (a <= i && min(m, i + B) <= b) {
                        count += unite(u, v);
                    } else if (!(b <= i || min(m, i + B) <= a)) {
                        partial.push_back({u, v, a, b});
                    }
                }

                vector<int> nodes;
                for (auto [u, v, a, b] : partial) {
                    if (a <= s && s < b) {
                        u = find(u), v = find(v);
                        adj[u].push_back(v);
                        adj[v].push_back(u);
                        nodes.push_back(u);
                        nodes.push_back(v);
                    }
                }

                for (auto u : nodes) {
                    if (!visited[u]) {
                        --ans[id];
                        dfs(u);
                    }
                }

                for (auto u : nodes) {
                    if (visited[u]) {
                        visited[u] = false;
                        adj[u].clear();
                        ++ans[id];
                    }
                }

                ans[id] += count;
            }        
        }

        for (int i = 0; i < m; ++i) {
            x[i] = n - 1 - x[i];
            y[i] = n - 1 - y[i];
        }

        for (int i = 0; i < q; ++i) {
            p[i] = n - 2 - p[i];
        }
    }

    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 5452 KB Output is correct
2 Incorrect 11 ms 5388 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 125 ms 10552 KB Output is correct
2 Incorrect 98 ms 10520 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 109 ms 10552 KB Output is correct
2 Incorrect 113 ms 10464 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 5452 KB Output is correct
2 Incorrect 11 ms 5388 KB Output isn't correct
3 Halted 0 ms 0 KB -