-
Notifications
You must be signed in to change notification settings - Fork 15
/
p310.java
59 lines (43 loc) · 1.28 KB
/
p310.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.ArrayList;
import java.util.Scanner;
/**
* @author Rubén Saiz
*/
class TreeNode {
ArrayList<TreeNode> nodes;
TreeNode() { nodes = new ArrayList<>(); }
}
public class p310 {
static int index;
static int altura;
static void insertNodes(TreeNode tree, String[] input) {
if (index == input.length) return;
int childs = Integer.parseInt(input[index++]);
for (int i = 0; i < childs; i++)
tree.nodes.add(new TreeNode());
for (TreeNode n : tree.nodes)
insertNodes(n, input);
}
static void getAltura(TreeNode tree, int temp) {
if (tree.nodes.isEmpty()) {
if (temp > altura) altura = temp;
return;
}
for (TreeNode n : tree.nodes)
getAltura(n, temp + 1);
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int casos = s.nextInt(); s.nextLine();
String[] input;
for (int i = 0; i < casos; i++) {
input = s.nextLine().split(" ");
TreeNode tree = new TreeNode();
index = 0;
insertNodes(tree, input);
altura = Integer.MIN_VALUE;
getAltura(tree, 1);
System.out.println( altura );
}
}
}