#include <cstdio> #include <cstdint> #include <cstdlib> #include <algorithm> #ifdef PA_LOCAL #include "../Common/common.h" #else #define init(...) #define finalize() #endif using namespace std; struct Road { int32_t from; int32_t to; bool orgs_conn; }; bool operator<(const Road& lhs, const Road& rhs) { return lhs.from < rhs.from; } struct City { int32_t orgs_num; int32_t fst_road; int32_t conns_num; int32_t group_id; }; int main(int argc, const char* argv[]) { init("example_succ"); //init("example_fail"); int32_t cities_num, roads_num, min_conns; scanf("%d%d%d", &cities_num, &roads_num, &min_conns); City* cities = new City[cities_num]; for (int32_t ci = 0; ci < cities_num; ++ci) { cities[ci].conns_num = 0; cities[ci].orgs_num = 0; cities[ci].group_id = -1; } Road* roads = new Road[roads_num * 2]; for (int32_t ri = 0; ri < roads_num; ++ri) { scanf("%d%d", &roads[ri * 2].from, &roads[ri * 2].to); roads[ri * 2].from--; roads[ri * 2].to--; roads[ri * 2].orgs_conn = false; roads[ri * 2 + 1].from = roads[ri * 2].to; roads[ri * 2 + 1].to = roads[ri * 2].from; roads[ri * 2 + 1].orgs_conn = false; } sort(roads, roads + roads_num * 2); for (int32_t ri = 0; ri < roads_num * 2; ++ri) { City& city = cities[roads[ri].from]; if (city.conns_num == 0) { city.fst_road = ri; } city.conns_num++; } for (int32_t ri = 0; ri < roads_num * 2; ++ri) { if (cities[roads[ri].from].conns_num >= min_conns && cities[roads[ri].to].conns_num >= min_conns) { cities[roads[ri].from].orgs_num++; roads[ri].orgs_conn = true; } } for (bool removed = true; removed;) { removed = false; for (int32_t ri = 0; ri < roads_num * 2; ++ri) { if (roads[ri].orgs_conn && cities[roads[ri].to].orgs_num < min_conns) { cities[roads[ri].from].orgs_num--; roads[ri].orgs_conn = false; removed = true; } } } int32_t groups_num = 0; for (int32_t ci = 0; ci < cities_num; ++ci) { City& city = cities[ci]; if (city.orgs_num >= min_conns) { if (city.group_id < 0) { city.group_id = groups_num; ++groups_num; } for (int32_t ri = city.fst_road, re = ri + city.conns_num; ri < re; ++ri) { if (roads[ri].orgs_conn) { cities[roads[ri].to].group_id = city.group_id; } } } } if (groups_num > 0) { int32_t* groups = new int32_t[groups_num]; for (int32_t gi = 0; gi < groups_num; ++gi) { groups[gi] = 0; } for (int32_t ci = 0; ci < cities_num; ++ci) { if (cities[ci].group_id >= 0) { groups[cities[ci].group_id]++; } } int32_t largest_group = 0; for (int32_t gi = 1; gi < groups_num; ++gi) { if (groups[gi] > groups[largest_group]) { largest_group = gi; } } printf("%d\n", groups[largest_group]); bool print_space = false; for (int32_t ci = 0; ci < cities_num; ++ci) { if (cities[ci].group_id == largest_group) { if (print_space) { printf(" "); } printf("%d", ci + 1); print_space = true; } } printf("\n"); } else { printf("NIE\n"); } finalize(); 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 | #include <cstdio> #include <cstdint> #include <cstdlib> #include <algorithm> #ifdef PA_LOCAL #include "../Common/common.h" #else #define init(...) #define finalize() #endif using namespace std; struct Road { int32_t from; int32_t to; bool orgs_conn; }; bool operator<(const Road& lhs, const Road& rhs) { return lhs.from < rhs.from; } struct City { int32_t orgs_num; int32_t fst_road; int32_t conns_num; int32_t group_id; }; int main(int argc, const char* argv[]) { init("example_succ"); //init("example_fail"); int32_t cities_num, roads_num, min_conns; scanf("%d%d%d", &cities_num, &roads_num, &min_conns); City* cities = new City[cities_num]; for (int32_t ci = 0; ci < cities_num; ++ci) { cities[ci].conns_num = 0; cities[ci].orgs_num = 0; cities[ci].group_id = -1; } Road* roads = new Road[roads_num * 2]; for (int32_t ri = 0; ri < roads_num; ++ri) { scanf("%d%d", &roads[ri * 2].from, &roads[ri * 2].to); roads[ri * 2].from--; roads[ri * 2].to--; roads[ri * 2].orgs_conn = false; roads[ri * 2 + 1].from = roads[ri * 2].to; roads[ri * 2 + 1].to = roads[ri * 2].from; roads[ri * 2 + 1].orgs_conn = false; } sort(roads, roads + roads_num * 2); for (int32_t ri = 0; ri < roads_num * 2; ++ri) { City& city = cities[roads[ri].from]; if (city.conns_num == 0) { city.fst_road = ri; } city.conns_num++; } for (int32_t ri = 0; ri < roads_num * 2; ++ri) { if (cities[roads[ri].from].conns_num >= min_conns && cities[roads[ri].to].conns_num >= min_conns) { cities[roads[ri].from].orgs_num++; roads[ri].orgs_conn = true; } } for (bool removed = true; removed;) { removed = false; for (int32_t ri = 0; ri < roads_num * 2; ++ri) { if (roads[ri].orgs_conn && cities[roads[ri].to].orgs_num < min_conns) { cities[roads[ri].from].orgs_num--; roads[ri].orgs_conn = false; removed = true; } } } int32_t groups_num = 0; for (int32_t ci = 0; ci < cities_num; ++ci) { City& city = cities[ci]; if (city.orgs_num >= min_conns) { if (city.group_id < 0) { city.group_id = groups_num; ++groups_num; } for (int32_t ri = city.fst_road, re = ri + city.conns_num; ri < re; ++ri) { if (roads[ri].orgs_conn) { cities[roads[ri].to].group_id = city.group_id; } } } } if (groups_num > 0) { int32_t* groups = new int32_t[groups_num]; for (int32_t gi = 0; gi < groups_num; ++gi) { groups[gi] = 0; } for (int32_t ci = 0; ci < cities_num; ++ci) { if (cities[ci].group_id >= 0) { groups[cities[ci].group_id]++; } } int32_t largest_group = 0; for (int32_t gi = 1; gi < groups_num; ++gi) { if (groups[gi] > groups[largest_group]) { largest_group = gi; } } printf("%d\n", groups[largest_group]); bool print_space = false; for (int32_t ci = 0; ci < cities_num; ++ci) { if (cities[ci].group_id == largest_group) { if (print_space) { printf(" "); } printf("%d", ci + 1); print_space = true; } } printf("\n"); } else { printf("NIE\n"); } finalize(); return 0; } |