Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Marsiane
- {
- enum Color {White, Grey, Black}
- class Program
- {
- static int[][] Input()
- {
- var n = int.Parse(Console.ReadLine());
- var martians = Enumerable.Range(0,n)
- .Select(x => Console.ReadLine().Split()
- .Select(y => int.Parse(y) - 1)
- .Where(y => y > -1)
- .ToArray())
- .ToArray();
- return martians;
- }
- static void Search(int[][] martians, Color[] colors, int v, Stack<int> stack)
- {
- colors[v] = Color.Grey;
- foreach (var w in martians[v])
- {
- if (colors[w] == Color.White)
- {
- Search(martians, colors, w, stack);
- }
- }
- colors[v] = Color.Black;
- stack.Push(v);
- }
- static void Main(string[] args)
- {
- var martians = Input();
- var stack = new Stack<int>();
- var colors = new Color[martians.Length];
- for (int i = 0; i < martians.Length; i++)
- {
- if (colors[i] == Color.White)
- {
- Search(martians, colors, i, stack);
- }
- }
- foreach(var e in stack)
- Console.Write("{0} ", e + 1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment