// // Created by Kamil Borzym on 29/09/15. // Copyright © 2015 kam800. All rights reserved. // #include "stdio.h" #include "stdlib.h" struct t_city; typedef struct t_road { struct t_city* c1Ref; struct t_city* c2Ref; long group; int closed; } t_road; typedef struct t_city { long uid; long gatesNo; struct t_road **roads; long roadsSize; long group; } t_city; #define INIT_CLOCK #define MEASURE_CLOCK(STR) static inline void removeRoadFromCity(t_road *r, t_city *c) { int i=0; t_road** roads = c->roads; long roadsSize = c->roadsSize; while (i<roadsSize) { if (roads[i] == r) { --roadsSize; roads[i] = roads[roadsSize]; c->roadsSize = roadsSize; c->gatesNo--; r->closed = 1; return; } ++i; } } int main() { long i; long n, m, d; scanf("%ld %ld %ld", &n, &m, &d); INIT_CLOCK t_city *cities = malloc(n * sizeof(t_city)); for (i=0; i<n; ++i) { t_city city = {0}; city.uid = i; city.group = -1; cities[i] = city; } MEASURE_CLOCK("init cities") t_road *roads = malloc(m * sizeof(t_road)); for (i=0; i<m; ++i) { long c1, c2; scanf("%ld %ld", &c1, &c2); --c1; --c2; if (c1 > c2) { long t = c1; c1 = c2; c2 = t; } t_city *c1Ref = &cities[c1]; t_city *c2Ref = &cities[c2]; c1Ref->gatesNo++; c2Ref->gatesNo++; t_road road = {0}; road.c1Ref = c1Ref; road.c2Ref = c2Ref; road.group = -1; roads[i] = road; } MEASURE_CLOCK("init roads") for (i=0; i<m; ++i) { t_road *road = &roads[i]; t_city *c1Ref = road->c1Ref; if (c1Ref->roads == 0) { c1Ref->roads = malloc(c1Ref->gatesNo * sizeof(t_road *)); c1Ref->roadsSize = 0; } c1Ref->roads[c1Ref->roadsSize] = road; c1Ref->roadsSize++; t_city *c2Ref = road->c2Ref; if (c2Ref->roads == 0) { c2Ref->roads = malloc(c2Ref->gatesNo * sizeof(t_road *)); c2Ref->roadsSize = 0; } c2Ref->roads[c2Ref->roadsSize] = road; c2Ref->roadsSize++; } MEASURE_CLOCK("connecting roads to cities") t_city **cleanCities = malloc(n * sizeof(t_city*)); long cleanCitiesSize = n; for (i=0; i<n; ++i) { cleanCities[i] = &cities[i]; } int cleaning = 1; while (cleaning) { int cleaned = 0; i=0; while (i<cleanCitiesSize) { // :( t_city *c = cleanCities[i]; if (c->gatesNo < d) { long cRoadsSize = c->roadsSize; t_road **cRoads = c->roads; while (cRoadsSize > 0) { --cRoadsSize; t_road *roadToClose = cRoads[cRoadsSize]; t_city *otherCity = (roadToClose->c1Ref == c) ? roadToClose->c2Ref : roadToClose->c1Ref; removeRoadFromCity(roadToClose, otherCity); } c->gatesNo = 0; c->roadsSize = 0; --cleanCitiesSize; cleanCities[i] = cleanCities[cleanCitiesSize]; cleaned = 1; } else { ++i; } } if (!cleaned) { cleaning = 0; } } MEASURE_CLOCK("cleaning cities") t_road **cleanRoads = malloc(m * sizeof(t_road*)); long cleanRoadsSize = 0; for (i=0; i<m; ++i) { if (roads[i].closed == 0) { cleanRoads[cleanRoadsSize] = &roads[i]; ++cleanRoadsSize; } } MEASURE_CLOCK("cleaning roads") int validCitiesNo = 0; for (i=0; i<n; ++i) { if (cities[i].gatesNo >= d) { ++validCitiesNo; } } t_city **validCities = malloc(validCitiesNo * sizeof(t_city *)); long validCitiesSize = 0; for (i=0; i<n; ++i) { t_city *city = &cities[i]; if (city->gatesNo >= d) { validCities[validCitiesSize] = city; ++validCitiesSize; } } MEASURE_CLOCK("collecting valid cities") long currentGroup = 0; t_road **roadsToMark = malloc(cleanRoadsSize * sizeof(t_road *)); long roadsToMarkSize = 0; if (cleanRoadsSize > 0) { roadsToMark[0] = cleanRoads[0]; roadsToMark[0]->group = currentGroup; roadsToMarkSize = 1; } else { printf("NIE"); return 0; } while (roadsToMarkSize > 0) { --roadsToMarkSize; t_road *roadToMark = roadsToMark[roadsToMarkSize]; roadToMark->group = currentGroup; roadToMark->c1Ref->group = currentGroup; roadToMark->c2Ref->group = currentGroup; t_road **roads1 = roadToMark->c1Ref->roads; long roads1Length = roadToMark->c1Ref->roadsSize; for (i=0; i<roads1Length; ++i) { t_road *road1 = roads1[i]; if (road1->group == -1) { road1->group = currentGroup; roadsToMark[roadsToMarkSize] = road1; ++roadsToMarkSize; } } t_road **roads2 = roadToMark->c2Ref->roads; long roads2Length = roadToMark->c2Ref->roadsSize; for (i=0; i<roads2Length; ++i) { t_road *road2 = roads2[i]; if (road2->group == -1) { road2->group = currentGroup; roadsToMark[roadsToMarkSize] = road2; ++roadsToMarkSize; } } if (roadsToMarkSize == 0) { ++currentGroup; for (i=0; i<cleanRoadsSize; ++i) { t_road *road = cleanRoads[i]; if (road->group == -1) { road->group = currentGroup; roadsToMark[roadsToMarkSize] = road; ++roadsToMarkSize; break; } } } } MEASURE_CLOCK("grouping") long *groupsHistogram = calloc(currentGroup, sizeof(long)); long maxValue = 0; long maxGroup = -1; for (i = 0; i<n; ++i) { long cityGroup = cities[i].group; if (cityGroup != -1) { ++groupsHistogram[cityGroup]; if (groupsHistogram[cityGroup] > maxValue) { maxValue = groupsHistogram[cityGroup]; maxGroup = cityGroup; } } } MEASURE_CLOCK("group histogram") printf("%ld\n", maxValue); for (i=0; i<n; ++i) { long cityGroup = cities[i].group; if (cityGroup == maxGroup) { printf("%ld ", (i+1)); } } printf("\n"); MEASURE_CLOCK("printing result") 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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | // // Created by Kamil Borzym on 29/09/15. // Copyright © 2015 kam800. All rights reserved. // #include "stdio.h" #include "stdlib.h" struct t_city; typedef struct t_road { struct t_city* c1Ref; struct t_city* c2Ref; long group; int closed; } t_road; typedef struct t_city { long uid; long gatesNo; struct t_road **roads; long roadsSize; long group; } t_city; #define INIT_CLOCK #define MEASURE_CLOCK(STR) static inline void removeRoadFromCity(t_road *r, t_city *c) { int i=0; t_road** roads = c->roads; long roadsSize = c->roadsSize; while (i<roadsSize) { if (roads[i] == r) { --roadsSize; roads[i] = roads[roadsSize]; c->roadsSize = roadsSize; c->gatesNo--; r->closed = 1; return; } ++i; } } int main() { long i; long n, m, d; scanf("%ld %ld %ld", &n, &m, &d); INIT_CLOCK t_city *cities = malloc(n * sizeof(t_city)); for (i=0; i<n; ++i) { t_city city = {0}; city.uid = i; city.group = -1; cities[i] = city; } MEASURE_CLOCK("init cities") t_road *roads = malloc(m * sizeof(t_road)); for (i=0; i<m; ++i) { long c1, c2; scanf("%ld %ld", &c1, &c2); --c1; --c2; if (c1 > c2) { long t = c1; c1 = c2; c2 = t; } t_city *c1Ref = &cities[c1]; t_city *c2Ref = &cities[c2]; c1Ref->gatesNo++; c2Ref->gatesNo++; t_road road = {0}; road.c1Ref = c1Ref; road.c2Ref = c2Ref; road.group = -1; roads[i] = road; } MEASURE_CLOCK("init roads") for (i=0; i<m; ++i) { t_road *road = &roads[i]; t_city *c1Ref = road->c1Ref; if (c1Ref->roads == 0) { c1Ref->roads = malloc(c1Ref->gatesNo * sizeof(t_road *)); c1Ref->roadsSize = 0; } c1Ref->roads[c1Ref->roadsSize] = road; c1Ref->roadsSize++; t_city *c2Ref = road->c2Ref; if (c2Ref->roads == 0) { c2Ref->roads = malloc(c2Ref->gatesNo * sizeof(t_road *)); c2Ref->roadsSize = 0; } c2Ref->roads[c2Ref->roadsSize] = road; c2Ref->roadsSize++; } MEASURE_CLOCK("connecting roads to cities") t_city **cleanCities = malloc(n * sizeof(t_city*)); long cleanCitiesSize = n; for (i=0; i<n; ++i) { cleanCities[i] = &cities[i]; } int cleaning = 1; while (cleaning) { int cleaned = 0; i=0; while (i<cleanCitiesSize) { // :( t_city *c = cleanCities[i]; if (c->gatesNo < d) { long cRoadsSize = c->roadsSize; t_road **cRoads = c->roads; while (cRoadsSize > 0) { --cRoadsSize; t_road *roadToClose = cRoads[cRoadsSize]; t_city *otherCity = (roadToClose->c1Ref == c) ? roadToClose->c2Ref : roadToClose->c1Ref; removeRoadFromCity(roadToClose, otherCity); } c->gatesNo = 0; c->roadsSize = 0; --cleanCitiesSize; cleanCities[i] = cleanCities[cleanCitiesSize]; cleaned = 1; } else { ++i; } } if (!cleaned) { cleaning = 0; } } MEASURE_CLOCK("cleaning cities") t_road **cleanRoads = malloc(m * sizeof(t_road*)); long cleanRoadsSize = 0; for (i=0; i<m; ++i) { if (roads[i].closed == 0) { cleanRoads[cleanRoadsSize] = &roads[i]; ++cleanRoadsSize; } } MEASURE_CLOCK("cleaning roads") int validCitiesNo = 0; for (i=0; i<n; ++i) { if (cities[i].gatesNo >= d) { ++validCitiesNo; } } t_city **validCities = malloc(validCitiesNo * sizeof(t_city *)); long validCitiesSize = 0; for (i=0; i<n; ++i) { t_city *city = &cities[i]; if (city->gatesNo >= d) { validCities[validCitiesSize] = city; ++validCitiesSize; } } MEASURE_CLOCK("collecting valid cities") long currentGroup = 0; t_road **roadsToMark = malloc(cleanRoadsSize * sizeof(t_road *)); long roadsToMarkSize = 0; if (cleanRoadsSize > 0) { roadsToMark[0] = cleanRoads[0]; roadsToMark[0]->group = currentGroup; roadsToMarkSize = 1; } else { printf("NIE"); return 0; } while (roadsToMarkSize > 0) { --roadsToMarkSize; t_road *roadToMark = roadsToMark[roadsToMarkSize]; roadToMark->group = currentGroup; roadToMark->c1Ref->group = currentGroup; roadToMark->c2Ref->group = currentGroup; t_road **roads1 = roadToMark->c1Ref->roads; long roads1Length = roadToMark->c1Ref->roadsSize; for (i=0; i<roads1Length; ++i) { t_road *road1 = roads1[i]; if (road1->group == -1) { road1->group = currentGroup; roadsToMark[roadsToMarkSize] = road1; ++roadsToMarkSize; } } t_road **roads2 = roadToMark->c2Ref->roads; long roads2Length = roadToMark->c2Ref->roadsSize; for (i=0; i<roads2Length; ++i) { t_road *road2 = roads2[i]; if (road2->group == -1) { road2->group = currentGroup; roadsToMark[roadsToMarkSize] = road2; ++roadsToMarkSize; } } if (roadsToMarkSize == 0) { ++currentGroup; for (i=0; i<cleanRoadsSize; ++i) { t_road *road = cleanRoads[i]; if (road->group == -1) { road->group = currentGroup; roadsToMark[roadsToMarkSize] = road; ++roadsToMarkSize; break; } } } } MEASURE_CLOCK("grouping") long *groupsHistogram = calloc(currentGroup, sizeof(long)); long maxValue = 0; long maxGroup = -1; for (i = 0; i<n; ++i) { long cityGroup = cities[i].group; if (cityGroup != -1) { ++groupsHistogram[cityGroup]; if (groupsHistogram[cityGroup] > maxValue) { maxValue = groupsHistogram[cityGroup]; maxGroup = cityGroup; } } } MEASURE_CLOCK("group histogram") printf("%ld\n", maxValue); for (i=0; i<n; ++i) { long cityGroup = cities[i].group; if (cityGroup == maxGroup) { printf("%ld ", (i+1)); } } printf("\n"); MEASURE_CLOCK("printing result") return 0; } |