Gracias chicos. Ya lo tengo claro mas o menos.
Dash bulter esta en lo correcto, yo me explique mal por lo cual lo siento
Aqui esta lo que habia que hacer:
Cita: We are given a tree of N nodes, each containing a distinct integer number (between 1 and 2147483640,
inclusive) and optionally a set of descendent nodes. Write a program that finds a path from some leaf of
the tree to another (different) leaf of the tree with maximal sum of its nodes and prints this sum.
Input
The input data should be read from the console.
The first input line contains N - the number of nodes in the tree.
At the next N-1 lines there are pairs of numbers in format (p1 <- p2) each meaning that node p1 is
parent of the node p2. See the example bellow.
The input data will always be valid and in the described format. There is no need to check it explicitly.
Output
The output data should be printed on the console.
At the only output line you should print the maximal sum of nodes found.
Constraints
N will be between 2 and 3000, inclusive.
Allowed working time for your program: 0.80 seconds.
Allowed memory: 16 MB
Y la solucion dada ( bueno es en C# )
Código C#:
Ver originalprivate static int FindLongestPath(Node<int> root)
{
if (root.Children.Count == 0)
{
return 0;
}
int maxPath = 0;
foreach (var node in root.Children)
{
maxPath = Math.Max(maxPath, FindLongestPath(node));
}
return maxPath + 1;
}
private static List<Node<int>> FindAllLeafs(Node<int>[] nodes)
{
List<Node<int>> leafs = new List<Node<int>>();
foreach (var node in nodes)
{
if (node.Children.Count == 0)
{
leafs.Add(node);
}
}
return leafs;
}
static void Main(string[] args)
{
var N = int.Parse(Console.ReadLine());
var nodes = new Node<int>[N];
for (int i = 0; i < N; i++)
{
nodes[i] = new Node<int>(i);
}
for (int i = 1; i <= N - 1; i++)
{
string edgeAsString = Console.ReadLine();
var edgeParts = edgeAsString.Split(' ');
int parentId = int.Parse(edgeParts[0]);
int childId = int.Parse(edgeParts[1]);
nodes[parentId].Children.Add(nodes[childId]);
nodes[childId].HasParent = true;
}
....
}
Que prácticamente es lo que dijo bulter pero en C#, siento no haber me explicado mejor
Saludos