Ver Mensaje Individual
  #39 (permalink)  
Antiguo 08/06/2013, 18:16
XpoZed
 
Fecha de Ingreso: junio-2013
Mensajes: 7
Antigüedad: 10 años, 11 meses
Puntos: 1
Respuesta: Buscar el camino mas largo

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 original
  1. private static int FindLongestPath(Node<int> root)
  2.         {
  3.             if (root.Children.Count == 0)
  4.             {
  5.                 return 0;
  6.             }
  7.  
  8.             int maxPath = 0;
  9.             foreach (var node in root.Children)
  10.             {
  11.                 maxPath = Math.Max(maxPath, FindLongestPath(node));
  12.             }
  13.  
  14.             return maxPath + 1;
  15.         }
  16.  
  17. private static List<Node<int>> FindAllLeafs(Node<int>[] nodes)
  18.         {
  19.             List<Node<int>> leafs = new List<Node<int>>();
  20.  
  21.             foreach (var node in nodes)
  22.             {
  23.                 if (node.Children.Count == 0)
  24.                 {
  25.                     leafs.Add(node);
  26.                 }
  27.             }
  28.  
  29.             return leafs;
  30.         }
  31.  
  32. static void Main(string[] args)
  33.         {
  34.  var N = int.Parse(Console.ReadLine());
  35.             var nodes = new Node<int>[N];
  36.  
  37.             for (int i = 0; i < N; i++)
  38.             {
  39.                 nodes[i] = new Node<int>(i);
  40.             }
  41.  
  42.             for (int i = 1; i <= N - 1; i++)
  43.             {
  44.                 string edgeAsString = Console.ReadLine();
  45.                 var edgeParts = edgeAsString.Split(' ');
  46.  
  47.                 int parentId = int.Parse(edgeParts[0]);
  48.                 int childId = int.Parse(edgeParts[1]);
  49.  
  50.                 nodes[parentId].Children.Add(nodes[childId]);
  51.                 nodes[childId].HasParent = true;
  52.             }
  53. ....
  54. }

Que prácticamente es lo que dijo bulter pero en C#, siento no haber me explicado mejor
Saludos