-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphland.java
More file actions
38 lines (38 loc) · 935 Bytes
/
Graphland.java
File metadata and controls
38 lines (38 loc) · 935 Bytes
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
import java.util.*;
import java.util.LinkedList;
class Graphland
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
in.nextLine();
String rows[]=in.nextLine().replaceAll("[^0-9\\[\\],]","").split("],\\[");
List<List<Integer>> g=new ArrayList<>();
for(String r:rows)
{
List<Integer> l=new ArrayList<>();
for(String s:r.replaceAll("[\\[\\]]","").split(","))
if(!s.isEmpty())
l.add(Integer.parseInt(s));
g.add(l);
}
boolean vis[]=new boolean[n];
Queue<Integer> q=new LinkedList<>();
List<Integer> res=new ArrayList<>();
q.add(0);
vis[0]=true;
while(!q.isEmpty())
{
int u=q.poll();
res.add(u);
for(int v:g.get(u))
if(!vis[v])
{
vis[v]=true;
q.add(v);
}
}
System.out.println(res);
}
}