Browse Source

更新 'Structure/自定义并查集模板.cpp'

更新添加init
ZZWX 1 year ago
parent
commit
d6391165ec
1 changed files with 14 additions and 10 deletions
  1. 14 10
      Structure/自定义并查集模板.cpp

+ 14 - 10
Structure/自定义并查集模板.cpp

@@ -1,11 +1,15 @@
-int f[/*size*/];
-int find(int x) {
-	return (f[x] == x) ? x : (f[x] = find(f[x]));
-}
-void merge(int x, int y) {
-	int fx = find(x), fy = find(y);
-	f[fx] = fy;
-}
-bool query(int x, int y) {
-	return find(x) == find(y);
+int f[/*size*/];
+void init(int n) {
+	for (int i = 1; i <= n; i++)
+		f[i] = i;
+}
+int find(int x) {
+	return (f[x] == x) ? x : (f[x] = find(f[x]));
+}
+void merge(int x, int y) {
+	int fx = find(x), fy = find(y);
+	f[fx] = fy;
+}
+bool query(int x, int y) {
+	return find(x) == find(y);
 }
 }