File tree Expand file tree Collapse file tree 1 file changed +56
-0
lines changed
Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class boj1068 {
6+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
7+ static StringTokenizer st;
8+ static void nextLine () throws Exception {st = new StringTokenizer (br .readLine ());}
9+ static int nextInt() {return Integer . parseInt(st. nextToken());}
10+
11+ public static void main(String [] args) throws Exception {
12+ nextLine();
13+ int N = nextInt();
14+ int answer = 0 ;
15+ ArrayList<ArrayList<Integer > > graph = new ArrayList<> ();
16+ nextLine();
17+ for (int i = 0 ; i < N ; i++ ) graph. add(new ArrayList<Integer > ());
18+ for (int i = 0 ; i < N ; i++ ) {
19+ int par = nextInt();
20+ if (par == - 1 ) continue ;
21+ graph. get(par). add(i); // 부모에 자식 연결
22+ }
23+ nextLine();
24+ int remove = nextInt();
25+ boolean [] poss = new boolean [N ];
26+ Arrays . fill(poss, true );
27+ Queue<Integer > q = new LinkedList<> ();
28+ poss[remove] = false ;
29+ for (int n : graph. get(remove)) {
30+ q. add(n);
31+ poss[n] = false ;
32+ }
33+ while (! q. isEmpty()) {
34+ int node = q. poll();
35+ for (int ne : graph. get(node)) {
36+ if (! poss[ne]) continue ;
37+ q. add(ne);
38+ poss[ne] = false ;
39+ }
40+ }
41+ for (int i = 0 ; i < N ; i++ ) {
42+ boolean flag = false ;
43+ if (! poss[i]) continue ;
44+ for (int p : graph. get(i)) {
45+ if (poss[p]) {
46+ flag = true ;
47+ break ;
48+ }
49+ }
50+ if (graph. get(i). isEmpty()) flag = false ;
51+ if (! flag) answer++ ;
52+ }
53+ System . out. println(answer);
54+ }
55+ }
56+ ```
You can’t perform that action at this time.
0 commit comments