-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathMessageRoutes.java
More file actions
111 lines (91 loc) · 2.67 KB
/
MessageRoutes.java
File metadata and controls
111 lines (91 loc) · 2.67 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
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
class Graph {
public HashMap<Integer,HashSet<Integer>> map;
public ArrayList<Integer> path;
public int[] parent;
int n;
public Graph(int n){
this.n=n;
parent=new int[n+1];
Arrays.fill(parent,-1);
path=new ArrayList<>();
map=new HashMap<>();
for(int i=1;i<=n;i++)
{
map.put(i,new HashSet<Integer>());
}
}
public void printPath(){
int i=n;
while(parent[i]!=-1){
path.add(0,i);
i=parent[i];
}
System.out.print(1+" ");
for(i=0;i<path.size();i++){
int c=path.get(i);
System.out.print(c+" ");
}
return;
}
public int bfs(int src,int des)
{
boolean[] visited=new boolean[des+1];
Queue<Integer> q=new LinkedList<>();
q.offer(src);
visited[src]=true;
int moves=0;
while(!q.isEmpty())
{
int size=q.size();
while(size-->0){
int val=q.poll();
if(val==des){
return moves;
}
for(int neighbour : map.get(val)){
if(!visited[neighbour]){
visited[neighbour]=true;
parent[neighbour]=val;
q.offer(neighbour);
}
}
}
moves++;
}
return -1;
}
}
public class MessageRoutes {
String url="https://cses.fi/problemset/task/1667/";
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
int numberOfComputers=sc.nextInt();
int numberOfConnections=sc.nextInt();
//1 to numberOfComputers
int a;int b;
Graph g=new Graph(numberOfComputers);
for(int i=0;i<numberOfConnections;i++)
{
a=sc.nextInt();
b=sc.nextInt();
g.map.get(a).add(b);
g.map.get(b).add(a);
}
//Since it is a connection
//So It is undirected
int ans=1+g.bfs(1,numberOfComputers);
if(ans==0){
System.out.println("IMPOSSIBLE");return;
}
System.out.println(ans);
g.printPath();
}
}
}