Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 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.  
  8. namespace Graph
  9. {
  10. class Program
  11. {
  12. public static int readSize(string path)
  13. {
  14. int size;
  15. System.IO.StreamReader file = new System.IO.StreamReader(path);
  16. size = int.Parse(file.ReadLine());
  17. return size;
  18. }
  19. public static List<int>[] readFile(string path, List<int>[] array, int size)
  20. {
  21. string line;
  22. System.IO.StreamReader file =
  23. new System.IO.StreamReader(path);
  24. for (int i = 0; i < size; i++)
  25. {
  26. array[i] = new List<int>();
  27. line = file.ReadLine();
  28. for (int k = 0; k < line.Length; k++)
  29. {
  30. if (!line[k].Equals('0') && !line[k].Equals(' '))
  31. array[i].Add(int.Parse(line[k].ToString()));
  32. Console.WriteLine(array);
  33. }
  34. }
  35. file.Close();
  36. return array;
  37. }
  38. public static void printResult(bool value, int[] used)
  39. {
  40. System.IO.StreamWriter textFile = new System.IO.StreamWriter(@"C:\Users\Mira\Desktop\out.txt");
  41. if (value)
  42. {
  43.  
  44. // Console.WriteLine("YES");
  45. textFile.WriteLine("Y");
  46. List<int> firstPart = new List<int>();
  47. List<int> secondPart = new List<int>();
  48.  
  49. for (int i = 1; i < used.Length; i++)
  50. {
  51. if (used[i] != 1)
  52. firstPart.Add(i);
  53. else secondPart.Add(i);
  54. }
  55.  
  56. for (int g = 0; g < firstPart.Count; g++)
  57. textFile.Write(firstPart[g] + " ");
  58. textFile.Write(0);
  59. textFile.WriteLine();
  60.  
  61.  
  62. for (int g = 0; g < secondPart.Count; g++)
  63. textFile.Write(secondPart[g] + " ");
  64. textFile.Close();
  65.  
  66. }
  67. else
  68. textFile.Write("N");
  69. textFile.Close();
  70. }
  71. public static void BFC(List<int>[] array, int size)
  72. {
  73. int[] queue = new int[size];//Очередь для поиска в ширину
  74. int[] used = new int[size];//массив отмечающий посещённые вершины/ 0 - 1 доля, 1 - вторая
  75. for (int i = 0; i < size; i++) {
  76. used[i] = -1;
  77. }
  78.  
  79. bool dvudol = true;
  80. for (int i = 1; i < size; ++i)
  81. {
  82. if (used[i] == -1)
  83. {
  84. int h = 0;
  85. int t = 0;
  86. queue[t++] = i;
  87. used[i] = 0;
  88.  
  89. while (h < t)
  90. {
  91. int v = queue[h++];// V текущая вершина
  92. for (int k = 0; k < array[v].Count; ++k) //проверяем смежные вершины
  93. {
  94. int t0 = array[v][k]; //вершина,смежная с текущей
  95. //Console.WriteLine(t0);
  96. if (used[t0] == -1)
  97. { //не посещенная вершина
  98. used[t0] = used[v] + 1;
  99. queue[t++] = t0;
  100. }
  101. dvudol &= used[t0] != used[v];
  102. }
  103. }
  104. }
  105.  
  106. }
  107. printResult(dvudol, used);
  108. }
  109. static void Main(string[] args)
  110. {
  111. string path = @"C:\Users\Mira\Desktop\in.txt";
  112. int size = readSize(path) + 1;
  113. List<int>[] checklistOfvertex = new List<int>[size];
  114. checklistOfvertex = readFile(path, checklistOfvertex, size);
  115. BFC(checklistOfvertex, size);
  116.  
  117.  
  118. }
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement