-
Notifications
You must be signed in to change notification settings - Fork 0
/
11403.java
53 lines (42 loc) · 1.14 KB
/
11403.java
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
public class Main{
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(input.readLine());
int arr[][] = new int[N][N];
int answer[][] = new int[N][N];
for(int i=0; i<N; i++){
String testCase[] = input.readLine().split(" ");
for(int j=0; j<N; j++){
arr[i][j] = Integer.parseInt(testCase[j]);
}
}
for(int i=0; i<N; i++){
Queue<Integer> queue = new LinkedList<Integer>();
boolean[] visited = new boolean[N];
queue.offer(i);
while(!queue.isEmpty()){
int p = queue.poll();
for(int j=0; j<N; j++){
if(arr[p][j] == 1 && !visited[j]){
queue.add(j);
visited[j] = true;
answer[i][j] = 1;
}
}
}
}
StringBuilder output = new StringBuilder();
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
output.append(answer[i][j]+" ");
}
output.append("\n");
}
System.out.println(output);
}
}