From 34d7c4432fbf49e1803e88ed5950e67ca00aa132 Mon Sep 17 00:00:00 2001 From: Jonghwan Lee <123362165+0224LJH@users.noreply.github.com> Date: Mon, 4 Aug 2025 21:46:58 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[20250804]=20BOJ=20/=20G3=20/=20=EC=A0=84?= =?UTF-8?q?=EA=B5=AD=EC=8B=9C=EB=8C=80=20/=20=EC=9D=B4=EC=A2=85=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\352\265\255\354\213\234\353\214\200.md" | 137 ++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 "0224LJH/202508/4 BOJ \354\240\204\352\265\255\354\213\234\353\214\200.md" diff --git "a/0224LJH/202508/4 BOJ \354\240\204\352\265\255\354\213\234\353\214\200.md" "b/0224LJH/202508/4 BOJ \354\240\204\352\265\255\354\213\234\353\214\200.md" new file mode 100644 index 00000000..f1d1b566 --- /dev/null +++ "b/0224LJH/202508/4 BOJ \354\240\204\352\265\255\354\213\234\353\214\200.md" @@ -0,0 +1,137 @@ +```java +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + + +public class Main { + + static final int WAR = 2; + static final int UNION = 1; + + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringBuilder sb = new StringBuilder(); + static int[] countryPower,parent; + static boolean[] isAlive; + static int countryCnt, recordCnt,ans; + static List ansList = new ArrayList<>(); + + public static void main(String[] args) throws IOException { + init(); + process(); + print(); + } + + private static void init() throws IOException { + StringTokenizer st = new StringTokenizer(br.readLine()); + countryCnt = Integer.parseInt(st.nextToken()); + recordCnt = Integer.parseInt(st.nextToken()); + countryPower = new int[countryCnt]; + parent = new int[countryCnt]; + isAlive = new boolean[countryCnt]; + Arrays.fill(isAlive, true); + + for (int i = 0; i < countryCnt; i++) { + countryPower[i] = Integer.parseInt(br.readLine()); + parent[i] = i; + } + } + + private static void process() throws IOException { + for (int i = 0; i < recordCnt; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + int order = Integer.parseInt(st.nextToken()); + int a = Integer.parseInt(st.nextToken())-1; + int b = Integer.parseInt(st.nextToken())-1; + + if (order == UNION) { + union(a, b); + } else{ + war(a,b); + } + } + + for (int i = 0; i < countryCnt; i++) { + if (!isAlive[i]) continue; + int iRoot = find(i); + if (iRoot != i) continue; + if (isAlive[iRoot]) { + ans++; + ansList.add(countryPower[iRoot]); + } + } + + + + } + + private static void war(int a, int b) { + int aRoot = find(a); + int bRoot = find(b); + + if (aRoot == bRoot) return; + + int aPower = countryPower[aRoot]; + int bPower = countryPower[bRoot]; + if (aPower > bPower) { + countryPower[aRoot] -= bPower; + countryPower[bRoot] = 0; + parent[bRoot] = aRoot; + isAlive[bRoot] = false; + } else if (aPower < bPower) { + countryPower[bRoot] -= aPower; + countryPower[aRoot] = 0; + parent[aRoot] = bRoot; + isAlive[aRoot] = false; + } else{ + countryPower[aRoot] = 0; + countryPower[bRoot] = 0; + isAlive[aRoot] = false; + isAlive[bRoot] = false; + } + + + } + + private static void union(int a, int b) { + int aRoot = find(a); + int bRoot = find(b); + + if (aRoot == bRoot) return; + + int aPower = countryPower[aRoot]; + int bPower = countryPower[bRoot]; + + if (aPower >= bPower) { + countryPower[aRoot] += bPower; + countryPower[bRoot] = 0; + parent[bRoot] = aRoot; + isAlive[bRoot] = false; + } else { + countryPower[bRoot] += aPower; + countryPower[aRoot] = 0; + parent[aRoot] = bRoot; + isAlive[aRoot] = false; + } + + } + + private static int find(int x){ + if ( parent[x] == x ) return x; + return parent[x] = find(parent[x]); + } + + private static void print() { + Collections.sort(ansList); + System.out.println(ansList.size()); + if (!ansList.isEmpty()) { + for (int i = 0; i < ansList.size(); i++) { + System.out.print(ansList.get(i)); + if (i < ansList.size() - 1) System.out.print(" "); + } + System.out.println(); + } + } +} +``` \ No newline at end of file From 0692077cac04879f9fd9dbbe5a0a69639512b284 Mon Sep 17 00:00:00 2001 From: Jonghwan Lee <123362165+0224LJH@users.noreply.github.com> Date: Tue, 5 Aug 2025 23:41:23 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[20250805]=20BOJ=20/=20G5=20/=20=EC=86=8C?= =?UTF-8?q?=EC=88=98=20=ED=99=94=ED=8F=90=20/=20=EC=9D=B4=EC=A2=85?= =?UTF-8?q?=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\354\210\230 \355\231\224\355\217\220.md" | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 "0224LJH/202508/5 BOJ \354\206\214\354\210\230 \355\231\224\355\217\220.md" diff --git "a/0224LJH/202508/5 BOJ \354\206\214\354\210\230 \355\231\224\355\217\220.md" "b/0224LJH/202508/5 BOJ \354\206\214\354\210\230 \355\231\224\355\217\220.md" new file mode 100644 index 00000000..3fe6bfdb --- /dev/null +++ "b/0224LJH/202508/5 BOJ \354\206\214\354\210\230 \355\231\224\355\217\220.md" @@ -0,0 +1,138 @@ +```java +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + + +public class Main { + + static final int WAR = 2; + static final int UNION = 1; + + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringBuilder sb = new StringBuilder(); + static int[] countryPower,parent; + static boolean[] isAlive; + static int countryCnt, recordCnt,ans; + static List ansList = new ArrayList<>(); + + public static void main(String[] args) throws IOException { + init(); + process(); + print(); + + } + + private static void init() throws IOException { + StringTokenizer st = new StringTokenizer(br.readLine()); + countryCnt = Integer.parseInt(st.nextToken()); + recordCnt = Integer.parseInt(st.nextToken()); + countryPower = new int[countryCnt]; + parent = new int[countryCnt]; + isAlive = new boolean[countryCnt]; + Arrays.fill(isAlive, true); + + for (int i = 0; i < countryCnt; i++) { + countryPower[i] = Integer.parseInt(br.readLine()); + parent[i] = i; + } + } + + private static void process() throws IOException { + for (int i = 0; i < recordCnt; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + int order = Integer.parseInt(st.nextToken()); + int a = Integer.parseInt(st.nextToken())-1; + int b = Integer.parseInt(st.nextToken())-1; + + if (order == UNION) { + union(a, b); + } else{ + war(a,b); + } + } + + for (int i = 0; i < countryCnt; i++) { + if (!isAlive[i]) continue; + int iRoot = find(i); + if (iRoot != i) continue; + if (isAlive[iRoot]) { + ans++; + ansList.add(countryPower[iRoot]); + } + } + + + + } + + private static void war(int a, int b) { + int aRoot = find(a); + int bRoot = find(b); + + if (aRoot == bRoot) return; + + int aPower = countryPower[aRoot]; + int bPower = countryPower[bRoot]; + if (aPower > bPower) { + countryPower[aRoot] -= bPower; + countryPower[bRoot] = 0; + parent[bRoot] = aRoot; + isAlive[bRoot] = false; + } else if (aPower < bPower) { + countryPower[bRoot] -= aPower; + countryPower[aRoot] = 0; + parent[aRoot] = bRoot; + isAlive[aRoot] = false; + } else{ + countryPower[aRoot] = 0; + countryPower[bRoot] = 0; + isAlive[aRoot] = false; + isAlive[bRoot] = false; + } + + + } + + private static void union(int a, int b) { + int aRoot = find(a); + int bRoot = find(b); + + if (aRoot == bRoot) return; + + int aPower = countryPower[aRoot]; + int bPower = countryPower[bRoot]; + + if (aPower >= bPower) { + countryPower[aRoot] += bPower; + countryPower[bRoot] = 0; + parent[bRoot] = aRoot; + isAlive[bRoot] = false; + } else { + countryPower[bRoot] += aPower; + countryPower[aRoot] = 0; + parent[aRoot] = bRoot; + isAlive[aRoot] = false; + } + + } + + private static int find(int x){ + if ( parent[x] == x ) return x; + return parent[x] = find(parent[x]); + } + + private static void print() { + Collections.sort(ansList); + System.out.println(ansList.size()); + if (!ansList.isEmpty()) { + for (int i = 0; i < ansList.size(); i++) { + System.out.print(ansList.get(i)); + if (i < ansList.size() - 1) System.out.print(" "); + } + System.out.println(); + } + } +} +``` \ No newline at end of file