-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |