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;
- using System.IO;
- namespace Bai3
- {
- class Program
- {
- static int n;
- static bool[] visited;
- static List<List<int>> adjList = new List<List<int>>();
- static LinkedList<int> res = new LinkedList<int>();
- static void Main(string[] args)
- {
- readFile("TopoSort.inp");
- DFS(1);
- writeFile("TopoSort.out");
- }
- static void writeFile(string fileName)
- {
- StreamWriter sW = new StreamWriter(fileName);
- foreach (int item in res)
- {
- sW.Write(item + " ");
- }
- sW.Close();
- }
- static void readFile(string fileName)
- {
- StreamReader sR = new StreamReader(fileName);
- n = int.Parse(sR.ReadLine());
- visited = new bool[n + 1];
- for (int i = 0; i <= n; i++)
- {
- adjList.Add(new List<int>());
- }
- for (int i = 1; i <= n; i++)
- {
- string temp = sR.ReadLine();
- if (temp == "") continue;
- string[] split = temp.Split();
- foreach (string a in split)
- {
- adjList[i].Add(int.Parse(a));
- }
- }
- sR.Close();
- }
- static void DFS(int u)
- {
- visited[u] = true;
- foreach (int v in adjList[u])
- {
- if (!visited[v]) DFS(v);
- }
- res.AddFirst(u);
- }
- static void TopoSort()
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement