Disjoint Set Union 模板

Dsu 内部节点下标从 1 开始. int Find(int u) 返回节点 u 的根节点 void Union(int u, int v) 合并两个节点所在的集合 之后可以加上带权并查集. #ifndef DSU_H #define DSU_H #include <cassert> #include <iostream> #include <sstream> #include <stdexcept> #include <vector> using namespace std; struct Dsu { int n{0}; vector<int> fa{}, sz{}; explicit Dsu(int n_) : n(n_), fa{vector<int>(n + 1)}, sz{fa} { if (n <= 0) { ostringstream output; output << "n<=0: " << "n=" << n; throw invalid_argument(output.str()); } for (int i = 1; i <= n; i++) { fa[i] = i; sz[i] = 1; } } int Find(int u) { assert(u >= 1 && u <= n); return u == fa[u] ? u : (fa[u] = Find(fa[u])); } void Union(int u, int v) { assert(u >= 1 && u <= n && v >= 1 && v <= n); int ru = Find(u), rv = Find(v); if (ru == rv) return; if (sz[ru] < sz[rv]) swap(ru, rv); fa[rv] = ru; sz[ru] += sz[rv]; } }; #endif

April 7, 2026 · 1 min · 158 words

CodeForces 1559D1 Mocha and Diana (Easy Version)

Mocha and Diana (Easy Version) 一个连通块中的两个点之间不能添加边,所以想到使用并查集维护集合。设第一个人为 $A$,第二个人是 $B$,那么 $A$ 中的两个点 $x, y$ 能够相连的条件是 $B$ 中的 $x, y$ 不在一个连通块中。如果在 $x, y$ 之间添加边,此时 $A, B$ 中的连通块数量都减少 $1$,当 $B$ 的连通块数量是 $1$ 的时候,就不能再添加边了。对于 $x, y$ 来说,其实换成对应的连通块中的其它符合条件的两个点都可以,它们是等价的,最终效果都一样。最终局面是 $B$ 只有一个连通块,$A$ 的连通块至少有一个。因此检查 $A$ 中的所有点对,能连的就放到答案中即可。 // Date: Wed Jan 17 19:55:47 2024 #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> VI; typedef pair<int, int> PII; template <class T> using pq = priority_queue<T>; template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>; const int INF = 0x3f3f3f3f, MOD = 1e9 + 7, MOD1 = 998'244'353; const ll INFL = 0x3f3f3f3f'3f3f3f3f; const double eps = 1e-8; const int dir[8][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}, }; const ull Pr = 131; #define For(i, a, b) for (int i = int(a); i < int(b); ++i) #define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i) #define For1(i, a, b) for (int i = int(a); i <= int(b); ++i) #define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i) #define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i]) #define f1 first #define f2 second #define pb push_back #define has(a, x) (a.find(x) != a.end()) #define nemp(a) (!a.empty()) #define all(a) (a).begin(), (a).end() #define SZ(a) int((a).size()) #define NL cout << '\n'; template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <typename t> istream &operator>>(istream &in, vector<t> &vec) { for (t &x : vec) in >> x; return in; } template <typename t> ostream &operator<<(ostream &out, vector<t> &vec) { int n = SZ(vec); For(i, 0, n) { out << vec[i]; if (i < n - 1) out << ' '; } return out; } void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #ifdef _DEBUG #define debug1(x) cout << #x " = " << x << endl; #define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl; #define debug3(x, y, z) \ cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl; #define dbg(x...) \ cerr << "\e[91m" << __func__ << ":" << __LINE__ << " [" << #x << "] = ["; \ _print(x); \ cerr << "\e[39m" << endl; #else #define debug1 #define debug2 #define debug3 #define dbg(x...) #endif const int N = 1100; int f[2][N], sz[2][N], n, m1, m2; void Init() { For1(i, 1, n) { f[0][i] = f[1][i] = i; sz[0][i] = sz[1][i] = 1; } } int Find(int i, int x) { if (f[i][x] == x) return x; return f[i][x] = Find(i, f[i][x]); } void Union(int i, int x, int y) { int rx = Find(i, x), ry = Find(i, y); if (rx == ry) return; f[i][ry] = rx; } void solve() { while (cin >> n >> m1 >> m2) { int u, v; Init(); For(i, 0, m1) { cin >> u >> v; Union(0, u, v); } For(i, 0, m2) { cin >> u >> v; Union(1, u, v); } vector<PII> res; For1(i, 1, n) { For1(j, i + 1, n) { int ri = Find(0, i), rj = Find(0, j); if (ri != rj) { int ri1 = Find(1, i), rj1 = Find(1, j); if (ri1 != rj1) { res.pb({i, j}); Union(1, i, j); Union(0, i, j); } } } } int len = SZ(res); cout << len << '\n'; if (len) { for (auto &[x, y] : res) { cout << x << ' ' << y << '\n'; } } } } int main(void) { #ifdef _DEBUG freopen("1559d1.in", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int T = 1; while (T--) { solve(); } return 0; }

January 17, 2024 · 5 min · 903 words