-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContest913B.java
More file actions
85 lines (65 loc) · 1.76 KB
/
Copy pathContest913B.java
File metadata and controls
85 lines (65 loc) · 1.76 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author rn-sshawish
*/
public class Contest913B {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
Tree tree = new Tree(n);
for (int i = 2; i <= n; i++) {
tree.add(input.nextInt(), i);
}
System.out.println(tree.bfs(1)?"Yes":"No");
}
}
class Tree {
Node [] nodes;
public Tree(int n) {
nodes = new Node[n+1];
init();
}
public void init(){
for (int i = 1; i < nodes.length; i++) {
nodes[i] = new Node(i);
}
}
public void add(int index,int edge){
nodes[index].edge.add(edge);
}
public boolean bfs (int root){
Queue<Integer> data = new LinkedList<>();
data.add(root);
while (!data.isEmpty()) {
int x = data.poll();
int size = nodes[x].edge.size();
for (Integer object : nodes[x].edge) {
if (!nodes[object].edge.isEmpty()) {
size--;
}
data.add(object);
}
if (size < 3 && nodes[x].edge.size()!= 0) {
return false;
}
}
return true;
}
}
class Node {
int value;
ArrayList<Integer> edge;
public Node(int value) {
this.value = value;
this.edge = new ArrayList<>();
}
}