BNUOJ 51275 道路修建(并查集)

题意:

Gn1n
m1m
0 u v:(u,v)G
1 u v:(u,v)kkuv0
Small:n103,m5×103
Large:n105,m5×105

分析:

Small,,1
k,,,
O(nm)

代码:

//
// Created by TaoSama on 2016-02-03
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
#include <cassert>

using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

struct DSU {
    int n, p[N], r[N];
    void init(int _n) {
        n = _n;
        for(int i = 1; i <= n; ++i) p[i] = i, r[i] = 0;
    }
    int find(int x) {
        return p[x] = p[x] == x ? x : find(p[x]);
    }
    bool unite(int x, int y) {
        x = find(x), y = find(y);
        if(x == y) return false;
        if(r[x] < r[y]) p[x] = y;
        else {
            p[y] = x;
            if(r[x] == r[y]) ++r[x];
        }
        return true;
    }
    bool same(int x, int y) {
        x = find(x), y = find(y);
        return x == y;
    }
} dsu, tmp;

int n, m;

int main() {
#ifdef LOCAL
    freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    int t; scanf("%d", &t);
    while(t--) {
        scanf("%d%d", &n, &m);
        dsu.init(n);
        int last = 0, ans = n;
        vector<pair<pair<int, int>, int> > Q;
        for(int q = 1; q <= m; ++q) {
            int op, u, v; scanf("%d%d%d", &op, &u, &v);
            u ^= last, v ^= last;
// printf("op.... %d %d %d\n", op, u, v);
            if(op == 0) {
                Q.push_back(make_pair(make_pair(u, v), q));
                ans -= dsu.unite(u, v);
                printf("%d\n", ans);
                last = ans;
            } else {
                int ret = dsu.same(u, v);
                if(ret) {
                    tmp.init(n);
                    for(int i = 0; i < Q.size(); ++i) {
                        int x = Q[i].first.first, y = Q[i].first.second;
                        int k = Q[i].second;
                        tmp.unite(x, y);
                        if(tmp.same(u, v)) {
                            ret = k;
                            break;
                        }
                    }
                }
                printf("%d\n", ret);
                last = ret;
            }
        }
    }
    return 0;
}

Large:使O(mlog3n)

t(u,v)uvroot(u)root(v)tuvt
,,,
(u,v)uvuv
O(logn)O(mlogn)

代码:

//
// Created by TaoSama on 2016-02-03
// Copyright (c) 2016 TaoSama. All rights reserved.
//
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

int n, m;
int p[N], r[N], id[N];

int find(int x) {
    return p[x] == x ? x : find(p[x]);
}

int unite(int x, int y, int q) {
    x = find(x), y = find(y);
    if(x == y) return false;
    if(r[x] > r[y]) swap(x, y);
    p[x] = y;
    id[x] = q;
    if(r[x] == r[y]) ++r[y];
    return true;
}

int lca(int u, int v) {
    set<int> s;
    while(u != p[u]) s.insert(u), u = p[u];
    s.insert(u);
    while(!s.count(v) && v != p[v]) v = p[v];
    if(!s.count(v)) return 0;
    return v;
}

int query(int x, int y) {
    int _lca = lca(x, y);
// printf("u: %d v: %d lca: %d\n", x, y, _lca);
    if(!_lca) return 0;
    int ret = 0;
    while(x != _lca) ret = max(ret, id[x]), x = p[x];
    while(y != _lca) ret = max(ret, id[y]), y = p[y];
    return ret;
}

int main() {
#ifdef LOCAL
    freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    int t; scanf("%d", &t);
    while(t--) {
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= n; ++i) p[i] = i, r[i] = 0;
        int ans = 0, cc = n;
        for(int i = 1; i <= m; ++i) {
            int op, x, y; scanf("%d%d%d", &op, &x, &y);
            x ^= ans, y ^= ans;
// printf("op..... %d %d %d\n", op, x, y);
            if(op == 0) {
                cc -= unite(x, y, i);
                ans = cc;
            } else {
                ans = query(x, y);
            }
            printf("%d\n", ans);
        }
    }
    return 0;
}
    原文作者:道路修建问题
    原文地址: https://blog.csdn.net/lwt36/article/details/50656426
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞