Untitled
unknown
plain_text
a year ago
1.6 kB
1
Indexable
Never
package he_thong_dien; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; public class Solution { static int n, m, h, res, max; static int[][] a; static int[] b, vt; static void dfs(int dinh, int c) { b[dinh] = c; for (int i = 0; i < a[dinh][n]; i++) { if(b[a[dinh][i]] == -1){ dfs(a[dinh][i], c+ 1); }else if(b[a[dinh][i]] > c + 1){ dfs(a[dinh][i], c+ 1); } } } public static void main(String[] args) { try { System.setIn(new FileInputStream("input.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } Scanner scanner = new Scanner(System.in); int test = scanner.nextInt(); for (int t = 0; t < test; t++) { n = scanner.nextInt(); m = scanner.nextInt(); h = scanner.nextInt(); a = new int[n][n + 1]; b = new int[n]; vt = new int[m]; for (int i = 0; i < n; i++) { b[i] = -1; } for (int i = 0; i < m; i++) { int x = scanner.nextInt(); b[x] = 0; vt[i] = x; } for (int i = 0; i < h; i++) { int x = scanner.nextInt(), y = scanner.nextInt(); a[x][a[x][n]] = y; a[x][n]++; a[y][a[y][n]] = x; a[y][n]++; } res = 0; max = 0; for (int i = 0; i < m; i++) { dfs(vt[i], 0); } for (int i = 0; i < n; i++) { if (b[i] > max) { max = b[i]; res = i; } if(b[i] == -1){ res = i; break; } } System.out.println(res); } scanner.close(); } }