-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGreatRevegetation.java
More file actions
55 lines (54 loc) · 1.61 KB
/
GreatRevegetation.java
File metadata and controls
55 lines (54 loc) · 1.61 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
import java.io.*;
import java.util.*;
public class GreatRevegetation {
static ArrayList<ArrayList<Integer>> AdjList;
static int[] sets;
public static void main(String[] args) throws IOException
{
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br = new BufferedReader(new FileReader("revegetate.in"));
String[] temp = br.readLine().split(" ");
int N = Integer.parseInt(temp[0]);
int M = Integer.parseInt(temp[1]);
AdjList = new ArrayList<ArrayList<Integer>>();
for (int i = 0; i < N; i++)
AdjList.add(new ArrayList<Integer>());
sets = new int[N];
Arrays.fill(sets, -1);
int node1, node2;
for (int i = 0; i < M; i++)
{
temp = br.readLine().split(" ");
node1 = Integer.parseInt(temp[1])-1;//turn 0 based
node2 = Integer.parseInt(temp[2])-1;
AdjList.get(node1).add(node2);
AdjList.get(node2).add(node1);
}
int setnum = 0;
for (int i = 0; i < N; i++)
{
if (sets[i] != -1)// if it belongs to another set
continue;
bfs(i, setnum++);
}
//for each set there is two options. 2^setnum for all possibilities
//System.out.print("1");
//for (int i = 0; i < setnum; i++)
// System.out.print("0");
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("revegetate.out")));
pw.write('1');
for (int i = 0; i < setnum; i++)
pw.write('0');
pw.println();
pw.close();
br.close();
}
public static void bfs(int node, int setnum)
{
if (sets[node] == setnum) //if we've already visited here
return;
sets[node] = setnum; //or else mark it as in the set
for (int edge: AdjList.get(node))
bfs(edge, setnum);
}
}