import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class mis { private static class IntegerList extends LinkedList<Integer> { } private static enum Status { NOT_VISITED, VISITED, BAD } private static class Holder { public int[] dv; public int[] marker; public Boolean[][] graph; public Status[] status; public int size; public Holder(int n) { dv = new int[n]; marker = new int[n]; graph = new Boolean[n][n]; status = new Status[n]; size = n; for(int i = 0; i < n; ++i) { status[i] = Status.NOT_VISITED; graph[i] = new Boolean[n]; for(int j = 0; j < n; ++j) { graph[i][j] = false; } } } } public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); run(input); } public static void run(BufferedReader input) throws IOException { String[] values = input.readLine().split(" "); int n = Integer.parseInt(values[0]); int m = Integer.parseInt(values[1]); int d = Integer.parseInt(values[2]); Holder h = new Holder(n); for(int i = 0; i < m; ++i) { values = input.readLine().split(" "); int a = Integer.parseInt(values[0]) - 1; int b = Integer.parseInt(values[1]) - 1; h.graph[a][b] = true; h.graph[b][a] = true; ++h.dv[a]; ++h.dv[b]; } solve(h, d); int max = 0; int maxs = 0; int s; IntegerList[] result = new IntegerList[n]; for(int i = 0; i < n; ++i) { s = h.marker[i]; if (s > 0) { if(result[s] == null) { result[s] = new IntegerList(); } result[s].add(i); if(result[s].size() > max) { max = result[s].size(); maxs = s; } } } if (max == 0) { System.out.println("NIE"); } else { System.out.println(max); for(int v : result[maxs]) { System.out.print(v+1 + " "); } } } private static void solve(Holder h, int d) { int s = 1; for(int v = 0; v < h.size; ++v) { if (h.status[v] == Status.NOT_VISITED) { visit(v, v, h, s, d); ++s; } } } private static void visit(int v, int p, Holder h, int s, int d) { if(h.dv[v] >= d) { h.status[v] = Status.VISITED; if (v != p) { if (h.marker[v] == 0) { h.marker[v] = s; } if (h.marker[p] == 0) { h.marker[p] = s; } } for (int u = 0; u < h.size; ++u) { if (h.graph[v][u] && h.status[u] == Status.NOT_VISITED) { visit(u, v, h, s, d); } } } else { h.status[v] = Status.BAD; if (h.marker[v] >= 0) { h.marker[v] = -1; } for (int u = 0; u < h.size; ++u) { if (h.graph[v][u]) { h.graph[v][u] = false; h.graph[u][v] = false; --h.dv[v]; --h.dv[u]; visit(u, v, h, s, d); } } } } }
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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class mis { private static class IntegerList extends LinkedList<Integer> { } private static enum Status { NOT_VISITED, VISITED, BAD } private static class Holder { public int[] dv; public int[] marker; public Boolean[][] graph; public Status[] status; public int size; public Holder(int n) { dv = new int[n]; marker = new int[n]; graph = new Boolean[n][n]; status = new Status[n]; size = n; for(int i = 0; i < n; ++i) { status[i] = Status.NOT_VISITED; graph[i] = new Boolean[n]; for(int j = 0; j < n; ++j) { graph[i][j] = false; } } } } public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); run(input); } public static void run(BufferedReader input) throws IOException { String[] values = input.readLine().split(" "); int n = Integer.parseInt(values[0]); int m = Integer.parseInt(values[1]); int d = Integer.parseInt(values[2]); Holder h = new Holder(n); for(int i = 0; i < m; ++i) { values = input.readLine().split(" "); int a = Integer.parseInt(values[0]) - 1; int b = Integer.parseInt(values[1]) - 1; h.graph[a][b] = true; h.graph[b][a] = true; ++h.dv[a]; ++h.dv[b]; } solve(h, d); int max = 0; int maxs = 0; int s; IntegerList[] result = new IntegerList[n]; for(int i = 0; i < n; ++i) { s = h.marker[i]; if (s > 0) { if(result[s] == null) { result[s] = new IntegerList(); } result[s].add(i); if(result[s].size() > max) { max = result[s].size(); maxs = s; } } } if (max == 0) { System.out.println("NIE"); } else { System.out.println(max); for(int v : result[maxs]) { System.out.print(v+1 + " "); } } } private static void solve(Holder h, int d) { int s = 1; for(int v = 0; v < h.size; ++v) { if (h.status[v] == Status.NOT_VISITED) { visit(v, v, h, s, d); ++s; } } } private static void visit(int v, int p, Holder h, int s, int d) { if(h.dv[v] >= d) { h.status[v] = Status.VISITED; if (v != p) { if (h.marker[v] == 0) { h.marker[v] = s; } if (h.marker[p] == 0) { h.marker[p] = s; } } for (int u = 0; u < h.size; ++u) { if (h.graph[v][u] && h.status[u] == Status.NOT_VISITED) { visit(u, v, h, s, d); } } } else { h.status[v] = Status.BAD; if (h.marker[v] >= 0) { h.marker[v] = -1; } for (int u = 0; u < h.size; ++u) { if (h.graph[v][u]) { h.graph[v][u] = false; h.graph[u][v] = false; --h.dv[v]; --h.dv[u]; visit(u, v, h, s, d); } } } } } |