forked from rudi8848/data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.2.height1.cpp
More file actions
55 lines (48 loc) · 818 Bytes
/
1.2.height1.cpp
File metadata and controls
55 lines (48 loc) · 818 Bytes
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
#include <iostream>
#include <vector>
int main(void)
{
int n = 0;
int check = 0;
std::cin >> n ;
if (n < 1)
return 0;
std::vector<int> v(n);
std::vector<int> depth(n);
for (int i = 0; i < n; ++i)
depth[i] = 0;
for (int i = 0; i < v.size(); ++i)
{
std::cin >> v[i];
if (v[i] == -1)
depth[i] = 1;
}
for (int i = 0; i < v.size(); ++i)
{
int parent = v[i];
int height = 1;
if (parent == -1)
continue;
if (depth[parent])
height += depth[parent];
else
{
while (!depth[parent])
{
++height;
std::cout << ++check << std::endl;
parent = v[parent];
}
height += depth[parent];
}
depth[i] = height;
}
int max = depth[0];
for (int i = 0; i < depth.size(); ++i)
{
if (max < depth[i])
max = depth[i];
}
std::cout <<max << std::endl;
return 0;
}