From 19448392c565718d49076c47c31df39837584faa Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Thu, 2 Oct 2025 17:21:18 +0900 Subject: [PATCH] =?UTF-8?q?[20251002]=20PGM=20/=20Lv2=20/=20=EB=8F=84?= =?UTF-8?q?=EB=84=9B=EA=B3=BC=20=EB=A7=89=EB=8C=80=20=EA=B7=B8=EB=9E=98?= =?UTF-8?q?=ED=94=84=20/=20=EC=9D=B4=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0 \352\267\270\353\236\230\355\224\204.md" | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 "lkhyun/202510/02 PGM Lv2 \353\217\204\353\204\233\352\263\274 \353\247\211\353\214\200 \352\267\270\353\236\230\355\224\204.md" diff --git "a/lkhyun/202510/02 PGM Lv2 \353\217\204\353\204\233\352\263\274 \353\247\211\353\214\200 \352\267\270\353\236\230\355\224\204.md" "b/lkhyun/202510/02 PGM Lv2 \353\217\204\353\204\233\352\263\274 \353\247\211\353\214\200 \352\267\270\353\236\230\355\224\204.md" new file mode 100644 index 00000000..3f862c9a --- /dev/null +++ "b/lkhyun/202510/02 PGM Lv2 \353\217\204\353\204\233\352\263\274 \353\247\211\353\214\200 \352\267\270\353\236\230\355\224\204.md" @@ -0,0 +1,72 @@ +```java +import java.util.*; +class Solution { + static Map> adjList = new HashMap<>(); + static Map visited = new HashMap<>(); + static Set startCandidate = new TreeSet<>(); + static Set noStart = new TreeSet<>(); + static int[] answer = new int[4]; + + public int[] solution(int[][] edges) { + + for(int[] edge : edges){ + List temp; + if(adjList.containsKey(edge[0])){ + startCandidate.add(edge[0]); + temp = adjList.get(edge[0]); + }else{ + temp = new ArrayList<>(); + } + temp.add(edge[1]); + adjList.put(edge[0],temp); + visited.put(edge[0],false); + visited.put(edge[1],false); + } + for(int[] edge : edges){ + if(startCandidate.contains(edge[1])){ + noStart.add(edge[1]); + } + } + for(int start : startCandidate){ + if(!noStart.contains(start)){ + answer[0] = start; + break; + } + } + + for(int start : adjList.get(answer[0])){ + BFS(start); + } + return answer; + } + public void BFS(int start){ + ArrayDeque q = new ArrayDeque<>(); + q.offer(start); + visited.put(start,true); + + while(!q.isEmpty()){ + int cur = q.poll(); + if(adjList.get(cur) == null){ + answer[2]++; + return; + } + for(int next : adjList.get(cur)){ + if(adjList.get(next) == null){ + answer[2]++; + return; + } + if(adjList.get(next).size() == 2){ + answer[3]++; + return; + } + if(visited.get(next)){ + answer[1]++; + return; + } + q.offer(next); + visited.put(next,true); + } + } + } +} +```