-
Notifications
You must be signed in to change notification settings - Fork 693
/
Solution.java
104 lines (89 loc) · 3.45 KB
/
Solution.java
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
//Problem: https://www.hackerrank.com/challenges/rust-murderer
//Java 8
/*
Initial Thoughts: Since we know the graph of main roads, but need
to travel only on side roads, we will need to
get the inverse of the main road graph. Then we
can just run a bfs shortest path on the new graph
Optimization: Instead of inversing the graph, we can just run BFS on
all non-neighbors thus removing the inverse graph step
Per test case
Time Complexity: O(N + M) //We must visit every node once and we do so my way of the main roads
Space Complexity: O(N)
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int T = input.nextInt();
for(int t = 0; t < T; t++)
{
int N = input.nextInt();//Cities(nodes)
int M = input.nextInt();//Main roads(edges)
HashMap<Integer,HashSet<Integer>> cityMap = new HashMap<>();
//Build city map of main roads
for(int i = 0; i < M; i++)
{
int source = input.nextInt();
int target = input.nextInt();
if(cityMap.containsKey(source)) cityMap.get(source).add(target);
else
{
HashSet<Integer> roads = new HashSet<>(); roads.add(target);
cityMap.put(source, roads);
}
if(cityMap.containsKey(target)) cityMap.get(target).add(source);
else
{
HashSet<Integer> roads = new HashSet<>(); roads.add(source);
cityMap.put(target, roads);
}
}
//Starting point of search
int startingPoint = input.nextInt();
//Perform a BFS by traversing non-edges
int[] distances = BFS_Distance(startingPoint, cityMap, N);
//Print output
StringBuilder output = new StringBuilder("");
for(int i = 0; i < distances.length; i++)
{
if(i+1 != startingPoint)
output.append(distances[i]+" ");
}
System.out.println(output);
}
}
//Performs a BFS from starting point to all non-neighbors
static int[] BFS_Distance(int root, HashMap<Integer,HashSet<Integer>> graph, int N)
{
int[] distances = new int[N];
HashSet<Integer> notVisited = new HashSet<>();
HashSet<Integer> visited = new HashSet<>();
for(int i = 1; i <= N; i++) notVisited.add(i);
Queue<Integer> queue = new LinkedList<>();
queue.offer(root);
notVisited.remove(root);
distances[0] = 0;
while(!queue.isEmpty())
{
int curr = queue.poll();
HashSet<Integer> neighbors = graph.get(curr);
for(int nv : notVisited)
{
if(neighbors == null || !neighbors.contains(nv))
{
queue.offer(nv);
distances[nv-1] = distances[curr-1]+1;
visited.add(nv);
}
}
notVisited.removeAll(visited);
visited = new HashSet<>();
}
return distances;
}
}