有老哥是第二题数组越界的嘛,找不到问题,是我太菜了吗
下面是代码
import java.util.*; public class Test11 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); List<List<Integer>> nums = new ArrayList<>(); int[][] dp = new int[n][n]; for(int i=0;i<m;i++){ List<Integer> tmp = new ArrayList<>(); int x = sc.nextInt(); for(int j=0;j<x;j++) tmp.add(sc.nextInt()); nums.add(tmp); } for(int k=0;k<nums.size();k++){ List<Integer> tmp = nums.get(k); for(int i=0;i<tmp.size();i++) { for (int j = 0; j < tmp.size(); j++) { if (i == j) continue; dp[tmp.get(i)][tmp.get(j)] = 1; } } } Deque<Integer> deque = new ArrayDeque<>(); deque.addLast(0); int res = 0; dp[0][0] = 1; while(!deque.isEmpty()){ int num = deque.pollFirst();; res++; for(int i=0;i<n;i++){ if(i==num) continue; if(dp[num][i]==1){ if(dp[i][i]==0){ deque.addLast(i); dp[i][i] = 1; } } } } System.out.println(res); } }
全部评论
(2) 回帖