-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCasteland.java
More file actions
32 lines (32 loc) · 830 Bytes
/
Casteland.java
File metadata and controls
32 lines (32 loc) · 830 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
import java.util.*;
class Castleland
{
static void dfs(int u,List<List<Integer>> g,boolean v[],List<Integer> r)
{
v[u]=true;
r.add(u);
for(int x:g.get(u))
if(!v[x])
dfs(x,g,v,r);
}
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 v[]=new boolean[n];
List<Integer> r=new ArrayList<>();
dfs(0,g,v,r);
System.out.println(r);
}
}