#include "sabotaz.h" #include "message.h" #include <cassert> #include <iostream> #include <functional> #include <vector> #include <sstream> //#define DEBUG #ifdef DEBUG static constexpr const bool debug = true; #else static constexpr const bool debug = false; #endif #define LOG(x) \ if(debug) { \ std::stringstream str; \ str << "Node " << MyNodeId() << "/" << NumberOfNodes() << ": "; \ str x; \ std::cerr << str.str(); \ } // ------------------------ messaging ----------------------- enum class message_type_t { FILTERED }; using node_t = int; namespace communication { template<unsigned size> struct communication_traits { }; #define GEN_TRAITS(type, name) \ template<> \ struct communication_traits<sizeof(type)> { \ template<class T> \ static void put(node_t target, T value) { \ static_assert(sizeof(T) == sizeof(type), "Size mismatch between T and " #type);\ Put ## name(target, static_cast<type>(value)); \ } \ \ template<class T> \ static T get(node_t target) { \ static_assert(sizeof(T) == sizeof(type), "Size mismatch between T and " #type);\ return static_cast<T>(Get ## name(target)); \ } \ }; GEN_TRAITS(char, Char) GEN_TRAITS(int, Int) GEN_TRAITS(long long, LL) template<class T> void put(node_t target, T value) { communication_traits<sizeof(value)>::put(target, value); } template<class T> void get(node_t target, T *number) { *number = communication_traits<sizeof(*number)>::template get<T>(target); } void send(node_t target) { Send(target); } void receive(node_t from) { Receive(from); } } // ------------------------------------------------------------------------------------------------ class graph_t { public: using vertexid_t = int32_t; using dfstime_t = int32_t; static constexpr const dfstime_t UNVISITED = -1; static constexpr const vertexid_t NO_PARENT = -1; graph_t(size_t vertex_count) : vertices(vertex_count, vertex_t{}) { } void add_edge(vertexid_t a, vertexid_t b) { vertices[a].adj.push_back(b); vertices[b].adj.push_back(a); } void clear() { for(auto &vertex: vertices) vertex.adj.clear(); } void dfs() { for(auto &vertex: vertices) vertex.order = UNVISITED; dfstime_t time = 1; for(vertexid_t vertexid = 0; vertexid < static_cast<vertexid_t>(vertices.size()); ++vertexid) { auto &vertex = vertices[vertexid]; if(vertex.order == UNVISITED) dfs(vertexid, NO_PARENT, time); } } template<class Fn> void iter_important(Fn &&fn) const { for(vertexid_t vertexid = 0; vertexid < static_cast<vertexid_t>(vertices.size()); ++vertexid) { auto &vertex = vertices[vertexid]; if(vertex.parentid != NO_PARENT) { fn(vertexid, vertex.parentid); } size_t parent_count = 0; for(auto adjid: vertex.adj) { auto &adj = vertices[adjid]; if(adj.order == vertex.low && adjid != vertexid && (adjid != vertex.parentid || parent_count++ == 1)) { fn(adjid, vertexid); break; } } } } template<class Fn> void iter_bridges(Fn &&fn) const { for(vertexid_t vertexid = 0; vertexid < static_cast<vertexid_t>(vertices.size()); ++vertexid) { auto &vertex = vertices[vertexid]; if(vertex.parentid != NO_PARENT && vertex.low == vertex.order) fn(vertexid, vertex.parentid); } } private: struct vertex_t { std::vector<vertexid_t> adj; vertexid_t parentid; dfstime_t order; dfstime_t low; }; std::vector<vertex_t> vertices; void dfs(vertexid_t vertexid, vertexid_t parentid, dfstime_t &time) { auto &vertex = vertices[vertexid]; vertex.order = vertex.low = time++; vertex.parentid = parentid; size_t parent_count = 0; for(auto adjid: vertex.adj) { auto &adj = vertices[adjid]; if(adj.order == UNVISITED) { dfs(adjid, vertexid, time); vertex.low = std::min(vertex.low, adj.low); } else if(adjid != parentid || parent_count++) { vertex.low = std::min(vertex.low, adj.order); } } } }; class solver_t { public: solver_t() : vertex_count(NumberOfIsles()) , graph(vertex_count) { } void map(size_t begin, size_t end) { LOG(<< "map(" << begin << ", " << end << ")"); graph.clear(); for(size_t idx = begin; idx < end; ++idx) graph.add_edge( static_cast<graph_t::vertexid_t>(BridgeEndA(idx)), static_cast<graph_t::vertexid_t>(BridgeEndB(idx)) ); graph.dfs(); } void reduce(const std::vector<node_t> &from) { prune(); for(auto node: from) { using namespace std::placeholders; make_edge_reader(node, std::bind(&graph_t::add_edge, &graph, _1, _2))(); } graph.dfs(); } void send(node_t target) { edge_writer_t edge_writer(target); using namespace std::placeholders; graph.iter_important(std::bind(&edge_writer_t::operator(), &edge_writer, _1, _2)); } void prune() { using namespace std::placeholders; graph_t new_graph(vertex_count); graph.iter_important(std::bind(&graph_t::add_edge, &new_graph, _1, _2)); graph = std::move(new_graph); } size_t number_of_bridges() const { size_t res = 0; graph.iter_bridges([&res](graph_t::vertexid_t, graph_t::vertexid_t) { ++res; }); return res; } private: size_t vertex_count, edge_count; graph_t graph; static constexpr const graph_t::vertexid_t NEXT_MESSAGE = -1; static constexpr const graph_t::vertexid_t END_OF_STREAM = -2; class edge_writer_t { public: edge_writer_t(node_t target) : target(target) , records(0) { } ~edge_writer_t() { communication::put(target, END_OF_STREAM); communication::send(target); } void operator()(graph_t::vertexid_t a, graph_t::vertexid_t b) { LOG(<< "to node " << target << ": " << a << " " << b << std::endl); communication::put(target, a); communication::put(target, b); records++; if(records >= RECORDS_IN_MESSAGE) { communication::put(target, NEXT_MESSAGE); communication::send(target); records = 0; } } private: node_t target; size_t records; static constexpr const size_t RECORDS_IN_MESSAGE = 7900 / (2 * sizeof(graph_t::vertexid_t)); }; template<class Callback> class edge_reader_t { public: edge_reader_t(node_t from, const Callback &callback) : from(from) , callback(callback) { } void operator()() { communication::receive(from); while(true) { graph_t::vertexid_t a, b; communication::get(from, &a); if(a == NEXT_MESSAGE) { communication::receive(from); continue; } else if(a == END_OF_STREAM) { return; } else { communication::get(from, &b); LOG(<< "from node " << from << ": " << a << " " << b << std::endl); callback(a, b); } } } private: node_t from; Callback callback; }; template<class Callback> edge_reader_t<Callback> make_edge_reader(node_t from, const Callback &callback) { return {from, callback}; } }; int main() { constexpr const node_t DIV = 8; const auto my_id = MyNodeId(); const auto edge_count = NumberOfBridges(); const auto node_count = std::min(static_cast<node_t>(edge_count), static_cast<node_t>(NumberOfNodes())); if(my_id >= node_count) return 0; const auto edges_per_worker = (edge_count + node_count - 1) / node_count; solver_t solver; solver.map(my_id * edges_per_worker, std::min<size_t>(edge_count, (my_id + 1) * edges_per_worker)); for(auto div = DIV; div <= DIV * node_count; div *= DIV) { LOG(<< "round " << div << " finished" << std::endl); if(my_id % div != 0) { solver.send(my_id - my_id % div); return 0; } std::vector<node_t> from; from.reserve(DIV); for(int idx = 1; idx < DIV; idx++) { node_t node = my_id + idx * div / DIV; if(node >= node_count) break; from.push_back(node); } solver.reduce(from); } std::cout << solver.number_of_bridges() << std::endl; }
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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | #include "sabotaz.h" #include "message.h" #include <cassert> #include <iostream> #include <functional> #include <vector> #include <sstream> //#define DEBUG #ifdef DEBUG static constexpr const bool debug = true; #else static constexpr const bool debug = false; #endif #define LOG(x) \ if(debug) { \ std::stringstream str; \ str << "Node " << MyNodeId() << "/" << NumberOfNodes() << ": "; \ str x; \ std::cerr << str.str(); \ } // ------------------------ messaging ----------------------- enum class message_type_t { FILTERED }; using node_t = int; namespace communication { template<unsigned size> struct communication_traits { }; #define GEN_TRAITS(type, name) \ template<> \ struct communication_traits<sizeof(type)> { \ template<class T> \ static void put(node_t target, T value) { \ static_assert(sizeof(T) == sizeof(type), "Size mismatch between T and " #type);\ Put ## name(target, static_cast<type>(value)); \ } \ \ template<class T> \ static T get(node_t target) { \ static_assert(sizeof(T) == sizeof(type), "Size mismatch between T and " #type);\ return static_cast<T>(Get ## name(target)); \ } \ }; GEN_TRAITS(char, Char) GEN_TRAITS(int, Int) GEN_TRAITS(long long, LL) template<class T> void put(node_t target, T value) { communication_traits<sizeof(value)>::put(target, value); } template<class T> void get(node_t target, T *number) { *number = communication_traits<sizeof(*number)>::template get<T>(target); } void send(node_t target) { Send(target); } void receive(node_t from) { Receive(from); } } // ------------------------------------------------------------------------------------------------ class graph_t { public: using vertexid_t = int32_t; using dfstime_t = int32_t; static constexpr const dfstime_t UNVISITED = -1; static constexpr const vertexid_t NO_PARENT = -1; graph_t(size_t vertex_count) : vertices(vertex_count, vertex_t{}) { } void add_edge(vertexid_t a, vertexid_t b) { vertices[a].adj.push_back(b); vertices[b].adj.push_back(a); } void clear() { for(auto &vertex: vertices) vertex.adj.clear(); } void dfs() { for(auto &vertex: vertices) vertex.order = UNVISITED; dfstime_t time = 1; for(vertexid_t vertexid = 0; vertexid < static_cast<vertexid_t>(vertices.size()); ++vertexid) { auto &vertex = vertices[vertexid]; if(vertex.order == UNVISITED) dfs(vertexid, NO_PARENT, time); } } template<class Fn> void iter_important(Fn &&fn) const { for(vertexid_t vertexid = 0; vertexid < static_cast<vertexid_t>(vertices.size()); ++vertexid) { auto &vertex = vertices[vertexid]; if(vertex.parentid != NO_PARENT) { fn(vertexid, vertex.parentid); } size_t parent_count = 0; for(auto adjid: vertex.adj) { auto &adj = vertices[adjid]; if(adj.order == vertex.low && adjid != vertexid && (adjid != vertex.parentid || parent_count++ == 1)) { fn(adjid, vertexid); break; } } } } template<class Fn> void iter_bridges(Fn &&fn) const { for(vertexid_t vertexid = 0; vertexid < static_cast<vertexid_t>(vertices.size()); ++vertexid) { auto &vertex = vertices[vertexid]; if(vertex.parentid != NO_PARENT && vertex.low == vertex.order) fn(vertexid, vertex.parentid); } } private: struct vertex_t { std::vector<vertexid_t> adj; vertexid_t parentid; dfstime_t order; dfstime_t low; }; std::vector<vertex_t> vertices; void dfs(vertexid_t vertexid, vertexid_t parentid, dfstime_t &time) { auto &vertex = vertices[vertexid]; vertex.order = vertex.low = time++; vertex.parentid = parentid; size_t parent_count = 0; for(auto adjid: vertex.adj) { auto &adj = vertices[adjid]; if(adj.order == UNVISITED) { dfs(adjid, vertexid, time); vertex.low = std::min(vertex.low, adj.low); } else if(adjid != parentid || parent_count++) { vertex.low = std::min(vertex.low, adj.order); } } } }; class solver_t { public: solver_t() : vertex_count(NumberOfIsles()) , graph(vertex_count) { } void map(size_t begin, size_t end) { LOG(<< "map(" << begin << ", " << end << ")"); graph.clear(); for(size_t idx = begin; idx < end; ++idx) graph.add_edge( static_cast<graph_t::vertexid_t>(BridgeEndA(idx)), static_cast<graph_t::vertexid_t>(BridgeEndB(idx)) ); graph.dfs(); } void reduce(const std::vector<node_t> &from) { prune(); for(auto node: from) { using namespace std::placeholders; make_edge_reader(node, std::bind(&graph_t::add_edge, &graph, _1, _2))(); } graph.dfs(); } void send(node_t target) { edge_writer_t edge_writer(target); using namespace std::placeholders; graph.iter_important(std::bind(&edge_writer_t::operator(), &edge_writer, _1, _2)); } void prune() { using namespace std::placeholders; graph_t new_graph(vertex_count); graph.iter_important(std::bind(&graph_t::add_edge, &new_graph, _1, _2)); graph = std::move(new_graph); } size_t number_of_bridges() const { size_t res = 0; graph.iter_bridges([&res](graph_t::vertexid_t, graph_t::vertexid_t) { ++res; }); return res; } private: size_t vertex_count, edge_count; graph_t graph; static constexpr const graph_t::vertexid_t NEXT_MESSAGE = -1; static constexpr const graph_t::vertexid_t END_OF_STREAM = -2; class edge_writer_t { public: edge_writer_t(node_t target) : target(target) , records(0) { } ~edge_writer_t() { communication::put(target, END_OF_STREAM); communication::send(target); } void operator()(graph_t::vertexid_t a, graph_t::vertexid_t b) { LOG(<< "to node " << target << ": " << a << " " << b << std::endl); communication::put(target, a); communication::put(target, b); records++; if(records >= RECORDS_IN_MESSAGE) { communication::put(target, NEXT_MESSAGE); communication::send(target); records = 0; } } private: node_t target; size_t records; static constexpr const size_t RECORDS_IN_MESSAGE = 7900 / (2 * sizeof(graph_t::vertexid_t)); }; template<class Callback> class edge_reader_t { public: edge_reader_t(node_t from, const Callback &callback) : from(from) , callback(callback) { } void operator()() { communication::receive(from); while(true) { graph_t::vertexid_t a, b; communication::get(from, &a); if(a == NEXT_MESSAGE) { communication::receive(from); continue; } else if(a == END_OF_STREAM) { return; } else { communication::get(from, &b); LOG(<< "from node " << from << ": " << a << " " << b << std::endl); callback(a, b); } } } private: node_t from; Callback callback; }; template<class Callback> edge_reader_t<Callback> make_edge_reader(node_t from, const Callback &callback) { return {from, callback}; } }; int main() { constexpr const node_t DIV = 8; const auto my_id = MyNodeId(); const auto edge_count = NumberOfBridges(); const auto node_count = std::min(static_cast<node_t>(edge_count), static_cast<node_t>(NumberOfNodes())); if(my_id >= node_count) return 0; const auto edges_per_worker = (edge_count + node_count - 1) / node_count; solver_t solver; solver.map(my_id * edges_per_worker, std::min<size_t>(edge_count, (my_id + 1) * edges_per_worker)); for(auto div = DIV; div <= DIV * node_count; div *= DIV) { LOG(<< "round " << div << " finished" << std::endl); if(my_id % div != 0) { solver.send(my_id - my_id % div); return 0; } std::vector<node_t> from; from.reserve(DIV); for(int idx = 1; idx < DIV; idx++) { node_t node = my_id + idx * div / DIV; if(node >= node_count) break; from.push_back(node); } solver.reduce(from); } std::cout << solver.number_of_bridges() << std::endl; } |