-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathURI1076DesignLabirints.java
More file actions
77 lines (68 loc) · 2.11 KB
/
URI1076DesignLabirints.java
File metadata and controls
77 lines (68 loc) · 2.11 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
/**
* See
* <a href="https://www.urionlinejudge.com.br/judge/en/problems/view/1076">Design
* Labirints</a>
*
* @author Brian Yeicol Restrepo Tangarife
*/
public class URI1076DesignLabirints {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(System.out);
static final int UNVISITED = -1;
static final int VISITED = 1;
static final int CONECTED = 1;
static int edges;
public static void main(String[] args) throws IOException {
int T = readInt(), V, E, source, destination, start;
int[][] G;
int[] vertex, aux;
while (T-- > 0) {
start = readInt();
aux = readArray();
V = aux[0];
E = aux[1];
vertex = new int[V];
G = new int[V][V];
Arrays.fill(vertex, UNVISITED);
while (E-- > 0) {
aux = readArray();
source = aux[0];
destination = aux[1];
G[source][destination] = CONECTED;
G[destination][source] = CONECTED;
}
edges = 0;
dfs(G, vertex, start);
out.println(edges * 2);
}
out.close();
}
private static void dfs(int[][] G, int[] vertex, int v) {
vertex[v] = VISITED;
for (int w = 0; w < G[v].length; w++) {
if (G[v][w] == CONECTED) {
if (vertex[w] == UNVISITED) {
edges++;
dfs(G, vertex, w);
}
}
}
}
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;
}
}