Advertisement
Guest User

Untitled

a guest
May 18th, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace acmPC
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             int n = Int32.Parse(Console.ReadLine());
  13.  
  14.             string[] l = Console.ReadLine().Split(' ');
  15.  
  16.             Basket[] baskets = new Basket[l.Length];
  17.  
  18.             for (int i = 0; i < l.Length; i++)
  19.             {
  20.                 var s = l[i];
  21.                
  22.                 baskets[i] = new Basket();
  23.                 baskets[i].Apples = Int32.Parse(l[i]);
  24.             }
  25.  
  26.             for (int i = 0; i < n-1; i++)
  27.             {
  28.                 l = Console.ReadLine().Split(' ');
  29.  
  30.                 int x = Int32.Parse(l[0]) - 1;
  31.                 int y = Int32.Parse(l[1]) - 1;
  32.  
  33.                 baskets[y].AddChild(baskets[x]);
  34.             }
  35.  
  36.             Console.ReadLine();
  37.  
  38.             l = Console.ReadLine().Split(' ');
  39.             for (int i = 0; i < l.Length; i++)
  40.             {
  41.                 int Q = Int32.Parse(l[i]) - 1;
  42.  
  43.                 Console.Write(baskets[Q].AllApples + " ");
  44.             }
  45.  
  46.         }
  47.     }
  48.  
  49.     class Basket
  50.     {
  51.         public int Apples { get; set; }
  52.  
  53.         private int _allApples = -1;
  54.         public int AllApples
  55.         {
  56.             get
  57.             {
  58.                 if (_allApples == -1)
  59.                     _allApples = CountApples();
  60.  
  61.                 return _allApples;
  62.             }
  63.         }
  64.  
  65.         public List<Basket> Children { get; set; }
  66.  
  67.         public Basket()
  68.         {
  69.             Children = new List<Basket>();    
  70.         }
  71.  
  72.         public void AddChild(Basket child)
  73.         {
  74.             Children.Add(child);
  75.         }
  76.  
  77.         private int CountApples()
  78.         {
  79.             int p = Apples + Children.Sum(child => child.AllApples);
  80.  
  81.             return p;
  82.         }
  83.     }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement