From b5ee8fd52bed2b5227c01b8aeb7f3b3fe2116d6a Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Thu, 13 Feb 2025 10:48:41 +0900 Subject: [PATCH] =?UTF-8?q?[20250213]=20BOJ=20/=20G4=20/=20=EC=99=95?= =?UTF-8?q?=EC=9C=84=20=EA=B3=84=EC=8A=B9=20/=20=EA=B9=80=EC=88=98?= =?UTF-8?q?=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\354\234\204 \352\263\204\354\212\271.md" | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 "suyeun84/202502/13 BOJ G4 \354\231\225\354\234\204 \352\263\204\354\212\271.md" diff --git "a/suyeun84/202502/13 BOJ G4 \354\231\225\354\234\204 \352\263\204\354\212\271.md" "b/suyeun84/202502/13 BOJ G4 \354\231\225\354\234\204 \352\263\204\354\212\271.md" new file mode 100644 index 00000000..8f77308a --- /dev/null +++ "b/suyeun84/202502/13 BOJ G4 \354\231\225\354\234\204 \352\263\204\354\212\271.md" @@ -0,0 +1,66 @@ +```java +import java.util.*; +import java.io.*; + +class Solution +{ + static ArrayList famList = new ArrayList<>(); + static ArrayList candidate = new ArrayList<>(); + static HashMap result = new HashMap<>(); + static double maxNum = 0; + static String answer = ""; + + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + int N = Integer.parseInt(st.nextToken()); + int M = Integer.parseInt(st.nextToken()); + String king = br.readLine().trim(); + + result.put(king, 1.0); + for (int i = 0; i < N; i++) { + st = new StringTokenizer(br.readLine()); + famList.add(new Family(st.nextToken().trim(), st.nextToken().trim(), st.nextToken().trim())); + } + for (int i = 0; i < M; i++) { + candidate.add(br.readLine().trim()); + } + + for (String can : candidate) { + double res = dfs(can); + if (maxNum < res) { + maxNum = res; + answer = can; + } + } + System.out.println(answer); + } + + public static double dfs(String name) { + if (!result.containsKey(name)) { + for (Family fam : famList) { + if (!fam.c.equals(name)) continue; + double res = 0.0; + res += (dfs(fam.p1)/2 + dfs(fam.p2)/2); + result.put(name, res); + break; + } + } + if (!result.containsKey(name)) result.put(name, 0.0); + return result.getOrDefault(name, 0.0); + } + + static class Family { + String c; + String p1; + String p2; + public Family(String c, String p1, String p2) { + this.c = c; + this.p1 = p1; + this.p2 = p2; + } + } +} + +```