Created
May 18, 2022 20:28
-
-
Save Earu/9d217e7a2d5168d3b5dd9d67f8e98854 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.CodeDom.Compiler; | |
using System.Collections.Generic; | |
using System.Collections; | |
using System.ComponentModel; | |
using System.Diagnostics.CodeAnalysis; | |
using System.Globalization; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using System.Runtime.Serialization; | |
using System.Text.RegularExpressions; | |
using System.Text; | |
using System; | |
class Result | |
{ | |
class GrapNode { | |
public int Value {get;set;} | |
public List<GrapNode> Links {get;set;} = new List<GrapNode>(); | |
} | |
/* | |
* Complete the 'bfs' function below. | |
* | |
* The function is expected to return an INTEGER_ARRAY. | |
* The function accepts following parameters: | |
* 1. INTEGER n | |
* 2. INTEGER m | |
* 3. 2D_INTEGER_ARRAY edges | |
* 4. INTEGER s | |
*/ | |
public static List<int> bfs(int n, int m, List<List<int>> edges, int s) | |
{ | |
Dictionary<int, GraphNode> nodes = new Dictionary<int, GraphNode>(); | |
for (int i = 0; i < m; i++) | |
{ | |
foreach (List<int> edge in edges[i]) | |
{ | |
if (!nodes.ContainsKey(edges[0])) | |
{ | |
GrapNode node = new GrapNode(); | |
node.Value = edges[0]; | |
nodes.Add(edges[0], node); | |
} | |
if (!nodes.ContainsKey(edges[1])) | |
{ | |
GrapNode node = new GrapNode(); | |
node.Value = edges[1]; | |
nodes.Add(edges[1], node); | |
} | |
nodes[edges[0]].Links.Add(nodes[edges[1]]); | |
} | |
} | |
for (int i = 0; i < n; i++) { | |
GraphNode node = nodes[i]; | |
HashSet<GraphNode> doneNodes = new HashSet<GraphNode>(); | |
} | |
} | |
} | |
class Solution | |
{ | |
public static void Main(string[] args) | |
{ | |
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true); | |
int q = Convert.ToInt32(Console.ReadLine().Trim()); | |
for (int qItr = 0; qItr < q; qItr++) | |
{ | |
string[] firstMultipleInput = Console.ReadLine().TrimEnd().Split(' '); | |
int n = Convert.ToInt32(firstMultipleInput[0]); | |
int m = Convert.ToInt32(firstMultipleInput[1]); | |
List<List<int>> edges = new List<List<int>>(); | |
for (int i = 0; i < m; i++) | |
{ | |
edges.Add(Console.ReadLine().TrimEnd().Split(' ').ToList().Select(edgesTemp => Convert.ToInt32(edgesTemp)).ToList()); | |
} | |
int s = Convert.ToInt32(Console.ReadLine().Trim()); | |
List<int> result = Result.bfs(n, m, edges, s); | |
textWriter.WriteLine(String.Join(" ", result)); | |
} | |
textWriter.Flush(); | |
textWriter.Close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment