-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathFindthenearestclone.cs
More file actions
114 lines (91 loc) · 3.38 KB
/
Findthenearestclone.cs
File metadata and controls
114 lines (91 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Find the nearest clone
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 Solution {
// Complete the findShortest function below.
/*
* For the unweighted graph, <name>:
*
* 1. The number of nodes is <name>Nodes.
* 2. The number of edges is <name>Edges.
* 3. An edge exists between <name>From[i] to <name>To[i].
*
*/
static int findShortest(int graphNodes, int[] graphFrom, int[] graphTo, long[] ids, int val) {
var visited = new bool[graphNodes];
var stepsToReach = new int[graphNodes];
var path = new Dictionary<int, List<int>>();
var colors = new Dictionary<int, long>();
for(var index = 0; index < graphFrom.Length; index++)
AddPath(path, graphFrom[index], graphTo[index]);
for(var index = 0; index < ids.Length; index++)
colors.Add(index + 1, ids[index]);
var queue = new Queue<int>();
queue.Enqueue(val);
while(TryDequeue(queue, out var current))
{
if(colors[current] == colors[val] && current != val) return stepsToReach[current - 1];
if (visited[current - 1]) continue;
else visited[current - 1] = true;
if (path.TryGetValue(current, out var list))
{
foreach (var next in list)
{
stepsToReach[next - 1] = stepsToReach[current - 1] + 1;
queue.Enqueue(next);
}
}
}
return -1;
}
static bool TryDequeue<T>(Queue<T> queue, out T value)
{
value = default(T);
if(queue.Count == 0) return false;
value = queue.Dequeue();
return true;
}
static void AddPath(Dictionary<int, List<int>> path, int from, int to, bool bothWays = true)
{
if (path.TryGetValue(from, out var list))
{
if (!list.Contains(to))
list.Add(to);
}
else
path.Add(from, new List<int> { to });
if(bothWays)
AddPath(path, to, from, false);
}
static void Main(string[] args) {
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
string[] graphNodesEdges = Console.ReadLine().Split(' ');
int graphNodes = Convert.ToInt32(graphNodesEdges[0]);
int graphEdges = Convert.ToInt32(graphNodesEdges[1]);
int[] graphFrom = new int[graphEdges];
int[] graphTo = new int[graphEdges];
for (int i = 0; i < graphEdges; i++) {
string[] graphFromTo = Console.ReadLine().Split(' ');
graphFrom[i] = Convert.ToInt32(graphFromTo[0]);
graphTo[i] = Convert.ToInt32(graphFromTo[1]);
}
long[] ids = Array.ConvertAll(Console.ReadLine().Trim().Split(' '), idsTemp => Convert.ToInt64(idsTemp))
;
int val = Convert.ToInt32(Console.ReadLine());
int ans = findShortest(graphNodes, graphFrom, graphTo, ids, val);
textWriter.WriteLine(ans);
textWriter.Flush();
textWriter.Close();
}
}