import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Set; import java.io.IOException; import java.io.InputStreamReader; import java.util.TreeSet; import java.util.StringTokenizer; import java.util.Queue; import java.io.BufferedReader; import java.util.LinkedList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class mis { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Mistrzostwa solver = new Mistrzostwa(); solver.solve(1, in, out); out.close(); } static class Mistrzostwa { public void solve(int testNumber, InputReader in, PrintWriter out) { int N = in.nextInt(); int M = in.nextInt(); int D = in.nextInt(); Set<Integer> adj[] = (Set<Integer>[]) new TreeSet[N]; int degree[] = new int[N]; for (int i = 0; i < N; i++) adj[i] = new TreeSet<>(); for (int i = 0; i < M; i++) { int from = in.nextInt() - 1; int to = in.nextInt() - 1; adj[from].add(to); adj[to].add(from); degree[from]++; degree[to]++; } Queue<Integer> toDelete = new LinkedList<>(); boolean onQueue[] = new boolean[N]; for (int i = 0; i < N; i++) { if (degree[i] < D) { toDelete.add(i); onQueue[i] = true; } } while (!toDelete.isEmpty()) { int v = toDelete.remove(); LinkedList<Integer> dd = new LinkedList<>(); for (int w : adj[v]) { degree[v]--; degree[w]--; dd.add(w); if (degree[w] < D && !onQueue[w]) toDelete.add(w); } for (int w : dd) { adj[v].remove(w); adj[w].remove(v); } } int count = 1; int cc[] = new int[N]; boolean visited[] = new boolean[N]; Queue<Integer> bfs = new LinkedList<>(); for (int v = 0; v < N; v++) { if (visited[v] || degree[v] < D) continue; bfs.add(v); visited[v] = true; cc[v] = count; while (!bfs.isEmpty()) { int x = bfs.remove(); for (int y : adj[x]) { if (!visited[y]) { bfs.add(y); visited[y] = true; cc[y] = count; } } } count++; } int cnt[] = new int[count]; for (int i = 0; i < N; i++) { cnt[cc[i]]++; } int max = 0; int max_idx = -1; for (int c = 1; c < count; c++) { if (cnt[c] > max) { max = cnt[c]; max_idx = c; } } if (max <= 1) { out.println("NIE"); } else { out.println(max); for (int i = 0; i < N; i++) { if (cc[i] == max_idx) out.print((i + 1) + " "); } out.println(); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Set; import java.io.IOException; import java.io.InputStreamReader; import java.util.TreeSet; import java.util.StringTokenizer; import java.util.Queue; import java.io.BufferedReader; import java.util.LinkedList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class mis { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Mistrzostwa solver = new Mistrzostwa(); solver.solve(1, in, out); out.close(); } static class Mistrzostwa { public void solve(int testNumber, InputReader in, PrintWriter out) { int N = in.nextInt(); int M = in.nextInt(); int D = in.nextInt(); Set<Integer> adj[] = (Set<Integer>[]) new TreeSet[N]; int degree[] = new int[N]; for (int i = 0; i < N; i++) adj[i] = new TreeSet<>(); for (int i = 0; i < M; i++) { int from = in.nextInt() - 1; int to = in.nextInt() - 1; adj[from].add(to); adj[to].add(from); degree[from]++; degree[to]++; } Queue<Integer> toDelete = new LinkedList<>(); boolean onQueue[] = new boolean[N]; for (int i = 0; i < N; i++) { if (degree[i] < D) { toDelete.add(i); onQueue[i] = true; } } while (!toDelete.isEmpty()) { int v = toDelete.remove(); LinkedList<Integer> dd = new LinkedList<>(); for (int w : adj[v]) { degree[v]--; degree[w]--; dd.add(w); if (degree[w] < D && !onQueue[w]) toDelete.add(w); } for (int w : dd) { adj[v].remove(w); adj[w].remove(v); } } int count = 1; int cc[] = new int[N]; boolean visited[] = new boolean[N]; Queue<Integer> bfs = new LinkedList<>(); for (int v = 0; v < N; v++) { if (visited[v] || degree[v] < D) continue; bfs.add(v); visited[v] = true; cc[v] = count; while (!bfs.isEmpty()) { int x = bfs.remove(); for (int y : adj[x]) { if (!visited[y]) { bfs.add(y); visited[y] = true; cc[y] = count; } } } count++; } int cnt[] = new int[count]; for (int i = 0; i < N; i++) { cnt[cc[i]]++; } int max = 0; int max_idx = -1; for (int c = 1; c < count; c++) { if (cnt[c] > max) { max = cnt[c]; max_idx = c; } } if (max <= 1) { out.println("NIE"); } else { out.println(max); for (int i = 0; i < N; i++) { if (cc[i] == max_idx) out.print((i + 1) + " "); } out.println(); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } } |