import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.util.Comparator; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; public class mis { private static class City { final int id; Set<City> neighbours; boolean visited; public City(int id) { this.id = id; neighbours = new HashSet<>(); visited = false; } @Override public int hashCode() { return id; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; City other = (City) obj; if (id != other.id) return false; return true; } } public static void main(String[] args) { // TODO Auto-generated method stub makeResult(); System.exit(0); } private static void makeResult() { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); StreamTokenizer st = new StreamTokenizer(bufferedReader); try { st.nextToken(); int n = (int) st.nval; st.nextToken(); int m = (int) st.nval; st.nextToken(); int d = (int) st.nval; City[] cities = new City[n + 1]; for (int i = 1; i <= n; i++) { cities[i] = new City(i); } int a, b = 0; for (int i = 1; i <= m; i++) { st.nextToken(); a = (int) st.nval; st.nextToken(); b = (int) st.nval; cities[a].neighbours.add(cities[b]); cities[b].neighbours.add(cities[a]); } bufferedReader.close(); SortedSet<City> atLastD = new TreeSet<mis.City>(Comparator.comparing(c -> c.id)); for (int i = 1; i <= n; i++) { if (cities[i].neighbours.size() < d) atLastD.add(cities[i]); } while (!atLastD.isEmpty()) { City city = atLastD.first(); for (City c : city.neighbours) { c.neighbours.remove(city); if (c.neighbours.size() < d) atLastD.add(c); } city.neighbours = new HashSet<mis.City>(); atLastD.remove(city); } int elements = 0; for (int i = 1; i <= n; i++) { if (cities[i].neighbours.size() >= d) elements++; } City[] world = new City[elements]; elements = 0; for (int i = 1; i <= n; i++) { if (cities[i].neighbours.size() >= d) world[elements++] = cities[i]; } List<City> result = new LinkedList<>(); for (int i = 0; i < elements; i++) { if (!world[i].visited) { List<City> actualResult = new LinkedList<>(); List<City> queue = new LinkedList<>(); world[i].visited = true; queue.add(world[i]); actualResult.add(world[i]); while (!queue.isEmpty()) { City city = queue.remove(0); for (City c : city.neighbours) { if (!c.visited) { c.visited = true; queue.add(c); actualResult.add(c); } } } if (actualResult.size() > result.size()) result = actualResult; } } if (result.size() < 2) { System.out.println("NIE"); return; } System.out.println(result.size()); result.sort(Comparator.comparing(c -> c.id)); result.forEach(c -> System.out.print(c.id + " ")); } catch (Exception e) { 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 | import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.util.Comparator; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; public class mis { private static class City { final int id; Set<City> neighbours; boolean visited; public City(int id) { this.id = id; neighbours = new HashSet<>(); visited = false; } @Override public int hashCode() { return id; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; City other = (City) obj; if (id != other.id) return false; return true; } } public static void main(String[] args) { // TODO Auto-generated method stub makeResult(); System.exit(0); } private static void makeResult() { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); StreamTokenizer st = new StreamTokenizer(bufferedReader); try { st.nextToken(); int n = (int) st.nval; st.nextToken(); int m = (int) st.nval; st.nextToken(); int d = (int) st.nval; City[] cities = new City[n + 1]; for (int i = 1; i <= n; i++) { cities[i] = new City(i); } int a, b = 0; for (int i = 1; i <= m; i++) { st.nextToken(); a = (int) st.nval; st.nextToken(); b = (int) st.nval; cities[a].neighbours.add(cities[b]); cities[b].neighbours.add(cities[a]); } bufferedReader.close(); SortedSet<City> atLastD = new TreeSet<mis.City>(Comparator.comparing(c -> c.id)); for (int i = 1; i <= n; i++) { if (cities[i].neighbours.size() < d) atLastD.add(cities[i]); } while (!atLastD.isEmpty()) { City city = atLastD.first(); for (City c : city.neighbours) { c.neighbours.remove(city); if (c.neighbours.size() < d) atLastD.add(c); } city.neighbours = new HashSet<mis.City>(); atLastD.remove(city); } int elements = 0; for (int i = 1; i <= n; i++) { if (cities[i].neighbours.size() >= d) elements++; } City[] world = new City[elements]; elements = 0; for (int i = 1; i <= n; i++) { if (cities[i].neighbours.size() >= d) world[elements++] = cities[i]; } List<City> result = new LinkedList<>(); for (int i = 0; i < elements; i++) { if (!world[i].visited) { List<City> actualResult = new LinkedList<>(); List<City> queue = new LinkedList<>(); world[i].visited = true; queue.add(world[i]); actualResult.add(world[i]); while (!queue.isEmpty()) { City city = queue.remove(0); for (City c : city.neighbours) { if (!c.visited) { c.visited = true; queue.add(c); actualResult.add(c); } } } if (actualResult.size() > result.size()) result = actualResult; } } if (result.size() < 2) { System.out.println("NIE"); return; } System.out.println(result.size()); result.sort(Comparator.comparing(c -> c.id)); result.forEach(c -> System.out.print(c.id + " ")); } catch (Exception e) { e.printStackTrace(); } } } |