-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_11403_stack.java
More file actions
65 lines (57 loc) · 1.76 KB
/
Copy pathBOJ_11403_stack.java
File metadata and controls
65 lines (57 loc) · 1.76 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
// BOJ - 11403
// Problem Sheet - https://www.acmicpc.net/problem/11403
import java.util.*;
import java.io.*;
public class Main {
private static int N;
private static int[][] am;
private static int[][] answer;
private static boolean[] visited;
public static void main(String[] args) throws IOException {
input();
System.out.println(solve());
}
private static void input() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
am = new int[N][N];
for (int i=0 ; i<N ; i++) {
String[] row = br.readLine().split(" ");
for (int j=0 ; j<N ; j++) {
am[i][j] = Integer.parseInt(row[j]);
}
}
answer = new int[N][N];
br.close();
}
private static String solve() {
StringBuilder sb = new StringBuilder();
for (int i=0 ; i<N ; i++) {
visited = new boolean[N];
dfs(i);
}
for (int i=0 ; i<N ; i++) {
for (int j=0 ; j<N ; j++) {
sb.append(answer[i][j]).append(" ");
}
sb.append("\n");
}
return sb.toString();
}
private static void dfs(int sv) {
Deque<Integer> stack = new ArrayDeque<>();
stack.push(sv);
while (!stack.isEmpty()) {
boolean allConnected = true;
for (int i=0 ; i<N ; i++) {
if (visited[i] || am[stack.peek()][i] == 0) continue;
stack.push(i);
visited[i] = true;
allConnected = false;
answer[sv][i] = 1;
break;
}
if (allConnected) stack.pop();
}
}
}