并查集
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 愣是没看懂一点。
银河英雄传说
-
表示 与本列队首的距离。
-
表示 所在列的长度。
-
然后就维护就好了。注意
find函数找祖先的同时要维护 。
#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;
}