Skip to content

Commit

Permalink
2024-05-30
Browse files Browse the repository at this point in the history
  • Loading branch information
mjj111 committed May 30, 2024
1 parent 66bb170 commit 03dcb75
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions mjj111/graph/μ–‘κ³ΌλŠ‘λŒ€.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.*;

class μ–‘κ³ΌλŠ‘λŒ€ {

int[][] edges;
int[] info;
int maxSheepCnt = 0;

public int solution(int[] Info, int[][] Edges) {
info = Info;
edges = Edges;
dfs(0, 0, 0, new HashSet<Integer>());
return maxSheepCnt;
}

private void dfs(int now, int sheepCnt, int wolfCnt, Set<Integer> nexts) {
if (info[now] == 0) sheepCnt++;
else wolfCnt++;

if (wolfCnt >= sheepCnt) return;
maxSheepCnt = Math.max(sheepCnt, maxSheepCnt);

// λ‹€μŒ 탐색 μœ„μΉ˜ 볡사
Set<Integer> copySet = new HashSet<>(nexts);

// λ‹€μŒ 탐색 λͺ©λ‘μ€‘ ν˜„μž¬ μœ„μΉ˜μ œμ™Έ
copySet.remove(now);

for(int[] edge : edges) {
int start = edge[0];
int end = edge[1];
if(start == now) copySet.add(end);
}

for (int next : copySet) {
dfs(next, sheepCnt, wolfCnt, copySet);
}
}
}

0 comments on commit 03dcb75

Please sign in to comment.