-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.java
116 lines (90 loc) · 2.56 KB
/
test.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
105
106
107
108
109
110
111
112
113
114
115
116
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Solution {
public static void BFSPath(boolean [][] edges, int n, int sv, int end, boolean [] visited, ArrayList<Integer> arr)
{
Queue<Integer> q=new LinkedList<>();
HashMap<Integer,Integer> map=new HashMap<>();
// ArrayList<Integer> temp = new ArrayList<>();
visited[sv]=true;
q.add(sv);
// boolean found=false;
while(!q.isEmpty())
{
int currVertex=q.poll();
// arr.add(currVertex);
if(currVertex==end)
{
//found=true;
break;
}
for(int i=0;i<n;i++)
{
// if(i==currVertex)
// continue;
if(i!=currVertex&&edges[currVertex][i]&&!visited[i])
{
q.add(i);
visited[i]=true;
map.put(i,currVertex);
}
}
q.poll();
if (q.isEmpty())//null
{
return;
}
}
arr.add(end);
int i = end;
while(i != sv)
{
arr.add(map.get(i));
i = map.get(i);
}
arr.add(sv);
}
public static void main(String[] args) throws NumberFormatException, IOException {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int e=sc.nextInt();
if(n==0)
return;
if(e==0)
{
return;
}
boolean [][] edge=new boolean[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
edge[i][j]=false;
}
for(int i=0;i<e;i++)
{
int a=sc.nextInt();
int b=sc.nextInt();
edge[a][b]=true;
edge[b][a]=true;
}
long V1=sc.nextLong();
long V2=sc.nextLong();
// if(V1>=n||V2>=n)
// {
// return;
// }
int v1=(int)V1;
int v2=(int)V2;
boolean [] visited=new boolean[n];
for(int i=0;i<n;i++)
visited[i]=false;
ArrayList <Integer> list=new ArrayList<>();
BFSPath(edge,n,v1,v2,visited,list);
if(list.isEmpty())
return;
for(int i=0;i<list.size();i++)
System.out.print(list.get(i)+" ");
}
}