-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathURI1082ConnectedComponents.java
More file actions
97 lines (86 loc) · 2.91 KB
/
URI1082ConnectedComponents.java
File metadata and controls
97 lines (86 loc) · 2.91 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.TreeSet;
/**
* See
* <a href="https://www.urionlinejudge.com.br/judge/en/problems/view/1082">Connected
* Components</a>
*
* @author Brian Yeicol Restrepo Tangarife
*/
public class URI1082ConnectedComponents {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
int caseNumber = 1, N = readInt(), V, E, s, d, connectedComponents;
int[] P;
int[][] G;
String links;
while (N-- > 0) {
P = readArray();
V = P[0];
E = P[1];
G = new int[V][V];
while (E-- > 0) {
links = read();
s = (int) links.charAt(0) - 97;
d = (int) links.charAt(2) - 97;
G[s][d] = 1;
G[d][s] = 1;
}
connectedComponents = 0;
out.println("Case #" + caseNumber++ + ":");
for (int i = 0; i < V; i++) {
TreeSet<Integer> treeSet = new TreeSet<>();
treeSet.add(i);
treeSet = getConectedComponents(treeSet, G, i, 0);
if (!treeSet.isEmpty()) {
connectedComponents++;
for (Integer next : treeSet) {
out.print((char) (next + 97) + ",");
}
out.println();
}
}
out.println(connectedComponents + " connected components");
out.println();
}
out.close();
}
private static TreeSet<Integer> getConectedComponents(TreeSet<Integer> treeSet, int[][] G, int i, int depth) {
boolean hasConnection = false;
int nonConnection = 0;
for (int j = 0; j < G[i].length; j++) {
if (G[i][j] == 1) {
hasConnection = true;
G[i][j] = 2;
G[j][i] = 2;
treeSet.add(j);
treeSet = getConectedComponents(treeSet, G, j, depth + 1);
} else if (G[i][j] == 0) {
nonConnection++;
}
}
if (!hasConnection && nonConnection < G[i].length && depth == 0) {
treeSet.remove(i);
}
return treeSet;
}
private static String read() throws IOException {
return in.readLine();
}
private static int readInt() throws IOException {
return Integer.parseInt(in.readLine());
}
private static int[] readArray() throws IOException {
String[] line = in.readLine().split("\\s");
int l = line.length;
int[] a = new int[l];
for (int i = 0; i < l; i++) {
a[i] = Integer.parseInt(line[i]);
}
return a;
}
}