import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.Stack; import java.util.StringTokenizer; public class mis { public static String solve(Reader in) throws IOException { final int cityCount = in.nextInt(); final int roadCount = in.nextInt(); final int minRoad = in.nextInt(); Map<Integer, Set<Integer>> connections = new HashMap<>(); for (int i=0; i<roadCount; i++) { int firstCity = in.nextInt(); int secondCity = in.nextInt(); Set<Integer> roadsFirst = connections.get(firstCity); if (roadsFirst == null) { connections.put(firstCity, roadsFirst = new HashSet<>()); } roadsFirst.add(secondCity); Set<Integer> roadsSecond = connections.get(secondCity); if (roadsSecond == null) { connections.put(secondCity, roadsSecond = new HashSet<>()); } roadsSecond.add(firstCity); } // remove all cities with not enough roads Stack<Integer> stack = new Stack<>(); Set<Integer> used = new HashSet<>(); for (Integer city: new HashSet<>(connections.keySet())) { Set<Integer> kids_city = connections.get(city); if (kids_city != null && kids_city.size() < minRoad) { stack.add(city); used.add(city); while (!stack.isEmpty()) { Integer cc = stack.pop(); Set<Integer> kids_cc = connections.get(cc); connections.remove(cc); if (kids_cc != null) { for (Integer tt: kids_cc) { if (!used.contains(tt)) { Set<Integer> kids_tt = connections.get(tt); if (kids_tt != null) { kids_tt.remove(cc); if (kids_tt.size() < minRoad) { stack.push(tt); } } } } } } } } // decide on the biggest subset used = new HashSet<>(); Set<Integer> bestSubset = new HashSet<>(); stack = new Stack<>(); for (Integer city: connections.keySet()) { if (!used.contains(city)) { Set<Integer> subset = new HashSet<>(); stack.push(city); used.add(city); while (!stack.isEmpty()) { Integer cc = stack.pop(); subset.add(cc); for (Integer tt: connections.get(cc)) { if (!used.contains(tt)) { stack.push(tt); used.add(tt); } } } if (subset.size() > bestSubset.size()) { bestSubset = subset; } } } if (bestSubset.size() == 0) { return "NIE\n"; } StringBuilder sb = new StringBuilder(); sb.append(bestSubset.size()).append("\n"); List<Integer> bb = new ArrayList<>(bestSubset); Collections.sort(bb); for (Integer city: bb) { sb.append(city).append(" "); } sb.append("\n"); return sb.toString(); } public static void main(String[] args) throws IOException { Reader reader = new Reader(System.in); Writer writer = new Writer(System.out); String solution = solve(reader); writer.println(solution); writer.flush(); } /** Class for buffered reading int, long and double values */ public static class Reader { BufferedReader reader; StringTokenizer tokenizer; public Reader(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } /** get next word */ String next() throws IOException { while (!tokenizer.hasMoreTokens()) { String str = reader.readLine(); if (str == null) { return null; } tokenizer = new StringTokenizer(str); } return tokenizer.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } } /** Class for buffered writing int and double values */ public static class Writer { BufferedWriter writer; public Writer(OutputStream out) { writer = new BufferedWriter(new OutputStreamWriter(out), 512); } void print(char c) throws IOException { writer.write(c); } void print(String str) throws IOException { writer.write(str); } void println(String str) throws IOException { writer.write(str); writer.write("\n"); } void println() throws IOException { writer.write("\n"); } void flush() throws IOException { writer.flush(); } @Override protected void finalize() { try { writer.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
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 202 203 204 205 206 207 | import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.Stack; import java.util.StringTokenizer; public class mis { public static String solve(Reader in) throws IOException { final int cityCount = in.nextInt(); final int roadCount = in.nextInt(); final int minRoad = in.nextInt(); Map<Integer, Set<Integer>> connections = new HashMap<>(); for (int i=0; i<roadCount; i++) { int firstCity = in.nextInt(); int secondCity = in.nextInt(); Set<Integer> roadsFirst = connections.get(firstCity); if (roadsFirst == null) { connections.put(firstCity, roadsFirst = new HashSet<>()); } roadsFirst.add(secondCity); Set<Integer> roadsSecond = connections.get(secondCity); if (roadsSecond == null) { connections.put(secondCity, roadsSecond = new HashSet<>()); } roadsSecond.add(firstCity); } // remove all cities with not enough roads Stack<Integer> stack = new Stack<>(); Set<Integer> used = new HashSet<>(); for (Integer city: new HashSet<>(connections.keySet())) { Set<Integer> kids_city = connections.get(city); if (kids_city != null && kids_city.size() < minRoad) { stack.add(city); used.add(city); while (!stack.isEmpty()) { Integer cc = stack.pop(); Set<Integer> kids_cc = connections.get(cc); connections.remove(cc); if (kids_cc != null) { for (Integer tt: kids_cc) { if (!used.contains(tt)) { Set<Integer> kids_tt = connections.get(tt); if (kids_tt != null) { kids_tt.remove(cc); if (kids_tt.size() < minRoad) { stack.push(tt); } } } } } } } } // decide on the biggest subset used = new HashSet<>(); Set<Integer> bestSubset = new HashSet<>(); stack = new Stack<>(); for (Integer city: connections.keySet()) { if (!used.contains(city)) { Set<Integer> subset = new HashSet<>(); stack.push(city); used.add(city); while (!stack.isEmpty()) { Integer cc = stack.pop(); subset.add(cc); for (Integer tt: connections.get(cc)) { if (!used.contains(tt)) { stack.push(tt); used.add(tt); } } } if (subset.size() > bestSubset.size()) { bestSubset = subset; } } } if (bestSubset.size() == 0) { return "NIE\n"; } StringBuilder sb = new StringBuilder(); sb.append(bestSubset.size()).append("\n"); List<Integer> bb = new ArrayList<>(bestSubset); Collections.sort(bb); for (Integer city: bb) { sb.append(city).append(" "); } sb.append("\n"); return sb.toString(); } public static void main(String[] args) throws IOException { Reader reader = new Reader(System.in); Writer writer = new Writer(System.out); String solution = solve(reader); writer.println(solution); writer.flush(); } /** Class for buffered reading int, long and double values */ public static class Reader { BufferedReader reader; StringTokenizer tokenizer; public Reader(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } /** get next word */ String next() throws IOException { while (!tokenizer.hasMoreTokens()) { String str = reader.readLine(); if (str == null) { return null; } tokenizer = new StringTokenizer(str); } return tokenizer.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } } /** Class for buffered writing int and double values */ public static class Writer { BufferedWriter writer; public Writer(OutputStream out) { writer = new BufferedWriter(new OutputStreamWriter(out), 512); } void print(char c) throws IOException { writer.write(c); } void print(String str) throws IOException { writer.write(str); } void println(String str) throws IOException { writer.write(str); writer.write("\n"); } void println() throws IOException { writer.write("\n"); } void flush() throws IOException { writer.flush(); } @Override protected void finalize() { try { writer.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } |