μ λμ¨ νμΈλλ Disjoint Setλ₯Ό ννν λ μ¬μ©νλ κ·Έλν μκ³ λ¦¬μ¦μΌλ‘,
λ λ
Έλκ° κ°μ κ·Έλνμ μνλμ§ νλ³νλ μκ³ λ¦¬μ¦μ
λλ€.
λ
Έλλ₯Ό ν©μΉλ Union
μ°μ°κ³Ό, λ
Έλμ λ£¨νΈ λ
Έλλ₯Ό μ°Ύλ Find
μ°μ°μΌλ‘ μ΄λ£¨μ΄μ§λλ€.
π‘ Disjoint Set
Disjoint Setμ΄λ μλ‘μ μ§ν© μλ£κ΅¬μ‘°λ‘,
μλ‘ μ€λ³΅λμ§ μλ λΆλΆ μ§ν©λ€λ‘ λλ μ§ μμλ€μ λν μ 보λ₯Ό μ μ₯νκ³ μ‘°μνλ μλ£κ΅¬μ‘°μ
λλ€.
μ¦, κ³΅ν΅ μμκ° μλ μνΈ λ°°νμ μΈ λΆλΆ μ§ν©λ€λ‘ λλ μ§ μμλ€μ λν μλ£κ΅¬μ‘°μ
λλ€.
μ΅μ΄ λ Έλλ μκΈ° μμ μ λ£¨νΈ λ Έλλ‘ κ°μ§λλ‘ μ΄κΈ°νν©λλ€.
for (int i = 0; i < parent.length; i++)
parent[i] = i;
xκ° μν κ·Έλνμ root λ Έλλ₯Ό λ°ννλ ν¨μμ λλ€.
static int find(int x) {
if (x == parent[x]) return x; // xκ° λ£¨νΈλ
ΈλμΌ κ²½μ° λ°ν
int nx = find(parent[x]); // μ΅μμ λ
Έλλ₯Ό μ°Ύλ κ³Όμ μμ κ²½λ‘ μ΅μ ν
parent[x] = nx;
return nx;
}
κ²½λ‘ μ΅μ νλ₯Ό μ§ννμ§ μμ κ²½μ°, μ¬ν₯ νΈλ¦¬(Skewed Tree)μΌ λ μκ° λ³΅μ‘λλ O(n)
μ΄ λ©λλ€.
κ²½λ‘ μμΆμ νκ² λλ©΄, λ§€λ² νΈλ¦¬μ λμ΄κ° λ¬λΌμ§κ² λλ―λ‘ μκ° λ³΅μ‘λλ O(a(n))
μ΄ λ©λλ€.
>
μ¬κΈ°μ a(N)μ μμ»€λ§ ν¨μλ₯Ό μλ―Έν©λλ€.
μμ»€λ§ ν¨μλ μμ μ¬κ· ν¨μκ° μλ μ μμ μΈ μ¬κ· ν¨μμ
λλ€.
μ΄ κ°μ μ
λ ₯μ΄ μλλΌλ λ§€μ° λΉ λ₯΄κ² μ¦κ°νμ¬, Nμ΄ 2^65536μΌ λ, μμ»€λ§ ν¨μμ κ°μ 5κ° λλ―λ‘ μμμ μκ° λ³΅μ‘λλ₯Ό κ°μ§λ€κ³ λ΄λ 무방ν©λλ€.
xκ° μν κ·Έλνμ yκ° μν κ·Έλνλ₯Ό ν©μΉ©λλ€.
static void union(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
parent[y] = x
}
static boolean isSameParent(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return true;
else return false;
}
import java.io.*;
import java.util.*;
public class Main {
static int[] parent;
static int find(int x) {
if (parent[x] == x) return x;
return parent[x] = find(parent[x]);
}
static void union(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
else parent[y] = x;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
int M = Integer.parseInt(br.readLine());
int connect;
parent = new int[N + 1];
for (int i = 1; i <= N; i++) parent[i] = i;
for (int i = 1; i <= N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 1; j <= N; j++) {
connect = Integer.parseInt(st.nextToken());
if (connect == 1) union(i, j);
}
}
int[] path = new int[M];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < M; i++) path[i] = Integer.parseInt(st.nextToken());
boolean isAvailable = true;
int root = find(path[0]);
for (int i = 1; i < M; i++) {
if (root != find(path[i])) { isAvailable = false; break; }
}
if (isAvailable) System.out.println("YES");
else System.out.println("NO");
br.close();
}
}
λ°±μ€ 4195λ² μΉκ΅¬ λ€νΈμν¬
import java.io.*;
import java.util.*;
public class Main {
static Map<String, String> parent;
static Map<String, Integer> cnt;
static String find(String x) {
if (parent.get(x) == x) return x;
String nx = find(parent.get(x));
parent.put(x, nx);
return nx;
}
static void union(String x, String y) {
x = find(x);
y = find(y);
if (x == y) return;
parent.put(y, x);
cnt.put(x, cnt.get(x) + cnt.get(y));
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st;
int T = Integer.parseInt(br.readLine());
int N;
String x, y;
for (int t = 0; t < T; t++) {
parent = new HashMap<>();
cnt = new HashMap<>();
N = Integer.parseInt(br.readLine());
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
x = st.nextToken();
y = st.nextToken();
if (!parent.containsKey(x)) { parent.put(x, x); cnt.put(x, 1); }
if (!parent.containsKey(y)) { parent.put(y, y); cnt.put(y, 1); }
union(x, y);
sb.append(cnt.get(find(x))).append("\n");
}
}
System.out.println(sb);
br.close();
}
}