Untitled
unknown
plain_text
a year ago
2.8 kB
4
Indexable
package lv4; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; class pos1 { int x, y; public pos1(int x, int y) { this.x = x; this.y = y; } } public class bai2 { static int cr, cc, nr, nc, r, c, n; static int[] Data = new int[1000005]; static int front, rear; public bai2() { front = rear = -1; } static void reset() { front = rear = -1; } static public void enQueue(int value) { Data[++rear] = value; } static int deQueue() { return Data[++front]; } static boolean isEmpty() { if (front == rear) { return true; } return false; } static public void BFS(int[][] arr, int start, int[] visited) { enQueue(start); visited[start] = 0; while (!isEmpty()) { int node = deQueue(); for (int i = 0; i < n; i++) { if (arr[node][i] == 1 && visited[i] == 0) { visited[i] = visited[node] + 1; enQueue(i); } } } } static public boolean BFS_cau(int[][] arr, int start, int end, int[] visited) { enQueue(start); visited[start] = 0; while (!isEmpty()) { int node = deQueue(); if (node == end) { return false; } for (int i = 0; i < n; i++) { if (arr[node][i] == 1 && visited[i] == 0) { visited[i] = 1; enQueue(i); } } } return true; } public static void main(String[] args) throws FileNotFoundException { System.setIn(new FileInputStream("Text")); Scanner scanner = new Scanner(System.in); int tc = scanner.nextInt(); for (int Case = 1; Case <= tc; Case++) { reset(); n = scanner.nextInt(); int[][] arr = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { arr[i][j] = scanner.nextInt(); } } int[] result = find(arr); System.out.println(result[0] + " " + result[1] + " " + result[2]); } } public static void reset_visited(int[] visited) { for (int i = 0; i < n; i++) { visited[i] = 0; } } public static int[] find(int[][] arr) { int vung = 0; int soluong = 0; int cau = 0; int[] visited = new int[n]; boolean[][] check = new boolean[n][n]; for (int i = 0; i < n; i++) { if (visited[i] == 0) { vung++; BFS(arr, i, visited); } } for (int i = 0; i < n; i++) { if (visited[i] == 0) { soluong++; } } reset_visited(visited); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (arr[i][j] == 1 && check[i][j] == false) { arr[i][j] = 0; reset(); check[i][j] = check[j][i] = true; reset_visited(visited); if (BFS_cau(arr, i, j, visited)) { cau++; } arr[i][j] = 1; } } } int[] result = { vung, soluong, cau }; return result; } }
Editor is loading...