Linux发行版的数量
2025-11-24 本文已影响0人
何以解君愁
Linux 操作系统有多个发行版,distrowatch.com 提供了各个发行版的资料。这些发行版互相存在关联,例如 Ubuntu 基于 Debian 开发,而 Mint 又基于 Ubuntu 开发,那么我们认为 Mint 同 Debian 也存在关联。
发行版集是一个或多个相关存在关联的操作系统发行版,集合内不包含没有关联的发行版。 给你一个 n x n 的关联矩阵 isConnected ,其中 isConnected[i][j] = 1 表示第 i 个发行版和第 j 个发行版直接关联,而 isConnected[i][j] = 0 表示二者不直接相连。
返回最大的发行版集中发行版的数量。
输入描述:第一行输入发行版的总数量 N,之后每行表示各发行版间是否直接相关。
输出最大的发行版集中发行版的数量输入: 4 1 1 0 0 1 1 1 0 0 1 1 0 0 0 0 1 输出:3
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] isConnected = new int[n][n];
for(int i =0;i < n;i++){
for(int j = 0;j < n;j++){
isConnected[i][j] = sc.nextInt();
}
}
int res = 0;
boolean[] check = new boolean[n];
for(int i = 0;i < n;i++){
if(!check[i]){
int count = 0;
count = dfs(isConnected,check,i);
res = Math.max(res,count);
}
}
System.out.print(res);
}
public static int dfs(int[][] isConnected,boolean[] check,int i){
check[i] = true;
int count = 1;
for(int j = 0;j < isConnected.length;j++){
if(isConnected[i][j] == 1&&!check[j]){
count += dfs(isConnected,check,j);
}
}
return count;
}
}