結果

問題 No.3103 Butterfly Effect
ユーザー t98slider
提出日時 2025-04-11 23:29:03
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 6,342 bytes
コンパイル時間 4,736 ms
コンパイル使用メモリ 274,236 KB
実行使用メモリ 95,384 KB
最終ジャッジ日時 2025-04-11 23:29:38
合計ジャッジ時間 27,506 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 38 WA * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;

template <class S, S (*op)(S, S), S (*e)()> class LinkCutTree {
    struct Node {
        Node *l, *r, *p;
        int id;
        S key, fsum, bsum;
        bool rev;

        explicit Node(const S &v) : key(v), fsum(v), bsum(v), rev(false), l(nullptr), r(nullptr), p(nullptr) {}
        bool is_root() const { return not p or (p->l != this and p->r != this); }
    };

    void update(Node* t) {
        t->fsum = t->key;
        t->bsum = t->key;
        if(t->l) {
            t->fsum = op(t->l->fsum, t->fsum);
            t->bsum = op(t->bsum, t->l->bsum);
        }
        if(t->r) {
            t->fsum = op(t->fsum, t->r->fsum);
            t->bsum = op(t->r->bsum, t->bsum);
        }
    }

    void rotate_right(Node* t) {
        Node *x = t->p, *y = x->p;
        if((x->l = t->r)) t->r->p = x;
        t->r = x, x->p = t;
        update(x), update(t);
        if((t->p = y)) {
            if(y->l == x) y->l = t;
            if(y->r == x) y->r = t;
            update(y);
        }
    }

    void rotate_left(Node* t) {
        Node *x = t->p, *y = x->p;
        if((x->r = t->l)) t->l->p = x;
        t->l = x, x->p = t;
        update(x), update(t);
        if((t->p = y)) {
            if(y->l == x) y->l = t;
            if(y->r == x) y->r = t;
            update(y);
        }
    }

    void toggle(Node* t) {
        std::swap(t->l, t->r);
        std::swap(t->fsum, t->bsum);
        t->rev ^= true;
    }

    void push(Node* t) {
        if(t->rev) {
            if(t->l) toggle(t->l);
            if(t->r) toggle(t->r);
            t->rev = false;
        }
    }

    void splay(Node *t) {
        push(t);
        while(not t->is_root()) {
            Node* q = t->p;
            if(q->is_root()) {
                push(q), push(t);
                q->l == t ? rotate_right(t) : rotate_left(t);
            } else {
                Node* r = q->p;
                push(r), push(q), push(t);
                if(r->l == q) {
                    if(q->l == t) rotate_right(q), rotate_right(t);
                    else rotate_left(t), rotate_right(t);
                } else {
                    if(q->r == t) rotate_left(q), rotate_left(t);
                    else rotate_right(t), rotate_left(t);
                }
            }
        }
    }

    int N;

    public:
    std::vector<Node*> Node_info;
    LinkCutTree(int n) : LinkCutTree(std::vector<S>(n, e())) {}
    LinkCutTree(const std::vector<S> &vec) : N(vec.size()), Node_info(vec.size()){
        for(int i = 0; i < N; i++) {
            Node_info[i] = new Node(vec[i]);
            Node_info[i]->id = i;
        }
    }

    int expose(int v) {
        Node* t = Node_info[v];
        Node* rp = nullptr;
        for(Node* cur = t; cur; cur = cur->p) {
            splay(cur);
            cur->r = rp;
            update(cur);
            rp = cur;
        }
        splay(t);
        return rp->id;
    }

    void evert(int v) {
        expose(v);
        toggle(Node_info[v]);
        push(Node_info[v]);
    }

    void link(int child_id, int parent_id) {
        Node *child = Node_info[child_id], *parent = Node_info[parent_id];
        evert(child_id);
        expose(parent_id);
        if(child_id == parent_id || child->p) {
            throw std::runtime_error("child and parent must be different connected components");
        }
        child->p = parent;
        parent->r = child;
        update(parent);
    }

    void cut(int child_id) {
        Node *child = Node_info[child_id];
        expose(child_id);
        auto *parent = child->l;
        child->l = nullptr;
        parent->p = nullptr;
    }

    void cut(int child_id, int parent_id){
        evert(parent_id);
        expose(child_id);
        while(Node_info[child_id]->l){
            Node* child = Node_info[child_id];
            Node* parent = child->l;
            child->l = nullptr;
            parent->p = nullptr;
            update(child);
            child_id = parent->id;
        }
    }

    bool same(int u, int v) {
        expose(u), expose(v);
        return u == v or Node_info[u]->p;
    }

    int lca(int u, int v) {
        if(u == v) return u;
        expose(u);
        int lcav = expose(v);
        if(not Node_info[u]->p) return -1;
        return lcav;
    }

    const S &query(int u, int v) {
        evert(u);
        expose(v);
        return Node_info[v]->fsum;
    }

    void set(int pos, S v) {
        expose(pos);
        Node_info[pos]->key = v;
        update(Node_info[pos]);
    }
};
using S = pair<int,int>;
S op(S lhs, S rhs){
    return max(lhs, rhs);
}
S e(){
    return make_pair(0, 0);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, q;
    cin >> n >> q;
    vector<tuple<int,int,int,int>> edge(q);
    vector<tuple<int,int,int>> tb(q);
    for(int i = 0; i < q; i++){
        auto &&[t, u, v, idx] = edge[i];
        cin >> t >> u >> v;
        t--, u--, v--;
        idx = i;
        tb[t] = make_tuple(u, v, idx);
    }
    atcoder::dsu uf1(n);
    LinkCutTree<S, op, e> lct(n + q);
    int cur = n;
    vector<vector<pair<int,int>>> add(q);
    for(int i = 0; i < q; i++){
        auto [u, v, idx] = tb[i];
        //cerr << u << " " << v << '\n';
        if(uf1.same(u, v)){
            auto w = lct.query(u, v);
            if(w.first < idx) continue;
            lct.cut(w.second);
            lct.set(cur, make_pair(idx, cur));
            lct.link(u, cur);
            lct.link(v, cur);
            cur++;
            add[idx].emplace_back(u, v);
            continue;
        }
        if(!uf1.same(0, u) && !uf1.same(0, v)) continue;
        if(uf1.same(0, u)){
            auto w = lct.query(0, u);
            //cerr << max(w.first, idx) << " " << u << " " << v << endl;
            add[max(w.first, idx)].emplace_back(u, v);
        }else{
            auto w = lct.query(0, v);
            //cerr << max(w.first, idx) << " " << u << " " << v << endl;
            add[max(w.first, idx)].emplace_back(u, v);
        }
        uf1.merge(u, v);
        lct.set(cur, make_pair(idx, cur));
        lct.link(u, cur);
        lct.link(v, cur);
        cur++;
    }
    atcoder::dsu uf(n);
    for(int i = 0; i < q; i++){
        for(auto [u, v] : add[i]) uf.merge(u, v);
        cout << uf.size(0) << '\n';
    }
}
0