using System using System Collections Generic using System Linq using

 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
const int l = 6;
int [] P = new int[l]{-1, 0, 3, 0, 2, 3};
int [] R = new int[l];
int[] level = new int[l]{-2, -2, -2, -2, -2, -2};
for (int i = 0; i < l; i++)
{
Console.Write(" {0}", P[i]);
}
Console.WriteLine();
int head=7;
for (int i = 0; i < l; i++)
{
if (P[i] == -1)
{
head = i;
R[i] = 0;
break;
}
}
for(int i=0; i<l; i++)
{
if (i != head)
{
int temp = i;
int count = 0;
while (P[temp] != -1)
{
temp = P[temp];
count = count + 1;
}
R[i] = count;
}
}
for (int i = 0; i < l; i++)
{
Console.Write(" {0}",R[i]);
}
}
}
}