import java.io.InputStream; import java.io.PrintStream; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class mis { public static void main(String[] args) { InputStream in = System.in; PrintStream out = System.out; calculate(in, out); } public static int calculate(InputStream in, PrintStream out) { /** Read input */ Scanner scanner = new Scanner(in); int n = Integer.parseInt(scanner.next()); int m = Integer.parseInt(scanner.next()); int d = Integer.parseInt(scanner.next()); City[] cities = initCities(n); readRoads(scanner, m, cities); scanner.close(); /** Find graph */ City[] stack = new City[m * 2]; validateAll(cities, d, stack); City maxGraph = findMaxGraph(cities, d, stack); /** Save result */ printResult(out, maxGraph); return 0; } private static City[] initCities(int n) { City[] cities = new City[n]; for (int i = 0; i < n; i++) { cities[i] = new City(i + 1); } return cities; } private static void readRoads(Scanner scanner, int m, City[] cities) { for (int i = 0; i < m; i++) { scanner.nextLine(); int a = Integer.parseInt(scanner.next()) - 1; int b = Integer.parseInt(scanner.next()) - 1; cities[a].add(cities[b]); cities[b].add(cities[a]); } } private static void validateAll(City[] cities, int d, City[] stack) { for (City city: cities) { validateCityToRecursive(d, city, stack); } } private static void validateCityToRecursive(int d, City representative, City[] stack) { int idx = 0; stack[idx++] = representative; City city; while (idx > 0) { city = stack[--idx]; if (!city.visited && city.size < d) { city.visited = true; for (City destinationCity : city.roads) { destinationCity.size--; } for (City destinationCity : city.roads) { stack[idx++] = destinationCity; } } } } private static City findMaxGraph(City[] cities, int d, City[] stack) { int maxGraphSize = 0; City maxGraph = null; for (City representative: cities) { if (!representative.visited) { addCityToGraphRecurive(representative, d, stack); if (representative.size > maxGraphSize) { maxGraph = representative; } } } return maxGraph; } private static void addCityToGraphRecurive(City representative, int d, City[] stack) { int idx = 0; stack[idx++] = representative; City city; while (idx > 0) { city = stack[--idx]; if (!city.visited) { city.visited = true; if (city.size >= d) { representative.graph.add(city); for (City destinationCity: city.roads) { stack[idx++] = destinationCity; } } } } } private static void printResult(PrintStream out, City maxGraph) { if (maxGraph == null) { out.print("NIE"); } else { int[] g = getSortedGraph(maxGraph); printGraph(out, g); } } private static int[] getSortedGraph(City maxGraph) { int g[] = new int[maxGraph.graph.size()]; int i = 0; for (City c: maxGraph.graph) { g[i++] = c.no; } radixsort(g); return g; } private static void radixsort(int[] input) { final int RADIX = 10; // declare and initialize bucket[] List<Integer>[] bucket = new ArrayList[RADIX]; for (int i = 0; i < bucket.length; i++) { bucket[i] = new ArrayList<Integer>(); } // sort boolean maxLength = false; int tmp = -1, placement = 1; while (!maxLength) { maxLength = true; // split input between lists for (Integer i : input) { tmp = i / placement; bucket[tmp % RADIX].add(i); if (maxLength && tmp > 0) { maxLength = false; } } // empty lists into input array int a = 0; for (int b = 0; b < RADIX; b++) { for (Integer i : bucket[b]) { input[a++] = i; } bucket[b].clear(); } // move to next digit placement *= RADIX; } } private static void printGraph(PrintStream out, int[] g) { out.println(g.length); out.print(g[0]); for (int i = 1; i < g.length; i++) { out.print(" " + g[i]); } } public static class City { public City(int no) { this.no = no; } int no = 0; List<City> roads = new ArrayList<>(); List<City> graph = new ArrayList<>(); boolean visited = false; int size = 0; public void add(City city) { roads.add(city); size++; } } }
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | import java.io.InputStream; import java.io.PrintStream; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class mis { public static void main(String[] args) { InputStream in = System.in; PrintStream out = System.out; calculate(in, out); } public static int calculate(InputStream in, PrintStream out) { /** Read input */ Scanner scanner = new Scanner(in); int n = Integer.parseInt(scanner.next()); int m = Integer.parseInt(scanner.next()); int d = Integer.parseInt(scanner.next()); City[] cities = initCities(n); readRoads(scanner, m, cities); scanner.close(); /** Find graph */ City[] stack = new City[m * 2]; validateAll(cities, d, stack); City maxGraph = findMaxGraph(cities, d, stack); /** Save result */ printResult(out, maxGraph); return 0; } private static City[] initCities(int n) { City[] cities = new City[n]; for (int i = 0; i < n; i++) { cities[i] = new City(i + 1); } return cities; } private static void readRoads(Scanner scanner, int m, City[] cities) { for (int i = 0; i < m; i++) { scanner.nextLine(); int a = Integer.parseInt(scanner.next()) - 1; int b = Integer.parseInt(scanner.next()) - 1; cities[a].add(cities[b]); cities[b].add(cities[a]); } } private static void validateAll(City[] cities, int d, City[] stack) { for (City city: cities) { validateCityToRecursive(d, city, stack); } } private static void validateCityToRecursive(int d, City representative, City[] stack) { int idx = 0; stack[idx++] = representative; City city; while (idx > 0) { city = stack[--idx]; if (!city.visited && city.size < d) { city.visited = true; for (City destinationCity : city.roads) { destinationCity.size--; } for (City destinationCity : city.roads) { stack[idx++] = destinationCity; } } } } private static City findMaxGraph(City[] cities, int d, City[] stack) { int maxGraphSize = 0; City maxGraph = null; for (City representative: cities) { if (!representative.visited) { addCityToGraphRecurive(representative, d, stack); if (representative.size > maxGraphSize) { maxGraph = representative; } } } return maxGraph; } private static void addCityToGraphRecurive(City representative, int d, City[] stack) { int idx = 0; stack[idx++] = representative; City city; while (idx > 0) { city = stack[--idx]; if (!city.visited) { city.visited = true; if (city.size >= d) { representative.graph.add(city); for (City destinationCity: city.roads) { stack[idx++] = destinationCity; } } } } } private static void printResult(PrintStream out, City maxGraph) { if (maxGraph == null) { out.print("NIE"); } else { int[] g = getSortedGraph(maxGraph); printGraph(out, g); } } private static int[] getSortedGraph(City maxGraph) { int g[] = new int[maxGraph.graph.size()]; int i = 0; for (City c: maxGraph.graph) { g[i++] = c.no; } radixsort(g); return g; } private static void radixsort(int[] input) { final int RADIX = 10; // declare and initialize bucket[] List<Integer>[] bucket = new ArrayList[RADIX]; for (int i = 0; i < bucket.length; i++) { bucket[i] = new ArrayList<Integer>(); } // sort boolean maxLength = false; int tmp = -1, placement = 1; while (!maxLength) { maxLength = true; // split input between lists for (Integer i : input) { tmp = i / placement; bucket[tmp % RADIX].add(i); if (maxLength && tmp > 0) { maxLength = false; } } // empty lists into input array int a = 0; for (int b = 0; b < RADIX; b++) { for (Integer i : bucket[b]) { input[a++] = i; } bucket[b].clear(); } // move to next digit placement *= RADIX; } } private static void printGraph(PrintStream out, int[] g) { out.println(g.length); out.print(g[0]); for (int i = 1; i < g.length; i++) { out.print(" " + g[i]); } } public static class City { public City(int no) { this.no = no; } int no = 0; List<City> roads = new ArrayList<>(); List<City> graph = new ArrayList<>(); boolean visited = false; int size = 0; public void add(City city) { roads.add(city); size++; } } } |