abc372E K-th Largest Connected Components

chenfy27發表於2024-10-07

有N個頂點的無向圖,最初沒有邊,接下來有Q組詢問,格式如下:

  • 1 u v:在頂點u和v之間加一條邊;
  • 2 x k: 問與頂點v連通的分量中,頂點編號第k大的是誰?如果不存在,輸出-1.
    1<=N,Q<=2E5,1<=u<v<=N, 1<=x<=N, 1<=k<=10

分析:由於k比較小,直接用vector維護連通分量的頂點集合,在合併時,如果頂點數超過k,則只取最大的k個。剩下的就是並查集模板操作。

#include <bits/stdc++.h>
using i64 = long long;

struct Dsu {
    void init(int n) {
        fa.resize(n + 1);
        has.resize(n + 1);
        for (int i = 1; i <= n; i++) {
            fa[i] = i;
            has[i].push_back(i);
        }
    }
    int leader(int x) {
        return x == fa[x] ? x : fa[x] = leader(fa[x]);
    }
    void join(int x, int y) {
        x = leader(x);
        y = leader(y);
        if (x != y) {
            if (has[x].size() < has[y].size()) {
                std::swap(x, y);
            }
            for (auto i : has[y]) {
                has[x].push_back(i);
            }
            std::sort(has[x].rbegin(), has[x].rend());
            if (has[x].size() > 10) {
                has[x].resize(10);
            }
            fa[y] = x;
        }
    }
    std::vector<int> fa;
    std::vector<std::vector<int>> has;
};

void solve() {
    int N, Q;
    std::cin >> N >> Q;
    Dsu dsu;
    dsu.init(N);
    for (int i = 1; i <= Q; i++) {
        int x, y, z;
        std::cin >> x >> y >> z;
        if (x == 1) {
            dsu.join(y, z);
        } else {
            int u = dsu.leader(y);
            if (z <= dsu.has[u].size()) {
                std::cout << dsu.has[u][z - 1] << "\n";
            } else {
                std::cout << "-1\n";
            }
        }
    }
}

int main() {
    std::cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    while (t--) solve();
    return 0;
}

如果k的範圍是[1,N],可以考慮用平衡樹來維護連通分量,需要用到啟發式合併。

#include <bits/stdc++.h>
using i64 = long long;

template <typename TYPE>
struct Treap {
    struct Node {
        TYPE data;
        int rnd, siz, dup, son[2];
        Node() { memset(&data, 0, sizeof(data)); rnd = siz = son[0] = son[1] = 0; }
        Node(const TYPE &d, int cnt=1) { init(d, cnt); }
        void init(const TYPE & d, int cnt) { data = d; dup = cnt; rnd = rand(); siz = 1; son[0] = son[1] = 0; }
    };
    Treap(int multi=1):multiple(multi) { reset(); }
    void setmulti(int multi) { multiple = multi; }
    int newnode(const TYPE & d, int cnt) {
        if (!reuse.empty()) {
            int r = reuse.front();
            reuse.pop_front();
            node[r].init(d, cnt);
            return r;
        }
        node.push_back({d, cnt});
        return node.size() - 1;
    }
    void reset() { root = 0; node.resize(1); reuse.clear(); }
    void maintain(int x) {
        node[x].siz = node[x].dup;
        if (node[x].son[0]) {
            node[x].siz += node[node[x].son[0]].siz;
        }
        if (node[x].son[1]) {
            node[x].siz += node[node[x].son[1]].siz;
        }
    }
    void rotate(int d, int &r) {
        int k = node[r].son[d^1];
        node[r].son[d^1] = node[k].son[d];
        node[k].son[d] = r;
        maintain(r);
        maintain(k);
        r = k;
    }
    void insert(const TYPE &data, int cnt, int &r) {
        if (r) {
            if (!(data < node[r].data) && !(node[r].data < data)) {
                if (multiple) {
                    node[r].dup += cnt;
                    maintain(r);
                }
            } else {
                int d = data < node[r].data ? 0 : 1;
                // avoid push_back -> realloc
                int u = node[r].son[d];
                insert(data, cnt, u);
                node[r].son[d] = u;
                if (node[node[r].son[d]].rnd > node[r].rnd) {
                    rotate(d^1, r);
                } else {
                    maintain(r);
                }
            }
        } else {
            r = newnode(data, cnt);
        }
    }
    TYPE kth(int k) {
        assert(1 <= k && k <= size());
        for (int r = root; r; ) {
            int x = node[r].son[0] ? node[node[r].son[0]].siz : 0;
            int y = node[r].dup;
            if (k <= x) {
                r = node[r].son[0];
            } else if (k <= x + y) {
                return node[r].data;
            } else {
                k -= x + y;
                r = node[r].son[1];
            }
        }
        assert(k == 0);
        return 0;
    }
    TYPE Kth(int k) {
        assert(1 <= k && k <= size());
        for (int r = root; r; ) {
            int x = node[r].son[1] ? node[node[r].son[1]].siz : 0;
            int y = node[r].dup;
            if (k <= x) {
                r = node[r].son[1];
            } else if (k <= x + y) {
                return node[r].data;
            } else {
                k -= x + y;
                r = node[r].son[0];
            }
        }
        assert(k == 0);
        return 0;
    }
    void erase(const TYPE& data, int cnt, int & r) {
        if (r == 0) return;
        int d = -1;
        if (data < node[r].data) {
            d = 0;
        } else if (node[r].data < data) {
            d = 1;
        }
        if (d == -1) {
            node[r].dup -= cnt;
            if (node[r].dup > 0) {
                maintain(r);
            } else {
                if (node[r].son[0] == 0) {
                    reuse.push_back(r);
                    r = node[r].son[1];
                } else if (node[r].son[1] == 0) {
                    reuse.push_back(r);
                    r = node[r].son[0];
                } else {
                    int dd = node[node[r].son[0]].rnd > node[node[r].son[1]].rnd ? 1 : 0;
                    rotate(dd, r);
                    erase(data, node[r].son[dd]);
                }
            }
        } else {
            erase(data, cnt, node[r].son[d]);
        }
        if (r) maintain(r);
    }
    int ltcnt(const TYPE &data) {
        int ans = 0;
        for (int r = root; r; ) {
            int x = node[r].son[0] ? node[node[r].son[0]].siz : 0;
            if (node[r].data < data) {
                ans += node[r].dup + x;
                r = node[r].son[1];
            } else if (data < node[r].data) {
                r = node[r].son[0];
            } else {
                ans += x;
                break;
            }
        }
        return ans;
    }
    int gtcnt(const TYPE &data) {
        int ans = 0;
        for (int r = root; r; ) {
            int x = node[r].son[1] ? node[node[r].son[1]].siz : 0;
            if (node[r].data < data) {
                r = node[r].son[1];
            } else if (data < node[r].data) {
                ans += node[r].dup + x;
                r = node[r].son[0];
            } else {
                ans += x;
                break;
            }
        }
        return ans;
    }
    int count(const TYPE &data) {
        for (int r = root; r; ) {
            if (data < node[r].data) {
                r = node[r].son[0];
            } else if (node[r].data < data) {
                r = node[r].son[1];
            } else {
                return node[r].dup;
            }
        }
        return 0;
    }
    std::pair<bool,TYPE> prev(const TYPE &data) {
        std::pair<bool,TYPE> ans = {false, 0};
        for (int r = root; r; ) {
            if (node[r].data < data) {
                if (ans.first) {
                    ans.second = std::max(ans.second, node[r].data);
                } else {
                    ans = {true, node[r].data};
                }
                r = node[r].son[1];
            } else {
                r = node[r].son[0];
            }
        }
        return ans;
    }       
    std::pair<bool,TYPE> next(const TYPE &data) {
        std::pair<bool,TYPE> ans = {false, 0};
        for (int r = root; r; ) {
            if (data < node[r].data) {
                if (ans.first) {
                    ans.second = std::min(ans.second, node[r].data);
                } else {
                    ans = {true, node[r].data};
                }
                r = node[r].son[0];
            } else {
                r = node[r].son[1];
            }
        }
        return ans;
    }
    void dofetch(int r, std::vector<std::pair<TYPE,int>> &vec) const {
        if (r) {
            dofetch(node[r].son[0], vec);
            vec.push_back({node[r].data, node[r].dup});
            dofetch(node[r].son[1], vec);
        }
    }
    void fetch(std::vector<std::pair<TYPE,int>> &vec) const {
        dofetch(root, vec);
    }
    void merge(const Treap<TYPE> &rhs) {
        std::vector<std::pair<TYPE,int>> vp;
        rhs.fetch(vp);
        for (auto &pr : vp) {
            insert(pr.first, pr.second);
        }
    }
    std::vector<Node> node;
    std::deque<int> reuse;
    int root, multiple;
    void insert(const TYPE& data, int cnt=1) { insert(data, cnt, root); }
    void erase(const TYPE& data, int cnt=1) { return erase(data, cnt, root); }
    int size() const { return root ? node[root].siz : 0; }
    int lecnt(const TYPE& data) { return size() - gtcnt(data, root); }
    int gecnt(const TYPE& data) { return size() - ltcnt(data, root); }
};

struct Dsu {
    void init(int n) {
        fa.resize(n + 1);
        has.resize(n + 1);
        for (int i = 1; i <= n; i++) {
            fa[i] = i;
            has[i].insert(i);
        }
    }
    int leader(int x) {
        return x == fa[x] ? x : fa[x] = leader(fa[x]);
    }
    void join(int x, int y) {
        x = leader(x);
        y = leader(y);
        if (x != y) {
            if (has[x].size() < has[y].size()) {
                std::swap(x, y);
            }
            has[x].merge(has[y]);
            fa[y] = x;
        }
    }
    std::vector<int> fa;
    std::vector<Treap<int>> has;
};

void solve() {
    int N, Q;
    std::cin >> N >> Q;
    Dsu dsu;
    dsu.init(N);
    for (int i = 1; i <= Q; i++) {
        int x, y, z;
        std::cin >> x >> y >> z;
        if (x == 1) {
            dsu.join(y, z);
        } else {
            int u = dsu.leader(y);
            if (z <= dsu.has[u].size()) {
                std::cout << dsu.has[u].Kth(z) << "\n";
            } else {
                std::cout << "-1\n";
            }
        }
    }
}

int main() {
    std::cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    while (t--) solve();
    return 0;
}

相關文章