From 195e83ecf8a9478264f021a55b709596b3a6ff8c Mon Sep 17 00:00:00 2001 From: oncsr Date: Sun, 31 Aug 2025 20:17:25 +0900 Subject: [PATCH] =?UTF-8?q?[20250831]=20BOJ=20/=20D5=20/=20=EA=B7=B8?= =?UTF-8?q?=EB=9E=98=ED=94=84=EC=99=80=20=EC=BF=BC=EB=A6=AC=20/=20?= =?UTF-8?q?=EA=B6=8C=ED=98=81=EC=A4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\354\231\200 \354\277\274\353\246\254.md" | 184 ++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 "khj20006/202508/31 BOJ D5 \352\267\270\353\236\230\355\224\204\354\231\200 \354\277\274\353\246\254.md" diff --git "a/khj20006/202508/31 BOJ D5 \352\267\270\353\236\230\355\224\204\354\231\200 \354\277\274\353\246\254.md" "b/khj20006/202508/31 BOJ D5 \352\267\270\353\236\230\355\224\204\354\231\200 \354\277\274\353\246\254.md" new file mode 100644 index 00000000..9285e590 --- /dev/null +++ "b/khj20006/202508/31 BOJ D5 \352\267\270\353\236\230\355\224\204\354\231\200 \354\277\274\353\246\254.md" @@ -0,0 +1,184 @@ +```java +import java.util.*; +import java.io.*; + +class IOController { + BufferedReader br; + BufferedWriter bw; + StringTokenizer st; + + public IOController() { + br = new BufferedReader(new InputStreamReader(System.in)); + bw = new BufferedWriter(new OutputStreamWriter(System.out)); + st = new StringTokenizer(""); + } + + String nextLine() throws Exception { + String line = br.readLine(); + st = new StringTokenizer(line); + return line; + } + + String nextToken() throws Exception { + while (!st.hasMoreTokens()) nextLine(); + return st.nextToken(); + } + + int nextInt() throws Exception { + return Integer.parseInt(nextToken()); + } + + long nextLong() throws Exception { + return Long.parseLong(nextToken()); + } + + double nextDouble() throws Exception { + return Double.parseDouble(nextToken()); + } + + void close() throws Exception { + bw.flush(); + bw.close(); + } + + void write(String content) throws Exception { + bw.write(content); + } + +} + +class DisjointSet { + int size; + int[] root, rank; + Stack works; + + DisjointSet(int size) { + this.size = size; + root = new int[size+1]; + rank = new int[size+1]; + for(int i=1;i<=size;i++) { + root[i] = i; + rank[i] = 1; + } + works = new Stack<>(); + } + + int find(int x) { return x == root[x] ? x : find(root[x]); } + + void union(int a, int b) { + int x = find(a), y = find(b); + if(x == y) { + works.push(new int[]{-1,-1,-1}); + return; + } + if(rank[x] < rank[y]) { + rank[y] += rank[x]; + root[x] = y; + works.add(new int[]{x, y, rank[x]}); + } + else { + rank[x] += rank[y]; + root[y] = x; + works.add(new int[]{y, x, rank[y]}); + } + } + + void rollback() { + if (!works.isEmpty()) { + int[] last = works.pop(); + int x = last[0], y = last[1], r = last[2]; + if(x == -1) return; + rank[y] -= r; + root[x] = x; + } + } + +} + +public class Main { + + static IOController io; + + // + + static int N, M; + static List[] lists; + static DisjointSet ds; + static List[] needAnswer; + + static void update(int s, int e, int l, int r, int n, int a, int b) { + if(l>r || l>e || r>1; + update(s,m,l,r,n*2,a,b); + update(m+1,e,l,r,n*2+1,a,b); + } + + static void clear(int s, int e, int n) throws Exception { + for(int[] edge : lists[n]) { + int a = edge[0], b = edge[1]; + ds.union(a,b); + } + if(s == e) { + for(int[] info : needAnswer[s]) { + int a = info[0], b = info[1]; + if(ds.find(a) == ds.find(b)) io.write("1\n"); + else io.write("0\n"); + } + for(int i=0;i>1; + clear(s,m,n*2); + clear(m+1,e,n*2+1); + for(int i=0;i(); + ds = new DisjointSet(N); + needAnswer = new List[M]; + for(int i=0;i(); + Map map = new TreeMap<>(); + for(int i=0;i b) { + int t = a; + a = b; + b = t; + } + if(op == 1) map.put(a+","+b, i); + else if(op == 2) { + update(0,M-1,map.get(a+","+b),i,1,a,b); + map.remove(a+","+b); + } + else { + needAnswer[i].add(new int[]{a,b}); + } + } + for(String key : map.keySet()) { + String[] words = key.split(","); + int a = Integer.parseInt(words[0]); + int b = Integer.parseInt(words[1]); + update(0,M-1,map.get(key),M,1,a,b); + } + + clear(0,M-1,1); + + io.close(); + + } + +} +```