fork download
  1. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace IsATree { public class CheckTree { //=============================================================================== //Your Code is Here: public static bool IsTree(string[] vertices, List<KeyValuePair<string, string>> edges) { // string [] color = {"White","Gray","Black"}; Dictionary<string, string> color = new Dictionary<string, string>(); Dictionary<string, List<string>> adj = new Dictionary<string, List<string>>(); for (int i = 0; i < edges.Count; i++) { if (!adj.ContainsKey(edges[i].Key))//to know if the key exists in the dictionary or not. adj[edges[i].Key] = new List<string>(); adj[edges[i].Key].Add(edges[i].Value); } for (int i = 0; i < vertices.Length; i++) { color[vertices[i]] = "White"; // Set every vertex to white. } bool DFS(string ITEM) { color[ITEM] = "Gray"; if (adj.ContainsKey(ITEM)) { List<string> list = adj[ITEM]; for (int i = 0; i < adj[ITEM].Count; i++) { if (color[list[i]] == "White") { if (!DFS(list[i])) return false; } if (color[list[i]] == "Gray") { return false; } } } color[ITEM] = "Black"; // If the node is black, return true because the tree ended. return true; } for (int i = 0; i < vertices.Length; i++) { if (color[vertices[i]] == "White") // check if the color of the Node is white or Not { if (!DFS(vertices[i])) //Go make a DFS and say if the result is a DFS or not. return false; } } return true; } } }
Success #stdin #stdout #stderr 0.26s 39340KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "using System"
Execution halted