File tree 1 file changed +39
-0
lines changed
1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+
3
+ private boolean [] visited ;
4
+ private int answer = 0 ;
5
+ private int [] info ;
6
+ private int [][] edges ;
7
+
8
+ public int solution (int [] _info , int [][] _edges ) {
9
+ answer = 0 ;
10
+ info = _info ;
11
+ edges = _edges ;
12
+ visited = new boolean [info .length +1 ];
13
+ visited [0 ] = true ;
14
+ dfs (1 , 0 );
15
+ return answer ;
16
+ }
17
+
18
+ public void dfs (int sheep , int wolf ) {
19
+ if (wolf < sheep ) {
20
+ answer = Math .max (answer , sheep );
21
+ }else {
22
+ return ;
23
+ }
24
+
25
+ for (int i =0 ; i <edges .length ; i ++) {
26
+ int parent = edges [i ][0 ];
27
+ int child = edges [i ][1 ];
28
+ if (visited [parent ] && !visited [child ]) {
29
+ visited [child ] = true ;
30
+ if (info [child ] == 0 ) {
31
+ dfs (sheep +1 , wolf );
32
+ }else {
33
+ dfs (sheep , wolf +1 );
34
+ }
35
+ visited [child ] = false ;
36
+ }
37
+ }
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments