Advertisement
Guest User

Untitled

a guest
May 25th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp1
  8. {
  9. class Program
  10. {
  11. static HashSet<int> dfs(int startNode, long[,] graph, bool killCycles=false)
  12. {
  13. /* Поиск в глубину, который умеет убивать циклы. */
  14. var visited = new HashSet<int>();
  15. var finished = new HashSet<int>();
  16. var stack = new Stack<int>();
  17. visited.Add(startNode);
  18. stack.Push(startNode);
  19. while (stack.Count != 0)
  20. {
  21. var node = stack.Pop();
  22. foreach (var nextNode in incidentNodes[node])
  23. {
  24. if (finished.Contains(nextNode)) continue;
  25. if (killCycles)
  26. {
  27. if (visited.Contains(nextNode)) {
  28. // 2 := "d" Убиваем циклы
  29. graph[node, nextNode] = 2;
  30. graph[nextNode, node] = 2;
  31. incidentNodes[node].Remove(nextNode);
  32. incidentNodes[nextNode].Remove(node);
  33. cost += d;
  34. break;
  35. }
  36. }
  37. visited.Add(nextNode);
  38. stack.Push(nextNode);
  39. }
  40. finished.Add(node);
  41. }
  42. return finished;
  43. }
  44.  
  45. static long n;
  46. static long d;
  47. static long a;
  48. static long cost;
  49. static Dictionary<int, List<int>> incidentNodes;
  50.  
  51. static void Main(string[] args)
  52. {
  53. // Ввод
  54. n = int.Parse(Console.ReadLine()); // Количество аэропортов (2 <= n <= 100)
  55. var s = Console.ReadLine().Split(); // Строка вида d a
  56. d = int.Parse(s[0]); // Стоимость удаления
  57. a = int.Parse(s[1]); // Стоимость добавления
  58. cost = 0; // Сколько нужно потратить в итоге
  59. var graph = new long[n, n];
  60. var colors = new Dictionary<int, int>(); // Каждому узлу ставим в соотвествие его "цвет"
  61. incidentNodes = new Dictionary<int, List<int>>();
  62. // Вводим граф
  63. for (var i = 0; i < n; i++)
  64. {
  65. var line = Console.ReadLine();
  66. var j = 0;
  67. incidentNodes[i] = new List<int>();
  68. foreach (var c in line)
  69. {
  70. graph[i, j] = int.Parse(c.ToString());
  71. if (c == '1') { incidentNodes[i].Add(j); }
  72. j++;
  73. }
  74. }
  75.  
  76. // Убиваем циклы
  77. for(var i = 0; i<n;i++) dfs(i, graph, true);
  78.  
  79. // Ищем компоненты связности
  80. var sNodes = 0;
  81. var result = dfs(0, graph);
  82. var currentColor = 0;
  83. foreach (var node in result)
  84. {
  85. colors[node] = currentColor;
  86. }
  87. sNodes += result.Count;
  88. while(sNodes < n)
  89. {
  90. for(var i = 0; i < n; i++)
  91. {
  92. if (!result.Contains(i))
  93. {
  94. result = dfs(i, graph, true);
  95. currentColor += 1;
  96. foreach (var node in result)
  97. {
  98. colors[node] = currentColor;
  99. }
  100. sNodes += result.Count;
  101. }
  102. }
  103. }
  104.  
  105. /* Далее, проверяем каждое ребро (u, v).
  106. * Если между две вершины не инцидентны,
  107. * то проверим в какой компоненты связности они находятся.
  108. * Если в разных, то раскрашиваем эти вершины в один цвет и добавляем между ними ребро.*/
  109. for (var i = 0; i < n; i++)
  110. {
  111. for(var j = 0; j < n; j++)
  112. {
  113. if(graph[i, j] == 0)
  114. {
  115. if(colors[i] != colors[j])
  116. {
  117. graph[i, j] = 3;
  118. graph[j, i] = 3;
  119. cost += a;
  120. colors[j] = colors[i];
  121. result = dfs(j, graph);
  122. foreach (var node in result)
  123. {
  124. colors[node] = colors[j];
  125. }
  126. }
  127. }
  128. }
  129. }
  130.  
  131. // Вывод
  132. Console.WriteLine(cost);
  133. for (var i = 0; i < n; i++) {
  134. for (var j = 0; j < n; j++) {
  135. if (graph[i, j] == 2) Console.Write("d");
  136. else if (graph[i, j] == 3) Console.Write("a");
  137. else Console.Write("0");
  138. }
  139. Console.Write("\n");
  140. }
  141. Console.ReadKey();
  142. }
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement