Advertisement
myname0

graph_contester_detours_6

May 25th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.IO;
  7.  
  8. namespace ConsoleApplication
  9. {
  10.     public class Graph
  11.     {
  12.         private class Node
  13.         {
  14.             List<List<int>> array = new List<List<int>>();
  15.             int countOfVertex;
  16.             private bool[] nov;
  17.  
  18.             public void NovSet()
  19.             {
  20.                 for (int i = 0; i < countOfVertex; i++)
  21.                     nov[i] = true;
  22.             }
  23.  
  24.             public Node(List<List<int>> tmp, int n)
  25.             {
  26.                 array = tmp;
  27.                 countOfVertex = n;
  28.                 nov = new bool[countOfVertex];
  29.             }
  30.  
  31.             public void SearchSink(StreamWriter fout)
  32.             {
  33.                 bool[] stocks = new bool[countOfVertex];
  34.                 for (int i = 0; i < countOfVertex; i++)
  35.                     stocks[i] = true;
  36.                 for (int i = 0; i < countOfVertex; i++)
  37.                 {
  38.                     NovSet();
  39.                     Dfs(i);
  40.                     for (int j = 0; j < countOfVertex; j++)
  41.                         if (nov[j] == true) stocks[j] = false;
  42.                 }
  43.                 for(int i = 0; i < countOfVertex; i++)
  44.                     if(stocks[i] == true) fout.Write("{0} ", i + 1);
  45.             }
  46.  
  47.             public void Dfs(int vertex)
  48.             {
  49.                 if ((!nov[vertex])) return;
  50.  
  51.                 nov[vertex] = false;
  52.  
  53.                 for(int j = 0; j < array.Count; j++)
  54.                     if (array[j][0] == vertex)
  55.                     {
  56.                         for (int i = 0; i < 2; i++)
  57.                             Dfs(array[j][i]);
  58.                     }                    
  59.             }
  60.         }
  61.         private Node graph;
  62.  
  63.         public Graph(string nameOfFile)
  64.         {
  65.             StreamReader fin = new StreamReader(nameOfFile);
  66.             string[] nm = fin.ReadLine().Split(' ');
  67.             int m = int.Parse(nm[0]);
  68.             int n = int.Parse(nm[1]);
  69.             List<List<int>> tmp = new List<List<int>>();
  70.             for (int i = 0; i < n; i++)
  71.             {
  72.                 List<int> tmp2 = new List<int>();
  73.                 string[] arr = fin.ReadLine().Split(' ');
  74.                 for (int j = 0; j < arr.Length; j++)
  75.                     tmp2.Add(int.Parse(arr[j]) - 1);
  76.                 tmp.Add(tmp2);
  77.             }
  78.             fin.Close();
  79.             graph = new Node(tmp, m);
  80.         }
  81.  
  82.         public void SearchSink(string nameOfFile)
  83.         {
  84.             StreamWriter fout = new StreamWriter(nameOfFile);
  85.             graph.SearchSink(fout);
  86.             fout.Close();
  87.         }
  88.     }
  89.     class Program
  90.     {
  91.         static void Main(string[] args)
  92.         {
  93.             Graph graph1 = new Graph("input.txt");
  94.             graph1.SearchSink("output.txt");
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement