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); + } + } + } +} +```