Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions lkhyun/202510/02 PGM Lv2 도넛과 막대 그래프.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
```java
import java.util.*;
class Solution {
static Map<Integer,List<Integer>> adjList = new HashMap<>();
static Map<Integer,Boolean> visited = new HashMap<>();
static Set<Integer> startCandidate = new TreeSet<>();
static Set<Integer> noStart = new TreeSet<>();
static int[] answer = new int[4];

public int[] solution(int[][] edges) {

for(int[] edge : edges){
List<Integer> 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<Integer> 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);
}
}
}
}
```