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;
- using System.IO;
- namespace ConsoleApplication
- {
- public class Graph
- {
- private class Node
- {
- List<List<int>> array = new List<List<int>>();
- int countOfVertex;
- private bool[] nov;
- public void NovSet()
- {
- for (int i = 0; i < countOfVertex; i++)
- nov[i] = true;
- }
- public Node(List<List<int>> tmp, int n)
- {
- array = tmp;
- countOfVertex = n;
- nov = new bool[countOfVertex];
- }
- public void SearchSink(StreamWriter fout)
- {
- bool[] stocks = new bool[countOfVertex];
- for (int i = 0; i < countOfVertex; i++)
- stocks[i] = true;
- for (int i = 0; i < countOfVertex; i++)
- {
- NovSet();
- Dfs(i);
- for (int j = 0; j < countOfVertex; j++)
- if (nov[j] == true) stocks[j] = false;
- }
- for(int i = 0; i < countOfVertex; i++)
- if(stocks[i] == true) fout.Write("{0} ", i + 1);
- }
- public void Dfs(int vertex)
- {
- if ((!nov[vertex])) return;
- nov[vertex] = false;
- for(int j = 0; j < array.Count; j++)
- if (array[j][0] == vertex)
- {
- for (int i = 0; i < 2; i++)
- Dfs(array[j][i]);
- }
- }
- }
- private Node graph;
- public Graph(string nameOfFile)
- {
- StreamReader fin = new StreamReader(nameOfFile);
- string[] nm = fin.ReadLine().Split(' ');
- int m = int.Parse(nm[0]);
- int n = int.Parse(nm[1]);
- List<List<int>> tmp = new List<List<int>>();
- for (int i = 0; i < n; i++)
- {
- List<int> tmp2 = new List<int>();
- string[] arr = fin.ReadLine().Split(' ');
- for (int j = 0; j < arr.Length; j++)
- tmp2.Add(int.Parse(arr[j]) - 1);
- tmp.Add(tmp2);
- }
- fin.Close();
- graph = new Node(tmp, m);
- }
- public void SearchSink(string nameOfFile)
- {
- StreamWriter fout = new StreamWriter(nameOfFile);
- graph.SearchSink(fout);
- fout.Close();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Graph graph1 = new Graph("input.txt");
- graph1.SearchSink("output.txt");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement