五一数据结构 day2

· · 个人记录

并查集

krusca

Envy(这题先咕着,没调出来)


//只有按秩合并的并查集可撤销 

#include <bits/stdc++.h>
#define gt getchar
#define pt putchar
using namespace std;
typedef long long ll;

const int MAXN = 1e5 + 5;

ll read() {
    ll x = 0, f = 1;char ch = gt();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1;ch = gt();}
    while (ch >= '0' && ch <= '9') {x *= 10;x += ch - '0';ch = gt();}
    return x * f;
}

ll fa[1000005], sz[1000005];
vector<ll> oper; 
ll find (ll now) {
    return fa[now] == now ? now : find(fa[now]);
}

// 按秩合并
// 此时find 复杂度为logn 
void merge(ll a, ll b) {
    a = find(a), b = find(b);
    if(siz[a] > sz[b]) {
        swap(a, b);
    }
    oper.push_back(a);
    f[a] = b;
    siz[b] += siz[a];
}

void rollback() {
    //回滚 
    ll now = oper.back();
    siz[fa[now]] -= siz[now];
    fa[now] = now;
}

int main() {
    //初始化 O(n)
    iota(fa, fa + 1000000, 0);
    fill(siz, siz+100000, 1);
    return 0;
}

星球大战

#include <bits/stdc++.h>
#define gt getchar
#define pt putchar
using namespace std;
typedef long long ll;

const int MAXN = 4e5 + 5;

ll read() {
    ll x = 0, f = 1;char ch = gt();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1;ch = gt();}
    while (ch >= '0' && ch <= '9') {x *= 10;x += ch - '0';ch = gt();}
    return x * f;
}

ll n, m;
vector<ll> z[MAXN];
ll a[MAXN];
bool f[MAXN];
ll fa[MAXN];
ll ans[MAXN];

ll find(ll x) {
    return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void merge(ll A, ll B) {
    fa[find(A)] = find(B);
}

int main() {
    ll n = read(), m = read();
    for (int i = 1; i <= m; i++) {
        ll x = read(), y = read();
        z[x].push_back(y);
        z[y].push_back(x);
    }
    ll k = read();
    for (int i = 1; i <= k; i++) {
        a[i] = read();
        f[a[i]] = true;
    }
    for (int i = 1; i <= n; i++) fa[i] = i;
    ll nowres = n - k;//没有被摧毁的点
    for (int i = 1; i <= n; i++) {
        if(!f[i]) {
            for (int j = 0; j < z[i].size(); j++) {
                ll s = i, e = z[i][j];
                if(!f[e] && find(s) != find(e)) {
                    nowres--;
                    merge(s, e);
                }
            }
        }
    }
    ans[k + 1] = nowres;
    for (int i = k; i >= 1; i--) {
        ll s = a[i];
        nowres++;//多了一个没被摧毁的点
        f[s] = false;
        for (int j = 0; j < z[s].size(); j++) {
            ll e = z[s][j];
            if(!f[e] && find(s) != find(e)) {
                nowres--;
                merge(s, e);
            }
        }
        ans[i] = nowres;
    }
    for (int i = 1; i <= k + 1; i ++) {
        cout << ans[i] << '\n';
    }
    return 0;
}

团伙

#include <bits/stdc++.h>
#define gt getchar
#define pt putchar
using namespace std;
typedef long long ll;

const int MAXN = 1e5 + 5;

ll read() {
    ll x = 0, f = 1;char ch = gt();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1;ch = gt();}
    while (ch >= '0' && ch <= '9') {x *= 10;x += ch - '0';ch = gt();}
    return x * f;
}

ll fa[MAXN];

ll find(ll x) {
    return fa[x] == x ? x : fa[x] = find(fa[x]);
}

int main() {
    ll n = read(), m = read();
    for (int i = 1; i <= n + n; i++) {
        fa[i] = i;
    }
    while(m--) {
        char c;
        ll p, q;
        cin >> c >> p >> q;
        if(c == 'F') {
            fa[find(p)] = find(q);
        }
        else {
            fa[find(q + n)] = find(p);
            fa[find(p + n)] = find(q);
        }
    }
    ll res = 0;
    for (int i = 1; i <= n; i++) {
        if(fa[i] == i) ++res;
    }
    cout << res;
    return 0;
}

带权并查集

群论没学过,看了几个大佬的 blog 愣是没看懂一点。

银河英雄传说

#include <bits/stdc++.h>
#define gt getchar
#define pt putchar
using namespace std;
typedef long long ll;

const int MAXN = 300005;

ll read() {
    ll x = 0, f = 1;char ch = gt();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1;ch = gt();}
    while (ch >= '0' && ch <= '9') {x *= 10;x += ch - '0';ch = gt();}
    return x * f;
}

ll fa[MAXN], front[MAXN], len[MAXN];
//front[i]表示i与队首的距离
//len[i]表示i所属的队列长度 

ll find(ll x) {
    if(fa[x] == x) return x;
    ll fx = find(fa[x]);
    front[x] += front[fa[x]];
    return fa[x] = fx;
}

int main() {
    ll n = read();
    for (int i = 1; i <= 300000; i++) {
        fa[i] = i;
        front[i] = 0;
        len[i] = 1;
    }
    while(n--) {
        char c;
        ll p, q;
        cin >> c >> p >> q;
        ll fp = find(p);
        ll fq = find(q);
        if(c == 'M') {
            front[fp] += len[fq];
            fa[fp] = fq;
            len[fq] += len[fp];
            len[fp] = 0;
        }
        else {
            if(fp != fq) cout << "-1\n";
            else cout << abs(front[p] - front[q]) - 1 << '\n';
        }
    }
    return 0;
}

Almost Union-Find


#include <bits/stdc++.h>
#define gt getchar
#define pt putchar
using namespace std;
typedef long long ll;

const int MAXN = 1e5 + 5;

ll read() {
    ll x = 0, f = 1;char ch = gt();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1;ch = gt();}
    while (ch >= '0' && ch <= '9') {x *= 10;x += ch - '0';ch = gt();}
    return x * f;
}

ll fa[MAXN], siz[MAXN], sum[MAXN];

ll find(ll x) {
    return fa[x] == x ? x : fa[x] = find(fa[x]);
}

int main() {
    ll n, m;
    while(scanf("%lld%lld", &n, &m) != EOF) {
        for (int i = 1; i <= n; i++){
            fa[i] = i + n;
            fa[i + n] = i + n;
            siz[i + n] = 1;
            sum[i + n] = i;
        }
        while(m--) {
            ll op = read();
            if(op == 1) {
                ll p = read(), q = read();
                ll fp = find(p), fq = find(q);
                if(fp != fq) {
                    fa[fp] = fq;
                    sum[fq] += sum[fp];
                    siz[fq] += siz[fp];
                }
            }
            else if(op == 2) {
                ll p = read(), q = read();
                ll fp = find(p), fq = find(q);
                if(fp != fq) {
                    fa[p] = fq;
                    siz[fp]--;
                    siz[fq]++;
                    sum[fp] -= p;
                    sum[fq] += p;
                }
            }
            else {
                ll p = read();
                cout << siz[find(p)] << ' ' << sum[find(p)] << '\n';
            }
        }
    }

    return 0;
}