Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.21 KB | None | 0 0
  1. public class Solution {
  2.    
  3.     public IList<IList<int>> CriticalConnections(int n, IList<IList<int>> connections)
  4.     {
  5.         if (connections == null
  6.            || connections.Count == 0
  7.            || connections.Any(e => e == null))
  8.             return new List<IList<int>>();
  9.  
  10.         //build nodes and edges
  11.         var nodes = new Dictionary<int, Node>();
  12.         var edges = new Dictionary<string, Edge>();
  13.         foreach (var cedge in connections)
  14.         {
  15.             if (!TryGet(edges, Edge.GetId(cedge[0], cedge[1]), out var edge))
  16.             {
  17.                 edge = new Edge(cedge);
  18.                 edges[edge.Id] = edge;
  19.  
  20.                 //from node
  21.                 if (!TryGet(nodes, edge.From, out var node))
  22.                     nodes[edge.From] = node = new Node(edge.From);
  23.  
  24.                 node.Edges.Add(edge);
  25.  
  26.                 //To node
  27.                 if (!TryGet(nodes, edge.To, out node))
  28.                     nodes[edge.To] = node = new Node(edge.To);
  29.  
  30.                 node.Edges.Add(edge);
  31.             }
  32.         }
  33.  
  34.         //start anywhere
  35.         var first = nodes.Values.First();
  36.         Traverse(null, first, new DFSContext { Step = 1, Nodes = nodes });
  37.  
  38.         return edges.Values
  39.             .Where(edge => edge.IsBridge)
  40.             .Select(edge => edge.ToList() as IList<int>)
  41.             .ToList();
  42.     }
  43.  
  44.     static public int Traverse(
  45.         Node parent,
  46.         Node node,
  47.         DFSContext context)
  48.     {
  49.         int? lowestConnection = null;
  50.         node.VisitStep = context.Step;
  51.         foreach (var edge in node.Edges)
  52.         {
  53.             var to = context.Nodes[edge.OtherNode(node.Id)];
  54.             if (to == parent || to.VisitStep > node.VisitStep)
  55.                 continue;
  56.  
  57.             if (to.VisitStep < node.VisitStep)
  58.                 lowestConnection = lowestConnection == null || to.VisitStep < lowestConnection
  59.                     ? to.VisitStep
  60.                     : lowestConnection;
  61.  
  62.             else
  63.             {
  64.                 var lowest = Traverse(node, to, context.Advance());
  65.                 if(lowest > node.VisitStep)
  66.                     edge.IsBridge = true;
  67.  
  68.                 lowestConnection = lowestConnection == null || lowest < lowestConnection
  69.                     ? lowest
  70.                     : lowestConnection;
  71.             }
  72.         }
  73.  
  74.         if (!lowestConnection.HasValue)
  75.             return node.VisitStep.Value;
  76.  
  77.         else return lowestConnection.Value;
  78.     }
  79.  
  80.     static public bool TryGet<K, V>(Dictionary<K, V> dict, K key, out V value)
  81.     {
  82.         if (dict.ContainsKey(key))
  83.         {
  84.             value = dict[key];
  85.             return true;
  86.         }
  87.         else
  88.         {
  89.             value = default(V);
  90.             return false;
  91.         }
  92.     }
  93. }
  94.  
  95. public class DFSContext
  96. {
  97.     public Dictionary<int, Node> Nodes { get; set; }
  98.  
  99.     public int Step { get; set; }
  100.  
  101.     public DFSContext Advance()
  102.     {
  103.         Step += 1;
  104.         return this;
  105.     }
  106. }
  107.  
  108. public class Edge
  109. {
  110.     public int From { get; }
  111.  
  112.     public int To { get; }
  113.  
  114.     public bool IsBridge { get; set; }
  115.  
  116.     public string Id => GetId(From, To);
  117.  
  118.  
  119.     public Edge(IList<int> edge)
  120.     {
  121.         To = Math.Max(edge[0], edge[1]);
  122.         From = Math.Min(edge[0], edge[1]);
  123.     }
  124.  
  125.     public IList<int> ToList() => new List<int> { From, To };
  126.  
  127.     public int OtherNode(int nodeId)
  128.     {
  129.         if (nodeId == From)
  130.             return To;
  131.  
  132.         else if (nodeId == To)
  133.             return From;
  134.  
  135.         else throw new Exception();
  136.     }
  137.  
  138.     public override bool Equals(object obj)
  139.     {
  140.         return obj is Edge edge
  141.             && edge.From == From
  142.             && edge.To == To;
  143.     }
  144.  
  145.     public override int GetHashCode()
  146.     {
  147.         var acc = 3 * 5 + From;
  148.         return acc * 13 + To;
  149.     }
  150.  
  151.     public static string GetId(int a, int b)
  152.     {
  153.         var min = Math.Min(a, b);
  154.         var max = Math.Max(a, b);
  155.  
  156.         return $"{min},{max}";
  157.     }
  158. }
  159.  
  160. public class Node
  161. {
  162.     public int Id { get; }
  163.  
  164.     public int? VisitStep { get; set; } = null;
  165.  
  166.     public HashSet<Edge> Edges { get; } = new HashSet<Edge>();
  167.  
  168.     public Node(int id)
  169.     {
  170.         Id = id;
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement