import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Scanner; import java.util.Set; public class mis { private static List<List<Integer>> v; private static boolean[] visited; public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int d = in.nextInt(); int[] a = new int[m]; int[] b = new int[m]; v = new ArrayList<List<Integer>>(n); for (int i = 0; i < n; i++) { v.add(new LinkedList<Integer>()); } int[] rCnt = new int[n]; for (int i = 0; i < m; i++) { a[i] = in.nextInt() - 1; b[i] = in.nextInt() - 1; rCnt[a[i]]++; rCnt[b[i]]++; v.get(a[i]).add(b[i]); v.get(b[i]).add(a[i]); } for (int i = 0; i < n; i++) { if (rCnt[i] < d) { v.get(i).clear(); for (int j = 0; j < n; j++) { v.get(j).remove(new Integer(i)); } } } visited = new boolean[n]; for (int i = 0; i < n; i++) { visited[i] = false; } List<Set<Integer>> graphs = new LinkedList<Set<Integer>>(); for (int i = 0; i < n; i++) { if (v.get(i).size() > 0 && !visited[i]) { Set<Integer> graph = new HashSet<Integer>(); graphs.add(graph); gotoNode(i, graph); } } int maxGraphSize = 0; Set<Integer> maxSet = null; for (Set<Integer> set : graphs) { if (set.size() > maxGraphSize) { maxGraphSize = set.size(); maxSet = set; } } if (maxSet != null) { System.out.println(maxGraphSize); for (Integer integer : maxSet) { System.out.print((integer + 1) + " "); } } else { System.out.println("NIE"); } } private static boolean gotoNode(int i, Set<Integer> graph) { graph.add(i); visited[i] = true; for (Integer integer : v.get(i)) { if (!visited[integer]) { gotoNode(integer, graph); } } return false; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Scanner; import java.util.Set; public class mis { private static List<List<Integer>> v; private static boolean[] visited; public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int d = in.nextInt(); int[] a = new int[m]; int[] b = new int[m]; v = new ArrayList<List<Integer>>(n); for (int i = 0; i < n; i++) { v.add(new LinkedList<Integer>()); } int[] rCnt = new int[n]; for (int i = 0; i < m; i++) { a[i] = in.nextInt() - 1; b[i] = in.nextInt() - 1; rCnt[a[i]]++; rCnt[b[i]]++; v.get(a[i]).add(b[i]); v.get(b[i]).add(a[i]); } for (int i = 0; i < n; i++) { if (rCnt[i] < d) { v.get(i).clear(); for (int j = 0; j < n; j++) { v.get(j).remove(new Integer(i)); } } } visited = new boolean[n]; for (int i = 0; i < n; i++) { visited[i] = false; } List<Set<Integer>> graphs = new LinkedList<Set<Integer>>(); for (int i = 0; i < n; i++) { if (v.get(i).size() > 0 && !visited[i]) { Set<Integer> graph = new HashSet<Integer>(); graphs.add(graph); gotoNode(i, graph); } } int maxGraphSize = 0; Set<Integer> maxSet = null; for (Set<Integer> set : graphs) { if (set.size() > maxGraphSize) { maxGraphSize = set.size(); maxSet = set; } } if (maxSet != null) { System.out.println(maxGraphSize); for (Integer integer : maxSet) { System.out.print((integer + 1) + " "); } } else { System.out.println("NIE"); } } private static boolean gotoNode(int i, Set<Integer> graph) { graph.add(i); visited[i] = true; for (Integer integer : v.get(i)) { if (!visited[integer]) { gotoNode(integer, graph); } } return false; } } |