#include <algorithm> #include <cassert> #include <iostream> #include <random> #include <utility> using namespace std; #include "message.h" #include "kollib.h" namespace { minstd_rand rng{0x230b0933U}; constexpr int factor = 10; struct Result { vector<pair<int, int>> dist; }; void send(int target, Result const& res) { PutInt(target, res.dist.size()); for (auto const& p: res.dist) { PutInt(target, p.first); PutInt(target, p.second); } Send(target); } pair<int, Result> receive(int source=-1) { source = Receive(source); Result res; res.dist.resize(GetInt(source)); for (auto& p: res.dist) { p.first = GetInt(source); p.second = GetInt(source); } return {source, move(res)}; } int get(int v, int e) { assert(0 <= e && e < 2); if (e == 0) return FirstNeighbor(v+1) - 1; else return SecondNeighbor(v+1) - 1; } Result calculate(vector<int> const& pointsp, vector<int> const& points, int l, int r) { Result res; for (int i = l; i < r; ++i) { for (int e = 0; e < 2; ++e) { int p = -1; int v = pointsp[i]; int d = 0; do { int w = get(v, e); if (w == p) w = get(v, 1-e); p = v; v = w; ++d; } while (!binary_search(points.begin(), points.end(), v)); res.dist.emplace_back(d, v); } } return res; } vector<int> combine(int n, vector<pair<int, int>> const& queries, vector<int> const& pointsp, vector<int> const& points, vector<Result> const& results) { auto find = [&](int v) { return lower_bound(points.begin(), points.end(), v) - points.begin(); }; auto it = results.begin(); auto it2 = it->dist.begin(); int m = points.size(); vector<pair<int, int>> neigh[2]; neigh[0].resize(m); neigh[1].resize(m); for (int i = 0; i < m; ++i) { int v = pointsp[i]; int q = find(v); for (int e = 0; e < 2; ++e) { while (it2 == it->dist.end()) { ++it; it2 = it->dist.begin(); } neigh[e][q] = *it2; ++it2; } } vector<int> num(points.size()); int p = -1; int v = points[0]; int d = 0; do { int q = find(v); num[q] = d; auto w = neigh[0][q]; if (w.second == p) w = neigh[1][q]; d += w.first; p = v; v = w.second; } while (v != points[0]); vector<int> answers; answers.reserve(queries.size()); for (auto const& q: queries) { int a = find(q.first); int b = find(q.second); int r = num[a] - num[b]; if (r < 0) r += n; answers.push_back(min(r, n-r)); } return answers; } } int main() { int const n = NumberOfStudents(); int const k = NumberOfNodes(); int const id = MyNodeId(); int const m = NumberOfQueries(); if (m == 0) return 0; vector<pair<int, int>> queries; queries.reserve(m); vector<int> points; for (int i = 0; i < m; ++i) { int a = QueryFrom(i+1) - 1; int b = QueryTo(i+1) - 1; queries.emplace_back(a, b); points.push_back(a); points.push_back(b); } uniform_int_distribution<int> dist(0, n-1); for (int i = 0; i < factor*k; ++i) { int x = dist(rng); points.push_back(x); } sort(points.begin(), points.end()); points.erase(unique(points.begin(), points.end()), points.end()); vector<int> pointsp = points; shuffle(pointsp.begin(), pointsp.end(), rng); int l = (id + 0LL) * points.size() / k; int r = (id + 1LL) * points.size() / k; Result result = calculate(pointsp, points, l, r); if (id > 0) { send(0, move(result)); } else { vector<Result> results; results.reserve(k); results.push_back(move(result)); for (int i = 1; i < k; ++i) { results.push_back(move(receive(i).second)); } auto res = combine(n, move(queries), move(pointsp), move(points), move(results)); for (auto ans: res) { cout << ans << '\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 | #include <algorithm> #include <cassert> #include <iostream> #include <random> #include <utility> using namespace std; #include "message.h" #include "kollib.h" namespace { minstd_rand rng{0x230b0933U}; constexpr int factor = 10; struct Result { vector<pair<int, int>> dist; }; void send(int target, Result const& res) { PutInt(target, res.dist.size()); for (auto const& p: res.dist) { PutInt(target, p.first); PutInt(target, p.second); } Send(target); } pair<int, Result> receive(int source=-1) { source = Receive(source); Result res; res.dist.resize(GetInt(source)); for (auto& p: res.dist) { p.first = GetInt(source); p.second = GetInt(source); } return {source, move(res)}; } int get(int v, int e) { assert(0 <= e && e < 2); if (e == 0) return FirstNeighbor(v+1) - 1; else return SecondNeighbor(v+1) - 1; } Result calculate(vector<int> const& pointsp, vector<int> const& points, int l, int r) { Result res; for (int i = l; i < r; ++i) { for (int e = 0; e < 2; ++e) { int p = -1; int v = pointsp[i]; int d = 0; do { int w = get(v, e); if (w == p) w = get(v, 1-e); p = v; v = w; ++d; } while (!binary_search(points.begin(), points.end(), v)); res.dist.emplace_back(d, v); } } return res; } vector<int> combine(int n, vector<pair<int, int>> const& queries, vector<int> const& pointsp, vector<int> const& points, vector<Result> const& results) { auto find = [&](int v) { return lower_bound(points.begin(), points.end(), v) - points.begin(); }; auto it = results.begin(); auto it2 = it->dist.begin(); int m = points.size(); vector<pair<int, int>> neigh[2]; neigh[0].resize(m); neigh[1].resize(m); for (int i = 0; i < m; ++i) { int v = pointsp[i]; int q = find(v); for (int e = 0; e < 2; ++e) { while (it2 == it->dist.end()) { ++it; it2 = it->dist.begin(); } neigh[e][q] = *it2; ++it2; } } vector<int> num(points.size()); int p = -1; int v = points[0]; int d = 0; do { int q = find(v); num[q] = d; auto w = neigh[0][q]; if (w.second == p) w = neigh[1][q]; d += w.first; p = v; v = w.second; } while (v != points[0]); vector<int> answers; answers.reserve(queries.size()); for (auto const& q: queries) { int a = find(q.first); int b = find(q.second); int r = num[a] - num[b]; if (r < 0) r += n; answers.push_back(min(r, n-r)); } return answers; } } int main() { int const n = NumberOfStudents(); int const k = NumberOfNodes(); int const id = MyNodeId(); int const m = NumberOfQueries(); if (m == 0) return 0; vector<pair<int, int>> queries; queries.reserve(m); vector<int> points; for (int i = 0; i < m; ++i) { int a = QueryFrom(i+1) - 1; int b = QueryTo(i+1) - 1; queries.emplace_back(a, b); points.push_back(a); points.push_back(b); } uniform_int_distribution<int> dist(0, n-1); for (int i = 0; i < factor*k; ++i) { int x = dist(rng); points.push_back(x); } sort(points.begin(), points.end()); points.erase(unique(points.begin(), points.end()), points.end()); vector<int> pointsp = points; shuffle(pointsp.begin(), pointsp.end(), rng); int l = (id + 0LL) * points.size() / k; int r = (id + 1LL) * points.size() / k; Result result = calculate(pointsp, points, l, r); if (id > 0) { send(0, move(result)); } else { vector<Result> results; results.reserve(k); results.push_back(move(result)); for (int i = 1; i < k; ++i) { results.push_back(move(receive(i).second)); } auto res = combine(n, move(queries), move(pointsp), move(points), move(results)); for (auto ans: res) { cout << ans << '\n'; } } return 0; } |