Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import algorithms.*;
- import java.util.*;
- class Main {
- static ArrayList<ArrayList<Integer>> g;
- static int m, n;
- public static void main(String[] args) {
- // Uncomment this line if you want to read from a file
- In.open("diff/test1.in");
- Out.compareTo("diff/test1.out");
- int t = In.readInt();
- for (int i = 0; i < t; i++) {
- testCase();
- }
- In.close();
- }
- public static void testCase() {
- // Input using In.java class
- n = In.readInt();
- m = In.readInt();
- g = new ArrayList<>();
- for (int i = 0; i < n; i++) {
- g.add(new ArrayList<>());
- }
- for (int i = 0; i < m; i++) {
- int u = In.readInt();
- int v = In.readInt();
- g.get(u).add(v);
- g.get(v).add(u);
- }
- int ZHKs = countZHK(-1);
- String result = "";
- for (int i = 0; i < n; i++) {
- if (countZHK(i) > ZHKs) {
- result += String.format("%d ",i);
- }
- }
- Out.println(result.equals("") ? -1 : result);
- }
- public static int countZHK(int without) {
- boolean[] vis = new boolean[n];
- int ZHKs = 0;
- for (int i = 0; i < n; i++) {
- if (!vis[i] && i != without) {
- ZHKs++;
- DFS(i, vis, without);
- }
- }
- return ZHKs;
- }
- public static void DFS(int v, boolean[] vis, int without) {
- vis[v] = true;
- for (int u : g.get(v)) {
- if (u != without && !vis[u]) {
- DFS(u, vis, without);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement