fork download
  1. import javax.swing.*;
  2. import java.util.*;
  3. import java.io.*;
  4.  
  5. import static java.lang.Math.max;
  6. import static java.lang.Math.min;
  7. import static java.lang.Math.abs;
  8.  
  9. public class Main {
  10. public static int mod = (int) 1e9 + 7;
  11.  
  12. static class FastReader {
  13.  
  14. public FastReader() {
  15. }
  16.  
  17. String next() {
  18. while (st == null || !st.hasMoreTokens()) {
  19. try {
  20. st = new StringTokenizer(br.readLine());
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. return st.nextToken();
  26. }
  27.  
  28. int nextInt() {
  29. return Integer.parseInt(next());
  30. }
  31.  
  32. long nextLong() {
  33. return Long.parseLong(next());
  34. }
  35.  
  36. double nextDouble() {
  37. return Double.parseDouble(next());
  38. }
  39.  
  40. String nextLine() {
  41. String str = "";
  42. try {
  43. str = br.readLine().trim();
  44. } catch (Exception e) {
  45. e.printStackTrace();
  46. }
  47. return str;
  48. }
  49. }
  50.  
  51. static class FastWriter {
  52. private final BufferedWriter bw;
  53.  
  54. public FastWriter() {
  55. this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
  56. }
  57.  
  58. public void print(Object object) throws IOException {
  59. bw.append("" + object);
  60. }
  61.  
  62. public void println(Object object) throws IOException {
  63. print(object);
  64. bw.append("\n");
  65. }
  66.  
  67. public void println() throws IOException {
  68. bw.append("\n");
  69. }
  70.  
  71. public void close() throws IOException {
  72. bw.close();
  73. }
  74.  
  75. public void printLongArr(long[] arr) throws IOException {
  76. for (long ele : arr) {
  77. print(ele + " ");
  78. }
  79. println();
  80. }
  81.  
  82. public void printIntArr(int[] arr) throws IOException {
  83. for (int ele : arr) {
  84. print(ele + " ");
  85. }
  86. println();
  87. }
  88. }
  89.  
  90. public static void bfs(int node, int[] dist, int n, BitSet[] graph) {
  91. BitSet visited = new BitSet();
  92. BitSet frontier = new BitSet(); // queue
  93.  
  94. Arrays.fill(dist, -1);
  95. dist[node] = 0;
  96. int level = 0;
  97. frontier.set(node);
  98. visited.set(node);
  99. while (!frontier.isEmpty()) {
  100. BitSet nextLevel = new BitSet();
  101. for (int u = frontier.nextSetBit(0); u != -1; u = frontier.nextSetBit(u + 1)) { // level order traversal
  102. BitSet neighbours = (BitSet) graph[u].clone();
  103. neighbours.andNot(visited);
  104. nextLevel.or(neighbours);
  105. }
  106. level++;
  107. for (int v = nextLevel.nextSetBit(0); v != -1; v = nextLevel.nextSetBit(v + 1)) {
  108. dist[v] = level;
  109. }
  110. visited.or(nextLevel);
  111. frontier = nextLevel;
  112. }
  113. }
  114.  
  115. public static String bfs2(int node, BitSet[] graph) {
  116. BitSet visited = new BitSet();
  117. BitSet frontier = new BitSet(); // queue
  118.  
  119. int level = 0;
  120. frontier.set(node);
  121. visited.set(node);
  122. while (!frontier.isEmpty()) {
  123. level++;
  124. BitSet nextLevel = new BitSet();
  125. for (int u = frontier.nextSetBit(0); u != -1; u = frontier.nextSetBit(u + 1)) { // level order traversal
  126. BitSet neighbours = (BitSet) graph[u].clone();
  127. if(neighbours.get(node)) {
  128. return String.valueOf(level);
  129. }
  130. neighbours.andNot(visited);
  131. nextLevel.or(neighbours);
  132. }
  133. visited.or(nextLevel);
  134. frontier = nextLevel;
  135. }
  136. return "NO WAY";
  137. }
  138.  
  139. public static void main
  140. (String[] args) {
  141. try {
  142. FastReader fin = new FastReader();
  143. FastWriter fout = new FastWriter();
  144.  
  145. int n = fin.nextInt();
  146. BitSet[] graph = new BitSet[n];
  147. // BitSet[] rev = new BitSet[n];
  148. for (int i = 0; i < n; i++) {
  149. graph[i] = new BitSet(n);
  150. // rev[i] = new BitSet(n);
  151. }
  152. for (int i = 0; i < n; i++) {
  153. for (int j = 0; j < n; j++) {
  154. int l = fin.nextInt();
  155. if (l == 1) {
  156. graph[i].set(j);
  157. // rev[j].set(i);
  158. }
  159. }
  160. }
  161. // int[] dist = new int[n];
  162. for (int i = 0; i < n; i++) {
  163. fout.println(bfs2(i, graph));
  164. // bfs(i,dist,n,rev);
  165. // int currentAns = Integer.MAX_VALUE;
  166. // for (int v = graph[i].nextSetBit(0); v != -1; v = graph[i].nextSetBit(v + 1)) {
  167. // if (dist[v] != -1) {
  168. // currentAns = Math.min(currentAns, 1 + dist[v]);
  169. // }
  170. // }
  171. // if (currentAns == Integer.MAX_VALUE) {
  172. // fout.println("NO WAY");
  173. // } else {
  174. // fout.println(currentAns);
  175. // }
  176. }
  177. fout.close();
  178. } catch (Exception e) {
  179. e.printStackTrace();
  180. return;
  181. }
  182. }
  183. }
  184.  
Success #stdin #stdout 0.14s 55536KB
stdin
5
0 1 0 0 1
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
1 0 0 0 0
stdout
2
5
5
5
2