-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS_trying.java
More file actions
74 lines (63 loc) · 1.81 KB
/
Copy pathBFS_trying.java
File metadata and controls
74 lines (63 loc) · 1.81 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
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 sc= new Scanner(System.in);
int t=sc.nextInt();
int n=0,e=0,s=0,temp=0,dist=0;
while(t>0)
{
n=0;e=0;
n= sc.nextInt();
e=sc.nextInt();
int m [][] = new int [n][n];
boolean visit[] = new boolean[n];
for(int i=0;i<n;i++)
{
visit[i] = false;
}
for(int i=0;i<n;i++)
{
int t1=sc.nextInt()-1;
int t2=sc.nextInt()-1;
m[t1][t2] = 1;
m[t2][t1] = 1;
}
s=sc.nextInt();
Queue <Integer> q=new LinkedList<Integer> ();
int[] h= new int[n];
for(int i=0;i<n;i++)
{
h[i] = -1;
}
s=s-1;
q.add(s);
int cnt=0;
while(!q.isEmpty())
{
temp =q.remove();
visit[temp]=true;
h[temp] = dist;
dist = dist +6;
for(int i=0;i<n;i++)
{
if(m[temp][i]==1 && visit[temp]==false)
{
q.add(i);
++cnt;
}
}
--cnt;
}
for(int i=0;i<n;i++)
{
System.out.print(h[i]+" ");
}
--t;
System.out.println();
}
}
}