diff --git a/code_study/Baekjoon/java/_1043.java b/code_study/Baekjoon/java/_1043.java new file mode 100644 index 0000000..264fe3c --- /dev/null +++ b/code_study/Baekjoon/java/_1043.java @@ -0,0 +1,77 @@ +import java.util.Scanner; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.stream.IntStream; + +class UnionFind { + private int [] data; + + UnionFind(int N) { + this.data = IntStream.rangeClosed(0, N).toArray(); + } + + public int find(int x) { + if(this.data[x] != x) { + return this.data[x] = this.find(this.data[x]); + } + + return this.data[x]; + } + + public void union(int a, int b) { + int rootA = this.find(a); + int rootB = this.find(b); + + if(rootA != rootB) { + if(rootA < rootB) this.data[rootB] = rootA; + else this.data[rootA] = rootB; + } + } + + public void setRoot(int index, int root) { + this.data[index] = root; + } +} + +public class _1043 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + int N = sc.nextInt(); + UnionFind uf = new UnionFind(N); + + int M = sc.nextInt(); + + int truthNum = sc.nextInt(); + int[] truthGroup = new int[truthNum]; + for(int i=0; i[] partyInfo = new ArrayList[M]; // 2중 ArrayList로 선언해도 되지만 제네릭 배열타입 연습을 위해 이 방법을 이용함. + for(int i=0; i(); + int personNum = sc.nextInt(); + for(int j=0; j party : partyInfo) { + for(int i=1; i party : partyInfo) { + if(uf.find(party.get(0)) != 0) canLie++; + } + + System.out.println(canLie); + } +} \ No newline at end of file