예제 #1
0
 void Visit(Doll item, ref Dictionary <Doll, List <Doll> > visited, int numDolls)
 {
     if (!visited.ContainsKey(item) && numDolls > 0)
     {
         visited[item] = new List <Doll> {
             item
         };
         var dependencies = new List <Doll>(item.Children);
         dependencies.Sort();
         if (dependencies.Count != 0)
         {
             Dictionary <Doll, List <Doll> > children = new Dictionary <Doll, List <Doll> >();
             foreach (var dependency in dependencies)
             {
                 Visit(dependency, ref visited, numDolls - 1);
                 if (visited.ContainsKey(dependency) && visited[dependency].Count == numDolls - 1)
                 {
                     children[dependency] = visited[dependency];
                 }
             }
             if (children.Keys.Count != 0)
             {
                 visited[item].AddRange(children[children.Keys.First()]);
             }
         }
     }
 }
예제 #2
0
        public override void DoWork()
        {
            bool first = true;

            while (true)
            {
                List <Doll> nestedDolls = new List <Doll>();
                int         numDolls    = Scanner.NextInt();
                if (numDolls == 0)
                {
                    break;
                }


                for (int i = 0; i < 2 * numDolls; i++)
                {
                    int j       = 0;
                    var H       = Scanner.NextInt();
                    var D       = Scanner.NextInt();
                    var W       = Scanner.NextInt();
                    var newDoll = new Doll(H, D, W);

                    nestedDolls.Add(newDoll);
                }
                Solution solution = new Solution();


                IEnumerable <IEnumerable <Doll> > sets = solution.LongestChains(nestedDolls);
                if (!first)
                {
                    Console.WriteLine("");
                }
                first = false;
                int loop = 0;
                foreach (var set in sets)
                {
                    foreach (var doll in set)
                    {
                        Console.WriteLine("{0} {1} {2}", doll.H, doll.D, doll.W);
                    }

                    if (loop == 0)
                    {
                        Console.WriteLine("-");
                        loop++;
                    }
                }
            }
        }