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;
- class Node
- {
- public string NodeName;
- public List<Node> ChildNodes = new List<Node>();
- public Node(string name)
- {
- NodeName = name;
- }
- }
- class DFS
- {
- static Dictionary<string, Node> nodes = new Dictionary<string, Node>();
- static void Main(string[] args)
- {
- string[] settings = Console.ReadLine().Split();
- int numNodes = int.Parse(settings[0]);
- int numSides = int.Parse(settings[1]);
- for (int i = 0; i < numNodes; i++)
- {
- string nodeName = Console.ReadLine();
- nodes.Add(nodeName, new Node(nodeName));
- }
- for (int i = 0; i < numSides; i++)
- {
- string[] pair = Console.ReadLine().Split();
- string fromNode = pair[0];
- string toNode = pair[1];
- nodes[fromNode].ChildNodes.Add(nodes[toNode]);
- }
- string[] nodeNames = nodes.Keys.ToArray();
- Array.Sort(nodeNames);
- Search(nodeNames);
- }
- static void Search(string[] graphNodes)
- {
- List<string> visited = new List<string>();
- while (graphNodes.Length > 0)
- {
- Node node = nodes[graphNodes.First()];
- if (!visited.Contains(node.NodeName))
- {
- visited.Add(node.NodeName);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement