-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfsShortestPath.java
More file actions
62 lines (60 loc) · 1.87 KB
/
bfsShortestPath.java
File metadata and controls
62 lines (60 loc) · 1.87 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
import java.util.*;
class main{
static class Graph{
public static class Edge{
int vertex;
Edge(int v ){
this.vertex=v;
}
}
ArrayList<Edge> graph[];
public Graph(int size){
this.graph=new ArrayList[size];
for(int i=0;i<size;i++){
this.graph[i]=new ArrayList<>();
}
}
void addEdge(int u , int v){
this.graph[u].add(new Edge(v));
// this.graph[v].add(new Edge(u));
}
void DFS(int src , int isVisited[] , int weight){
isVisited[src]=weight;
// System.out.println(Arrays.toString(isVisited));
for(Edge e : this.graph[src]){
if(isVisited[e.vertex]==-1){
DFS(e.vertex,isVisited,weight+6);
}
}
}
void display(){
for(int i=0;i<this.graph.length;i++){
System.out.print(i+"->");
for(Edge e : this.graph[i]){
System.out.print(e.vertex+",");
}
System.out.println();
}
}
}
public static void main(String args[]){
Scanner scn = new Scanner(System.in);
for(int tc=scn.nextInt();tc>0;tc--){
int n=scn.nextInt();
int m=scn.nextInt();
Graph g = new Graph(n+1);
for(int i=1;i<m;i++){
g.addEdge(scn.nextInt(),scn.nextInt());
}
int src = scn.nextInt();
int isVisited[] = new int[n+1];
Arrays.fill(isVisited,-1);
for(int i=1;i<n;i++){
if(isVisited[i]==-1)g.DFS(src,isVisited,0);
}
for(int i=1;i<n;i++){
if(i!=src) System.out.print(isVisited[i]==0?-1:isVisited);
}
}
}
}