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.Threading.Tasks;
- namespace Graph
- {
- class Program
- {
- public static int readSize(string path)
- {
- int size;
- System.IO.StreamReader file = new System.IO.StreamReader(path);
- size = int.Parse(file.ReadLine());
- return size;
- }
- public static List<int>[] readFile(string path, List<int>[] array, int size)
- {
- string line;
- System.IO.StreamReader file =
- new System.IO.StreamReader(path);
- for (int i = 0; i < size; i++)
- {
- array[i] = new List<int>();
- line = file.ReadLine();
- for (int k = 0; k < line.Length; k++)
- {
- if (!line[k].Equals('0') && !line[k].Equals(' '))
- array[i].Add(int.Parse(line[k].ToString()));
- Console.WriteLine(array);
- }
- }
- file.Close();
- return array;
- }
- public static void printResult(bool value, int[] used)
- {
- System.IO.StreamWriter textFile = new System.IO.StreamWriter(@"C:\Users\Mira\Desktop\out.txt");
- if (value)
- {
- // Console.WriteLine("YES");
- textFile.WriteLine("Y");
- List<int> firstPart = new List<int>();
- List<int> secondPart = new List<int>();
- for (int i = 1; i < used.Length; i++)
- {
- if (used[i] != 1)
- firstPart.Add(i);
- else secondPart.Add(i);
- }
- for (int g = 0; g < firstPart.Count; g++)
- textFile.Write(firstPart[g] + " ");
- textFile.Write(0);
- textFile.WriteLine();
- for (int g = 0; g < secondPart.Count; g++)
- textFile.Write(secondPart[g] + " ");
- textFile.Close();
- }
- else
- textFile.Write("N");
- textFile.Close();
- }
- public static void BFC(List<int>[] array, int size)
- {
- int[] queue = new int[size];//Очередь для поиска в ширину
- int[] used = new int[size];//массив отмечающий посещённые вершины/ 0 - 1 доля, 1 - вторая
- for (int i = 0; i < size; i++) {
- used[i] = -1;
- }
- bool dvudol = true;
- for (int i = 1; i < size; ++i)
- {
- if (used[i] == -1)
- {
- int h = 0;
- int t = 0;
- queue[t++] = i;
- used[i] = 0;
- while (h < t)
- {
- int v = queue[h++];// V текущая вершина
- for (int k = 0; k < array[v].Count; ++k) //проверяем смежные вершины
- {
- int t0 = array[v][k]; //вершина,смежная с текущей
- //Console.WriteLine(t0);
- if (used[t0] == -1)
- { //не посещенная вершина
- used[t0] = used[v] + 1;
- queue[t++] = t0;
- }
- dvudol &= used[t0] != used[v];
- }
- }
- }
- }
- printResult(dvudol, used);
- }
- static void Main(string[] args)
- {
- string path = @"C:\Users\Mira\Desktop\in.txt";
- int size = readSize(path) + 1;
- List<int>[] checklistOfvertex = new List<int>[size];
- checklistOfvertex = readFile(path, checklistOfvertex, size);
- BFC(checklistOfvertex, size);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement