-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1043.java
44 lines (41 loc) · 1.25 KB
/
1043.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.*;
import java.io.*;
public class Main {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static Scanner input = new Scanner(System.in);
static Queue<Integer> queue = new LinkedList<Integer>();
static boolean flag = false, complete = true;
static int n, node, now;
static int[][] tree = new int[1000001][3];
public static void main(String[] args) throws IOException {
in.nextToken();
n = (int) in.nval;
// n = input.nextInt();
for (int i = 1; i < n; i++) {
in.nextToken();
node = (int) in.nval;
// node = input.nextInt();
tree[i][0] = node;
if (tree[node][1] == 0) tree[node][1] = i;
else tree[node][2] = i;
}
if (tree[0][1] != 0) queue.offer(tree[0][1]);
if (tree[0][2] != 0) queue.offer(tree[0][2]);
while (!queue.isEmpty()) {
now = queue.poll();
if ((tree[now][1] == 0)&&(tree[now][2] == 0)) flag = true;
if (tree[now][1] != 0) {
queue.offer(tree[now][1]);
if (flag ==true) {
complete = false;
break;
}
}
if (tree[now][2] != 0) queue.offer(tree[now][2]);
}
System.out.println(complete);
}
}