#include <iostream> #include <string> #include <cctype> #include <set> #include <vector> #include <algorithm> #define SIZE_OF_ARRAY(x) (sizeof(x) / sizeof(*(x))) #ifndef _MSC_VER #define __int64 long long #endif typedef unsigned int uint; typedef unsigned __int64 uint64; static char get_char_buffer[4 * 1024]; static std::size_t get_char_buffer_i = SIZE_OF_ARRAY(get_char_buffer); static std::size_t get_char_buffer_n = SIZE_OF_ARRAY(get_char_buffer); static char get_char_prefix = 0; static char get_char_result = 0; int get_char() { get_char_result = 0; if (get_char_prefix) { get_char_result = get_char_prefix; get_char_prefix = 0; } else if (get_char_buffer_n == 0) { get_char_result = std::char_traits<char>::eof(); } else if (get_char_buffer_i < get_char_buffer_n) { get_char_result = get_char_buffer[get_char_buffer_i++]; } else { std::cin.read(get_char_buffer, SIZE_OF_ARRAY(get_char_buffer)); get_char_buffer_i = 0; get_char_buffer_n = std::cin.gcount(); return get_char(); } return get_char_result; } void unget_char() { get_char_prefix = get_char_result; } uint get_uint() { int c; while (isspace(c = get_char())); uint value = (c - '0'); while (isdigit(c = get_char())) { value = (10 * value) + (c - '0'); } return value; } static char put_uint_buffer[16]; void put_uint(uint value) { if (value == 0) { std::cout.put('0'); return; } int i; for (i = SIZE_OF_ARRAY(put_uint_buffer); value != 0; value /= 10) { put_uint_buffer[--i] = '0' + (value % 10); } std::cout.write(put_uint_buffer + i, SIZE_OF_ARRAY(put_uint_buffer) - i); } typedef std::set<uint> uint_set; typedef std::vector<uint> uint_vector; uint_set node_connected_to[200002]; bool visited[200002]; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); uint n = get_uint(); uint m = get_uint(); uint d = get_uint(); for (uint i = 0; i < m; ++i) { uint a = get_uint(); uint b = get_uint(); node_connected_to[a].insert(b); node_connected_to[b].insert(a); } uint_set updated; for (uint id = 1; id <= n; ++id) { updated.insert(id); } while (updated.size() > 0) { uint_set new_updated; for (auto id : updated) { if (node_connected_to[id].size() > 0 && node_connected_to[id].size() < d) // remove { for (auto connected_to : node_connected_to[id]) { node_connected_to[connected_to].erase(id); new_updated.insert(connected_to); } node_connected_to[id].clear(); new_updated.insert(id); } } std::swap(updated, new_updated); } uint_vector best_result; for (uint id = 1; id <= n; ++id) { if (visited[id] || node_connected_to[id].size() == 0) continue; uint_vector result; uint_vector curr, next; result.push_back(id); curr.push_back(id); visited[id] = true; while (curr.size() > 0) { for (auto iid : curr) { for (auto connected_to : node_connected_to[iid]) { if (visited[connected_to]) continue; result.push_back(connected_to); next.push_back(connected_to); visited[connected_to] = true; } } std::swap(next, curr); next.clear(); } if (result.size() > best_result.size()) { std::swap(best_result, result); } } if (best_result.size() > 0) { std::sort(best_result.begin(), best_result.end()); put_uint(best_result.size()); std::cout.put('\n'); for (auto id : best_result) { put_uint(id); std::cout.put(' '); } std::cout.put('\n'); } else { std::cout << "NIE\n"; } return 0; }
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 208 | #include <iostream> #include <string> #include <cctype> #include <set> #include <vector> #include <algorithm> #define SIZE_OF_ARRAY(x) (sizeof(x) / sizeof(*(x))) #ifndef _MSC_VER #define __int64 long long #endif typedef unsigned int uint; typedef unsigned __int64 uint64; static char get_char_buffer[4 * 1024]; static std::size_t get_char_buffer_i = SIZE_OF_ARRAY(get_char_buffer); static std::size_t get_char_buffer_n = SIZE_OF_ARRAY(get_char_buffer); static char get_char_prefix = 0; static char get_char_result = 0; int get_char() { get_char_result = 0; if (get_char_prefix) { get_char_result = get_char_prefix; get_char_prefix = 0; } else if (get_char_buffer_n == 0) { get_char_result = std::char_traits<char>::eof(); } else if (get_char_buffer_i < get_char_buffer_n) { get_char_result = get_char_buffer[get_char_buffer_i++]; } else { std::cin.read(get_char_buffer, SIZE_OF_ARRAY(get_char_buffer)); get_char_buffer_i = 0; get_char_buffer_n = std::cin.gcount(); return get_char(); } return get_char_result; } void unget_char() { get_char_prefix = get_char_result; } uint get_uint() { int c; while (isspace(c = get_char())); uint value = (c - '0'); while (isdigit(c = get_char())) { value = (10 * value) + (c - '0'); } return value; } static char put_uint_buffer[16]; void put_uint(uint value) { if (value == 0) { std::cout.put('0'); return; } int i; for (i = SIZE_OF_ARRAY(put_uint_buffer); value != 0; value /= 10) { put_uint_buffer[--i] = '0' + (value % 10); } std::cout.write(put_uint_buffer + i, SIZE_OF_ARRAY(put_uint_buffer) - i); } typedef std::set<uint> uint_set; typedef std::vector<uint> uint_vector; uint_set node_connected_to[200002]; bool visited[200002]; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); uint n = get_uint(); uint m = get_uint(); uint d = get_uint(); for (uint i = 0; i < m; ++i) { uint a = get_uint(); uint b = get_uint(); node_connected_to[a].insert(b); node_connected_to[b].insert(a); } uint_set updated; for (uint id = 1; id <= n; ++id) { updated.insert(id); } while (updated.size() > 0) { uint_set new_updated; for (auto id : updated) { if (node_connected_to[id].size() > 0 && node_connected_to[id].size() < d) // remove { for (auto connected_to : node_connected_to[id]) { node_connected_to[connected_to].erase(id); new_updated.insert(connected_to); } node_connected_to[id].clear(); new_updated.insert(id); } } std::swap(updated, new_updated); } uint_vector best_result; for (uint id = 1; id <= n; ++id) { if (visited[id] || node_connected_to[id].size() == 0) continue; uint_vector result; uint_vector curr, next; result.push_back(id); curr.push_back(id); visited[id] = true; while (curr.size() > 0) { for (auto iid : curr) { for (auto connected_to : node_connected_to[iid]) { if (visited[connected_to]) continue; result.push_back(connected_to); next.push_back(connected_to); visited[connected_to] = true; } } std::swap(next, curr); next.clear(); } if (result.size() > best_result.size()) { std::swap(best_result, result); } } if (best_result.size() > 0) { std::sort(best_result.begin(), best_result.end()); put_uint(best_result.size()); std::cout.put('\n'); for (auto id : best_result) { put_uint(id); std::cout.put(' '); } std::cout.put('\n'); } else { std::cout << "NIE\n"; } return 0; } |