Wacky_rubia

Aliens

Apr 7th, 2014
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Marsiane
  9. {
  10.     enum Color {White, Grey, Black}
  11.  
  12.     class Program
  13.     {
  14.         static int[][] Input()
  15.         {
  16.             var n = int.Parse(Console.ReadLine());
  17.             var martians = Enumerable.Range(0,n)
  18.                 .Select(x => Console.ReadLine().Split()
  19.                     .Select(y => int.Parse(y) - 1)
  20.                     .Where(y => y > -1)
  21.                     .ToArray())
  22.                 .ToArray();
  23.             return martians;
  24.         }
  25.  
  26.         static void Search(int[][] martians, Color[] colors, int v, Stack<int> stack)
  27.         {
  28.             colors[v] = Color.Grey;
  29.             foreach (var w in martians[v])
  30.             {
  31.                 if (colors[w] == Color.White)
  32.                 {
  33.                     Search(martians, colors, w, stack);
  34.                 }
  35.             }
  36.             colors[v] = Color.Black;
  37.             stack.Push(v);
  38.         }
  39.  
  40.         static void Main(string[] args)
  41.         {
  42.             var martians = Input();
  43.             var stack = new Stack<int>();
  44.             var colors = new Color[martians.Length];
  45.             for (int i = 0; i < martians.Length; i++)
  46.             {
  47.                 if (colors[i] == Color.White)
  48.                 {
  49.                     Search(martians, colors, i, stack);
  50.                 }
  51.             }
  52.             foreach(var e in stack)
  53.                 Console.Write("{0} ", e + 1);
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment