Untitled

 avatar
unknown
plain_text
a year ago
1.3 kB
5
Indexable
public class Solution {

	static int[][] map;
	static int[] visited;
	static Queue<Integer> qt;
	static int[] dx = { -1, 0, 1, 0 };
	static int[] dy = { 0, 1, 0, -1 };

	public static void main(String args[]) throws Exception {
		Scanner sc = new Scanner(System.in);
		qt = new Queue<Integer>();

		int T = sc.nextInt();
		for (int tc = 1; tc <= T; tc++) {
			int n = sc.nextInt();

			map = new int[n][n];
			visited = new int[n];
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					map[i][j] = sc.nextInt();
				}
			}

			int ans = 0;
			int maxAns = 0;
			for (int i = 0; i < n; i++) {
				visited = new int[n];
				visited[i] = 1;
				int count = 0;
				for (int j = 0; j < n; j++) {
					if (visited[j] == 0) {
						count++;
						bfs(j);
					}
				}

				int maxCount = 1;
				if (count > maxCount) {
					if (maxAns < count) {
						maxAns = count;
						ans = i + 1;
					}
				}
			}

			System.out.println(ans);

		}
	}

	private static void bfs(int u) throws Exception {
		qt.reset();
		qt.enQueue(u);

		int tmp = 0;
		while (!qt.isEmpty()) {
			tmp = qt.peek();
			qt.deQueue();

			for (int i = 0; i < map.length; i++) {
				if (visited[i] == 0) {
					if (map[tmp][i] == 1) {
						visited[i] = 1;
						qt.enQueue(i);
					}
				}
			}
		}
	}
Editor is loading...
Leave a Comment