Advertisement
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.IO;
- using System.Threading.Tasks;
- namespace Nevezetes_Algoritmusok
- {
- public class _70
- {
- public void Solve()
- {
- List<string> lines = new List<string>(File.ReadAllLines("../../70_input_1.txt"));
- Node start_node = new Node(int.Parse(lines[0].Split()[2]));
- Node finish_node = new Node(int.Parse(lines[0].Split()[3]));
- Dictionary<int, Node> nodes = new Dictionary<int, Node>();
- nodes.Add(start_node.number, start_node);
- nodes.Add(finish_node.number, finish_node);
- lines.RemoveAt(0);
- PreProcess(lines, nodes);
- List<Node> topological_order = Topoligical_Sort(start_node, nodes);
- //Console.WriteLine(string.Join(" ", topological_order));
- List<Node> critical_nodes = GetCriticalNodes(start_node, finish_node, topological_order);
- Console.WriteLine(critical_nodes.Count);
- Console.WriteLine(string.Join(" ", critical_nodes));
- Console.ReadLine();
- }
- //Kahn's algorithm
- private List<Node> Topoligical_Sort(Node start_node, Dictionary<int, Node> nodes)
- {
- List<Node> topological_order = new List<Node>();
- Stack<Node> zero_in_count_nodes = new Stack<Node>();
- zero_in_count_nodes.Push(start_node);
- while (zero_in_count_nodes.Count > 0)
- {
- Node current = zero_in_count_nodes.Pop();
- topological_order.Add(current);
- HashSet<Node> children = new HashSet<Node>(current.directlyAccessibleNodes);
- foreach (Node child in children)
- {
- child.nodesDirectlyAccessingThisNode.Remove(current);
- current.directlyAccessibleNodes.Remove(child);
- if (child.nodesDirectlyAccessingThisNode.Count == 0)
- zero_in_count_nodes.Push(child);
- }
- }
- return topological_order;
- }
- private List<Node> GetCriticalNodes(Node start_node, Node finish_node, List<Node> topological_order)
- {
- List<Node> criticalNodes = new List<Node>();
- int magic = 0;
- foreach (var node in topological_order)
- {
- magic -= node.InCount;
- if (magic == 0 && node != start_node && node != finish_node)
- criticalNodes.Add(node);
- magic += node.OutCount;
- }
- return criticalNodes;
- }
- private void PreProcess(List<string> lines, Dictionary<int, Node> nodes)
- {
- lines.ForEach(line =>
- {
- int from_node = int.Parse(line.Split()[0]);
- int to_node = int.Parse(line.Split()[1]);
- if (!nodes.ContainsKey(from_node))
- nodes.Add(from_node, new Node(from_node));
- if (!nodes.ContainsKey(to_node))
- nodes.Add(to_node, new Node(to_node));
- nodes[from_node].directlyAccessibleNodes.Add(nodes[to_node]);
- nodes[to_node].nodesDirectlyAccessingThisNode.Add(nodes[from_node]);
- nodes[from_node].OutCount++;
- nodes[to_node].InCount++;
- });
- }
- private class Node
- {
- public int number;
- public HashSet<Node> nodesDirectlyAccessingThisNode = new HashSet<Node>();
- public HashSet<Node> directlyAccessibleNodes = new HashSet<Node>();
- public int InCount;
- public int OutCount;
- public Node(int _number)
- {
- number = _number;
- }
- public override int GetHashCode()
- {
- return number.GetHashCode();
- }
- public override bool Equals(object obj)
- {
- if (obj is Node)
- {
- return number == ((Node)obj).number;
- }
- return false;
- }
- public override string ToString()
- {
- return number.ToString();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement