village
unknown
plain_text
2 years ago
1.7 kB
9
Indexable
package practise;
import java.util.Scanner;
public class village {
static int bound, iso, n;
static boolean[] visit;
private static int checkBridge(int[][] road, int n){
boolean[] checked = new boolean[n];
int result = 0;
for(int i = 0; i < n; i++){
if(!checked[i]){
int count = 0, x = 0, y = 0;
boolean isIso = true;
for(int j = 0; j < n; j++){
if(road[i][j] == 1){
count++;
x = i; y = j;
isIso = false;
}
}
if(isIso) iso++;
if(count == 1){
checked[y] = true;
result++;
}
}
}
return result;
}
private static void check(int[][] road, int x) {
visit[x] = true;
for(int i = 0; i < n; i++) {
if(road[x][i] == 1) {
if(!visit[i]) {
check(road, i);
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int testcase = sc.nextInt();
for(int tc = 0; tc <= testcase; tc++){
n = sc.nextInt();
int[][] road = new int[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
road[i][j] = sc.nextInt();
}
}
//kiem tra cau + iso
iso = 0;
int bridge = checkBridge(road, n);
// int bridgee = 0;
visit = new boolean[n];
bound = 0;
for(int i = 0; i < n; i++){
for(int j = i; j < n; j++){
if(road[i][j] == 1){
if(!visit[i]) {
bound++;
visit[i] = true;
}
if(!visit[j])
check(road, j);
}
}
}
iso = 0;
for(int i = 0; i < n; i++) {
if(!visit[i]) iso++;
}
System.out.println((bound + iso)+ " " + iso + " " + bridge);
}
}
}Editor is loading...