#include <algorithm> #include <cstdio> #include <cstdlib> #include <map> #include <set> #include <vector> #include "kollib.h" #include "message.h" using namespace std; const int kMaxStartPoints = 500; class Edge { public: Edge(const int from_, const int to_, const int offset_) : from(from_), to(to_), offset(offset_) {} int from; int to; int offset; }; class Query { public: Query(const int cursor_, const int student_, const int offset_) : cursor(cursor_), student(student_), offset(offset_) {} int cursor; int student; int offset; }; void ChooseCursors(const int l, const int r, int points, vector<pair<int, int> > *cursors) { if (points == 0) return; if (l == r) { cursors->push_back(make_pair(l, FirstNeighbor(l))); cursors->push_back(make_pair(l, SecondNeighbor(l))); return; } const int m = (l + r) / 2; int all = r - l + 1; int left = m - l + 1; int points_left = 0; int points_right = 0; while (points--) if (rand() % all-- < left) { ++points_left; --left; } else { ++points_right; } ChooseCursors(l, m, points_left, cursors); ChooseCursors(m + 1, r, points_right, cursors); } int n; void BuildCursors(vector<pair<int, int> > *cursors) { ChooseCursors(1, n, min(n, kMaxStartPoints), cursors); sort(cursors->begin(), cursors->end()); } int Negate(const int i) { return (n - i) % n; } int Add(const int i, const int j) { return (i + j) % n; } void DFS(const int a, const int offset, const vector<vector<pair<int, int> > > &all_edges, const vector<vector<pair<int, int> > > &all_queries, vector<bool> *visited, map<int, int> *query_offsets) { if ((*visited)[a]) return; (*visited)[a] = true; for (int i = 0; i < all_edges[a].size(); ++i) DFS(all_edges[a][i].first, Add(offset, all_edges[a][i].second), all_edges, all_queries, visited, query_offsets); for (int i = 0; i < all_queries[a].size(); ++i) (*query_offsets)[all_queries[a][i].first] = Add(offset, all_queries[a][i].second); } int GetDistance(const int i, const int j) { return min(Add(i, Negate(j)), Add(j, Negate(i))); } int main() { const int node = MyNodeId(); const int nodes = NumberOfNodes(); n = NumberOfStudents(); set<int> query_points; for (int i = 1; i <= NumberOfQueries(); ++i) { query_points.insert(QueryFrom(i)); query_points.insert(QueryTo(i)); } vector<pair<int, int> > cursors; BuildCursors(&cursors); // Data gathered by a worker. vector<Edge> edges; vector<Query> queries; // Go over my cursors. for (int i = node; i < cursors.size(); i += nodes) { pair<int, int> cursor = cursors[i]; int offset = 0; while (true) { // Advance cursor. const int first = FirstNeighbor(cursor.second); const int second = SecondNeighbor(cursor.second); if (first != cursor.first) { cursor = make_pair(cursor.second, first); } else { cursor = make_pair(cursor.second, second); } ++offset; // Note if we found any queries. if (query_points.find(cursor.first) != query_points.end()) queries.push_back(Query(i, cursor.first, offset)); // Note if we found any cursors. const vector<pair<int, int> >::const_iterator it = lower_bound(cursors.begin(), cursors.end(), cursor); if (it != cursors.end() && *it == cursor) { edges.push_back(Edge(i, it - cursors.begin(), offset)); break; } } } // Send a report to zero. // // Relations between cursors. PutInt(0, edges.size()); for (int i = 0; i < edges.size(); ++i) { PutInt(0, edges[i].from); PutInt(0, edges[i].to); PutInt(0, edges[i].offset); } // Queries encountered. PutInt(0, queries.size()); for (int i = 0; i < queries.size(); ++i) { PutInt(0, queries[i].cursor); PutInt(0, queries[i].student); PutInt(0, queries[i].offset); } Send(0); if (node == 0) { // Total information from all workers. vector<vector<pair<int, int> > > all_edges(cursors.size()); vector<vector<pair<int, int> > > all_queries(cursors.size()); // Process worker messages one by one. for (int i = 0; i < nodes; ++i) { Receive(i); // Relations between cursors. const int edges_size = GetInt(i); for (int j = 0; j < edges_size; ++j) { const int from = GetInt(i); const int to = GetInt(i); const int offset = GetInt(i); all_edges[from].push_back(make_pair(to, offset)); all_edges[to].push_back(make_pair(from, Negate(offset))); } // Queries encountered. const int queries_size = GetInt(i); for (int j = 0; j < queries_size; ++j) { const int cursor = GetInt(i); const int student = GetInt(i); const int offset = GetInt(i); all_queries[cursor].push_back(make_pair(student, offset)); } } // Compute query offsets. map<int, int> query_offsets; vector<bool> visited(cursors.size(), false); DFS(0, 0, all_edges, all_queries, &visited, &query_offsets); // Print out the answers. for (int i = 1; i <= NumberOfQueries(); ++i) printf("%d\n", GetDistance(query_offsets[QueryFrom(i)], query_offsets[QueryTo(i)])); } 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 | #include <algorithm> #include <cstdio> #include <cstdlib> #include <map> #include <set> #include <vector> #include "kollib.h" #include "message.h" using namespace std; const int kMaxStartPoints = 500; class Edge { public: Edge(const int from_, const int to_, const int offset_) : from(from_), to(to_), offset(offset_) {} int from; int to; int offset; }; class Query { public: Query(const int cursor_, const int student_, const int offset_) : cursor(cursor_), student(student_), offset(offset_) {} int cursor; int student; int offset; }; void ChooseCursors(const int l, const int r, int points, vector<pair<int, int> > *cursors) { if (points == 0) return; if (l == r) { cursors->push_back(make_pair(l, FirstNeighbor(l))); cursors->push_back(make_pair(l, SecondNeighbor(l))); return; } const int m = (l + r) / 2; int all = r - l + 1; int left = m - l + 1; int points_left = 0; int points_right = 0; while (points--) if (rand() % all-- < left) { ++points_left; --left; } else { ++points_right; } ChooseCursors(l, m, points_left, cursors); ChooseCursors(m + 1, r, points_right, cursors); } int n; void BuildCursors(vector<pair<int, int> > *cursors) { ChooseCursors(1, n, min(n, kMaxStartPoints), cursors); sort(cursors->begin(), cursors->end()); } int Negate(const int i) { return (n - i) % n; } int Add(const int i, const int j) { return (i + j) % n; } void DFS(const int a, const int offset, const vector<vector<pair<int, int> > > &all_edges, const vector<vector<pair<int, int> > > &all_queries, vector<bool> *visited, map<int, int> *query_offsets) { if ((*visited)[a]) return; (*visited)[a] = true; for (int i = 0; i < all_edges[a].size(); ++i) DFS(all_edges[a][i].first, Add(offset, all_edges[a][i].second), all_edges, all_queries, visited, query_offsets); for (int i = 0; i < all_queries[a].size(); ++i) (*query_offsets)[all_queries[a][i].first] = Add(offset, all_queries[a][i].second); } int GetDistance(const int i, const int j) { return min(Add(i, Negate(j)), Add(j, Negate(i))); } int main() { const int node = MyNodeId(); const int nodes = NumberOfNodes(); n = NumberOfStudents(); set<int> query_points; for (int i = 1; i <= NumberOfQueries(); ++i) { query_points.insert(QueryFrom(i)); query_points.insert(QueryTo(i)); } vector<pair<int, int> > cursors; BuildCursors(&cursors); // Data gathered by a worker. vector<Edge> edges; vector<Query> queries; // Go over my cursors. for (int i = node; i < cursors.size(); i += nodes) { pair<int, int> cursor = cursors[i]; int offset = 0; while (true) { // Advance cursor. const int first = FirstNeighbor(cursor.second); const int second = SecondNeighbor(cursor.second); if (first != cursor.first) { cursor = make_pair(cursor.second, first); } else { cursor = make_pair(cursor.second, second); } ++offset; // Note if we found any queries. if (query_points.find(cursor.first) != query_points.end()) queries.push_back(Query(i, cursor.first, offset)); // Note if we found any cursors. const vector<pair<int, int> >::const_iterator it = lower_bound(cursors.begin(), cursors.end(), cursor); if (it != cursors.end() && *it == cursor) { edges.push_back(Edge(i, it - cursors.begin(), offset)); break; } } } // Send a report to zero. // // Relations between cursors. PutInt(0, edges.size()); for (int i = 0; i < edges.size(); ++i) { PutInt(0, edges[i].from); PutInt(0, edges[i].to); PutInt(0, edges[i].offset); } // Queries encountered. PutInt(0, queries.size()); for (int i = 0; i < queries.size(); ++i) { PutInt(0, queries[i].cursor); PutInt(0, queries[i].student); PutInt(0, queries[i].offset); } Send(0); if (node == 0) { // Total information from all workers. vector<vector<pair<int, int> > > all_edges(cursors.size()); vector<vector<pair<int, int> > > all_queries(cursors.size()); // Process worker messages one by one. for (int i = 0; i < nodes; ++i) { Receive(i); // Relations between cursors. const int edges_size = GetInt(i); for (int j = 0; j < edges_size; ++j) { const int from = GetInt(i); const int to = GetInt(i); const int offset = GetInt(i); all_edges[from].push_back(make_pair(to, offset)); all_edges[to].push_back(make_pair(from, Negate(offset))); } // Queries encountered. const int queries_size = GetInt(i); for (int j = 0; j < queries_size; ++j) { const int cursor = GetInt(i); const int student = GetInt(i); const int offset = GetInt(i); all_queries[cursor].push_back(make_pair(student, offset)); } } // Compute query offsets. map<int, int> query_offsets; vector<bool> visited(cursors.size(), false); DFS(0, 0, all_edges, all_queries, &visited, &query_offsets); // Print out the answers. for (int i = 1; i <= NumberOfQueries(); ++i) printf("%d\n", GetDistance(query_offsets[QueryFrom(i)], query_offsets[QueryTo(i)])); } return 0; } |