#include <message.h> #include "kollib.h" #include <unordered_set> #include <climits> #include <cstdio> #include <utility> #include <map> typedef std::unordered_set<int> S; typedef std::pair<int, int> P; struct Res { Res() {} Res(int to, int toCnt) : _to1(to), _to1Cnt(toCnt), _to2(-1), _to2Cnt(0) {} void add(int to, int toCnt) { _to2 = to; _to2Cnt = toCnt; } void setCutoffRes(int to, int cnt) { if (INT_MAX == _to1) { _to1 = to; _to1Cnt = cnt; } else { _to2 = to; _to2Cnt = cnt; } } int _to1; int _to1Cnt; int _to2; int _to2Cnt; }; typedef std::map<int, Res> M; void sendOrder(int who, int startNode, int neighbour) { PutInt(who, startNode); PutInt(who, neighbour); Send(who); } int receiveResult(M &m, int &cutoff) { int node = Receive(-1); int val = GetInt(node); if (val != -1) { int from = GetInt(node); // get real message with result int count = GetInt(node); auto found = m.find(from); if (found == m.end()) { m[from] = Res(val, count); } else { found->second.add(val, count); } if (INT_MAX == val) cutoff = from; } return node; } const int MAXSEARCH = 500000000; P getImportant( const S &s, int start, int neighbour ) { int result = 1; while (s.find(neighbour) == s.end() && result <= MAXSEARCH) { int fn = FirstNeighbor(neighbour); if (fn != start) { start = neighbour; neighbour = fn; ++result; } else if ((fn = SecondNeighbor(neighbour)) != start) { start = neighbour; neighbour = fn; ++result; } } return P(result <= MAXSEARCH ? neighbour : INT_MAX, result); } void output(const M &m, int from, int to) { int count = 0; int prev = from; while (from != to) { auto f = m.find(from); if (f->second._to1 != prev) { prev = from; count += f->second._to1Cnt; from = f->second._to1; } else { prev = from; count += f->second._to2Cnt; from = f->second._to2; } } printf("%d\n", std::min(count, NumberOfStudents()-count)); } void countCutoff(M &m, int cutoff) { Res &start = m[cutoff]; int prev = cutoff; int next = 0; int count = 0; if (start._to1 != INT_MAX) { next = start._to1; count = start._to1Cnt; } else { next = start._to2; count = start._to2Cnt; } bool finish = false; do { auto f = m.find(next); if (f->second._to1 == INT_MAX || f->second._to2 == INT_MAX) { f->second.setCutoffRes(cutoff, NumberOfStudents()-count); start.setCutoffRes(next, NumberOfStudents()-count); finish = true; } else { if (f->second._to1 != prev) { prev = next; count += f->second._to1Cnt; next = f->second._to1; } else { prev = next; count += f->second._to2Cnt; next = f->second._to2; } } } while (!finish); } int main() { S s; int qnum = NumberOfQueries(); s.reserve(2*qnum); for (int i = 0; i < qnum; ++i) { s.insert(QueryFrom(i+1)); s.insert(QueryTo(i+1)); } if (MyNodeId() == 0) { M m; int numnodes = NumberOfNodes(); int cutoff = -1; for (auto it = s.cbegin(); it != s.cend(); ++it) { auto node = receiveResult(m, cutoff); sendOrder(node, *it, FirstNeighbor(*it)); node = receiveResult(m, cutoff); sendOrder(node, *it, SecondNeighbor(*it)); } for (int i = 1; i < numnodes; ++i) { auto node = receiveResult(m, cutoff); PutInt(node, -1); Send(node); } if (cutoff != -1) { countCutoff(m, cutoff); } // now, compute and print results for (int i = 0; i < qnum; ++i) { output(m, QueryFrom(i+1), QueryTo(i+1)); } } else { PutInt(0, -1); Send(0); bool finish = false; do { Receive(0); int start = GetInt(0); if (start != -1) { int neighbour = GetInt(0); // now, find the important neighbour P p = getImportant(s, start, neighbour); PutInt(0, p.first); // send the answer PutInt(0, start); PutInt(0, p.second); Send(0); } else finish = true; } while (!finish); } 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 | #include <message.h> #include "kollib.h" #include <unordered_set> #include <climits> #include <cstdio> #include <utility> #include <map> typedef std::unordered_set<int> S; typedef std::pair<int, int> P; struct Res { Res() {} Res(int to, int toCnt) : _to1(to), _to1Cnt(toCnt), _to2(-1), _to2Cnt(0) {} void add(int to, int toCnt) { _to2 = to; _to2Cnt = toCnt; } void setCutoffRes(int to, int cnt) { if (INT_MAX == _to1) { _to1 = to; _to1Cnt = cnt; } else { _to2 = to; _to2Cnt = cnt; } } int _to1; int _to1Cnt; int _to2; int _to2Cnt; }; typedef std::map<int, Res> M; void sendOrder(int who, int startNode, int neighbour) { PutInt(who, startNode); PutInt(who, neighbour); Send(who); } int receiveResult(M &m, int &cutoff) { int node = Receive(-1); int val = GetInt(node); if (val != -1) { int from = GetInt(node); // get real message with result int count = GetInt(node); auto found = m.find(from); if (found == m.end()) { m[from] = Res(val, count); } else { found->second.add(val, count); } if (INT_MAX == val) cutoff = from; } return node; } const int MAXSEARCH = 500000000; P getImportant( const S &s, int start, int neighbour ) { int result = 1; while (s.find(neighbour) == s.end() && result <= MAXSEARCH) { int fn = FirstNeighbor(neighbour); if (fn != start) { start = neighbour; neighbour = fn; ++result; } else if ((fn = SecondNeighbor(neighbour)) != start) { start = neighbour; neighbour = fn; ++result; } } return P(result <= MAXSEARCH ? neighbour : INT_MAX, result); } void output(const M &m, int from, int to) { int count = 0; int prev = from; while (from != to) { auto f = m.find(from); if (f->second._to1 != prev) { prev = from; count += f->second._to1Cnt; from = f->second._to1; } else { prev = from; count += f->second._to2Cnt; from = f->second._to2; } } printf("%d\n", std::min(count, NumberOfStudents()-count)); } void countCutoff(M &m, int cutoff) { Res &start = m[cutoff]; int prev = cutoff; int next = 0; int count = 0; if (start._to1 != INT_MAX) { next = start._to1; count = start._to1Cnt; } else { next = start._to2; count = start._to2Cnt; } bool finish = false; do { auto f = m.find(next); if (f->second._to1 == INT_MAX || f->second._to2 == INT_MAX) { f->second.setCutoffRes(cutoff, NumberOfStudents()-count); start.setCutoffRes(next, NumberOfStudents()-count); finish = true; } else { if (f->second._to1 != prev) { prev = next; count += f->second._to1Cnt; next = f->second._to1; } else { prev = next; count += f->second._to2Cnt; next = f->second._to2; } } } while (!finish); } int main() { S s; int qnum = NumberOfQueries(); s.reserve(2*qnum); for (int i = 0; i < qnum; ++i) { s.insert(QueryFrom(i+1)); s.insert(QueryTo(i+1)); } if (MyNodeId() == 0) { M m; int numnodes = NumberOfNodes(); int cutoff = -1; for (auto it = s.cbegin(); it != s.cend(); ++it) { auto node = receiveResult(m, cutoff); sendOrder(node, *it, FirstNeighbor(*it)); node = receiveResult(m, cutoff); sendOrder(node, *it, SecondNeighbor(*it)); } for (int i = 1; i < numnodes; ++i) { auto node = receiveResult(m, cutoff); PutInt(node, -1); Send(node); } if (cutoff != -1) { countCutoff(m, cutoff); } // now, compute and print results for (int i = 0; i < qnum; ++i) { output(m, QueryFrom(i+1), QueryTo(i+1)); } } else { PutInt(0, -1); Send(0); bool finish = false; do { Receive(0); int start = GetInt(0); if (start != -1) { int neighbour = GetInt(0); // now, find the important neighbour P p = getImportant(s, start, neighbour); PutInt(0, p.first); // send the answer PutInt(0, start); PutInt(0, p.second); Send(0); } else finish = true; } while (!finish); } return 0; } |