#define debug_printf(fmt, ...) //#define debug_printf(fmt, ...) do { printf(fmt, __VA_ARGS__); } while (0) #include <cstdio> #include <cstdlib> #include <cstring> #include <climits> #include <cassert> #include <vector> #include <set> #include <map> #include <deque> #include <algorithm> #include <limits> #include <iostream> const long long LLMAX = (std::numeric_limits<long long>()).max(); const long long LLMIN = (std::numeric_limits<long long>()).min(); const int n_max = 200000; const int m_max = 200000; int n, m, d; struct Road { int a; int b; static bool less(const Road* p, const Road* q) { return (p->a != q->a) ? p->a < q->a : p->b < q->b; } }; Road roads_buf[m_max * 2]; Road* roads_tmp[m_max * 2]; struct City { bool is_rejected; Road** roads; // might contain rejected cities int roads_len; // might contain rejected cities and thus be not equal to roads_count int roads_count; bool is_visited; }; City cities[n_max + 1]; // 1-based int city_num(City& city) { return &city - cities; } void consider_rejection(City& city) { if (city.roads_count < d && !city.is_rejected) { city.is_rejected = true; city.is_visited = true; for (int i=0; i < city.roads_len; ++i) { Road& road = *city.roads[i]; City& other_city = cities[road.b]; --other_city.roads_count; consider_rejection(other_city); } } } int current_set[n_max]; int current_set_len; void visit(City& city) { city.is_visited = true; current_set[current_set_len++] = city_num(city); for (int i=0; i < city.roads_len; ++i) { Road& road = *city.roads[i]; City& other_city = cities[road.b]; if (!other_city.is_visited) { visit(other_city); } } } int best_set[n_max]; int best_set_len; int main() { std::ios_base::sync_with_stdio(false); scanf("%d %d %d", &n, &m, &d); int ri = 0; for (int i=0; i < m; ++i) { int ai, bi; scanf("%d %d", &ai, &bi); ++cities[ai].roads_count; ++cities[bi].roads_count; Road& road1 = roads_buf[ri]; road1.a = ai; road1.b = bi; roads_tmp[ri] = &road1; ++ri; Road& road2 = roads_buf[ri]; road2.a = bi; road2.b = ai; roads_tmp[ri] = &road2; ++ri; } std::sort(roads_tmp, roads_tmp + ri, Road::less); int curr_city = -1; for (int i=0; i < ri; ++i) { Road& road = *roads_tmp[i]; if (curr_city != road.a) { if (curr_city != -1) { assert(cities[curr_city].roads_len == cities[curr_city].roads_count); } curr_city = road.a; cities[curr_city].roads = &roads_tmp[i]; } ++cities[curr_city].roads_len; } for (int i=1; i <= n; ++i) { City& city = cities[i]; consider_rejection(city); } for (int i=1; i <= n; ++i) { City& city = cities[i]; if (!city.is_visited) { current_set_len = 0; visit(city); if (best_set_len < current_set_len) { best_set_len = current_set_len; std::memcpy(best_set, current_set, current_set_len * sizeof current_set[0]); } } } std::sort(best_set, best_set + best_set_len); if (best_set_len != 0) { printf("%d\n", best_set_len); for (int i=0; i < best_set_len; ++i) { printf("%d ", best_set[i]); } } else { printf("NIE\n"); } }
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 | #define debug_printf(fmt, ...) //#define debug_printf(fmt, ...) do { printf(fmt, __VA_ARGS__); } while (0) #include <cstdio> #include <cstdlib> #include <cstring> #include <climits> #include <cassert> #include <vector> #include <set> #include <map> #include <deque> #include <algorithm> #include <limits> #include <iostream> const long long LLMAX = (std::numeric_limits<long long>()).max(); const long long LLMIN = (std::numeric_limits<long long>()).min(); const int n_max = 200000; const int m_max = 200000; int n, m, d; struct Road { int a; int b; static bool less(const Road* p, const Road* q) { return (p->a != q->a) ? p->a < q->a : p->b < q->b; } }; Road roads_buf[m_max * 2]; Road* roads_tmp[m_max * 2]; struct City { bool is_rejected; Road** roads; // might contain rejected cities int roads_len; // might contain rejected cities and thus be not equal to roads_count int roads_count; bool is_visited; }; City cities[n_max + 1]; // 1-based int city_num(City& city) { return &city - cities; } void consider_rejection(City& city) { if (city.roads_count < d && !city.is_rejected) { city.is_rejected = true; city.is_visited = true; for (int i=0; i < city.roads_len; ++i) { Road& road = *city.roads[i]; City& other_city = cities[road.b]; --other_city.roads_count; consider_rejection(other_city); } } } int current_set[n_max]; int current_set_len; void visit(City& city) { city.is_visited = true; current_set[current_set_len++] = city_num(city); for (int i=0; i < city.roads_len; ++i) { Road& road = *city.roads[i]; City& other_city = cities[road.b]; if (!other_city.is_visited) { visit(other_city); } } } int best_set[n_max]; int best_set_len; int main() { std::ios_base::sync_with_stdio(false); scanf("%d %d %d", &n, &m, &d); int ri = 0; for (int i=0; i < m; ++i) { int ai, bi; scanf("%d %d", &ai, &bi); ++cities[ai].roads_count; ++cities[bi].roads_count; Road& road1 = roads_buf[ri]; road1.a = ai; road1.b = bi; roads_tmp[ri] = &road1; ++ri; Road& road2 = roads_buf[ri]; road2.a = bi; road2.b = ai; roads_tmp[ri] = &road2; ++ri; } std::sort(roads_tmp, roads_tmp + ri, Road::less); int curr_city = -1; for (int i=0; i < ri; ++i) { Road& road = *roads_tmp[i]; if (curr_city != road.a) { if (curr_city != -1) { assert(cities[curr_city].roads_len == cities[curr_city].roads_count); } curr_city = road.a; cities[curr_city].roads = &roads_tmp[i]; } ++cities[curr_city].roads_len; } for (int i=1; i <= n; ++i) { City& city = cities[i]; consider_rejection(city); } for (int i=1; i <= n; ++i) { City& city = cities[i]; if (!city.is_visited) { current_set_len = 0; visit(city); if (best_set_len < current_set_len) { best_set_len = current_set_len; std::memcpy(best_set, current_set, current_set_len * sizeof current_set[0]); } } } std::sort(best_set, best_set + best_set_len); if (best_set_len != 0) { printf("%d\n", best_set_len); for (int i=0; i < best_set_len; ++i) { printf("%d ", best_set[i]); } } else { printf("NIE\n"); } } |