public static IList <string> Answer(IList <string> inputs)
        {
            var chars        = new[] { ' ' };
            var line0        = inputs[0].Split(chars);
            var verticeCount = int.Parse(line0[0]);
            var edgeCount    = int.Parse(line0[1]);

            var xs = Enumerable.Range(1, edgeCount)
                     .Select(i =>
            {
                var items = inputs[i].Trim().Split(chars, StringSplitOptions.RemoveEmptyEntries);
                return(new
                {
                    Left = GetIndex(items[0]),
                    Right = GetIndex(items[1])
                });
            });

            var graph = new AdjacencyListGraph(verticeCount);

            foreach (var x in xs)
            {
                graph.AddDirectedEdge(x.Left, x.Right);
            }

            //Console.WriteLine(graph);
            var s = new TopologicalSort(graph);

            s.Search();
            var answer = string.Join(" ", s.Order.Select(GetSource));

            return(new[] { answer });
        }
Beispiel #2
0
        //public static void Main(string[] args)
        //{
        //    string s;
        //    var inputs = new List<string>();
        //    while ((s = Console.ReadLine()) != null)
        //        inputs.Add(s);

        //    foreach (var result in Answer(inputs.ToArray()))
        //        Console.WriteLine(result);
        //}

        public static IList <string> Answer(IList <string> inputs)
        {
            var graph = Inputs.AdjacencyListGraphLong(inputs).ToDirectedAdjacencyGraph();
            //Console.WriteLine(graph);

            var s = new TopologicalSort(graph);

            s.Search();
            var answer = string.Join(" ", s.Order.Select(GetSource));

            return(new[] { answer });
        }
        public static IEnumerable <int> StronglyConnectedComponents(AdjacencyListGraph <long> graph, AdjacencyListGraph <long> reverse)
        {
            //run dfs of reverse graph
            var srg = new TopologicalSort(reverse);

            srg.Search();

            //look for v in graph in reverse post order
            var sg    = new DepthFirstSearchWithComponents(graph);
            var order = srg.Order;

            foreach (var v in order)
            {
                //if not visited => explore and mark visted vertices as new component
                sg.Explore(v);
            }
            return(sg.Components);
        }