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 Chemistry
- {
- public class Node<T>
- {
- private T value;
- public T Value
- {
- get
- {
- return this.value;
- }
- set
- {
- this.value = value;
- }
- }
- public Node(T value)
- {
- this.Value = value;
- }
- public override string ToString()
- {
- return this.value.ToString();
- }
- public override bool Equals(object obj)
- {
- Node<T> n = obj as Node<T>;
- return n.Value.Equals(this.Value);
- }
- public override int GetHashCode()
- {
- return this.value.GetHashCode();
- }
- }
- class Graph<T>
- {
- private Dictionary<Node<T>, List<Node<T>>> adj;
- public Graph()
- {
- adj = new Dictionary<Node<T>, List<Node<T>>>();
- }
- public void AddNode(Node<T> node)
- {
- if (!this.adj.ContainsKey(node))
- {
- this.adj[node] = new List<Node<T>>();
- }
- }
- public void AddEdge(Node<T> source, Node<T> dest)
- {
- if (!adj.ContainsKey(source))
- {
- throw new ArgumentException("Nodes not existing!");
- }
- if (adj[source].Contains(dest))
- {
- return;
- //throw new Exception("Edge already exist!");
- }
- adj[source].Add(dest);
- }
- public bool HasEdge(Node<T> src, Node<T> dest)
- {
- if (!adj.ContainsKey(src))
- {
- return false;
- }
- if (!adj[src].Contains(dest))
- {
- return false;
- }
- return true;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Graph<string> chemistry = new Graph<string>();
- while (true)
- {
- string input = Console.ReadLine();
- if (input == "end") break;
- string[] el = input.Split(' ').ToArray();
- Node<string> n1 = new Node<string>(el[0]);
- Node<string> n2 = new Node<string>(el[1]);
- chemistry.AddNode(n1);
- chemistry.AddNode(n2);
- chemistry.AddEdge(n1, n2);
- }
- while (true)
- {
- string input = Console.ReadLine();
- if (input == "end") break;
- string[] el = input.Split('-').ToArray();
- string[] el2 = el[1].Split(',').ToArray();
- Node<string> n1 = new Node<string>(el[0]);
- foreach (var item in el2)
- {
- Node<string> n2 = new Node<string>(item);
- if (chemistry.HasEdge(n1, n2))
- {
- Console.WriteLine($"{el[0]}-{item}");
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement