#include <algorithm> #include <cstdlib> #include <iostream> #include <map> #include <set> #include <queue> #include "kollib.h" #include "message.h" using namespace std; typedef pair<int, int> pii; const int mmax = 200+5; int slaves, nid; int n, m; set<int> qPoints; int queryFrom[mmax], queryTo[mmax]; deque<int> order; map<pii, int> nbdistance; map<int, pii> neighbors; vector< pair<pii, int> > detected; map<int, int> nbdistanceCache; ostream& operator<<(ostream& out, const pii& p) { out << '(' << p.first << ',' << p.second << ')'; return out; } inline int neighbor(int sid, int away) { return (FirstNeighbor(sid) != away) ? FirstNeighbor(sid) : SecondNeighbor(sid); } pii nextNeighbor(int nb, int away) { int d = 1; while (qPoints.find(nb) == qPoints.end()) { int e = neighbor(nb, away); away = nb; nb = e; d++; if (d > (n/2 + 2)) { nb = 0; break; } } return make_pair(nb, d); } void calcNeighbors(int sid) { pii nb1 = nextNeighbor(FirstNeighbor(sid), sid); detected.push_back(make_pair(make_pair(sid, nb1.first), nb1.second)); pii nb2 = nextNeighbor(SecondNeighbor(sid), sid); detected.push_back(make_pair(make_pair(sid, nb2.first), nb2.second)); } void sendDetected() { PutInt(0, detected.size()); for (auto dd : detected) { PutInt(0, dd.first.first); PutInt(0, dd.first.second); PutInt(0, dd.second); } Send(0); } struct { void buildOrder() { int first = *qPoints.begin(); int last = neighbors[first].first, prev = first; order.push_back(first); while (last && last != first) { // cerr << "+" << last << "\n"; order.push_back(last); int next = (neighbors[last].first != prev) ? neighbors[last].first : neighbors[last].second; prev = last; last = next; } last = neighbors[first].second, prev = first; while (last && last != first) { // cerr << "+" << last << "\n"; order.push_front(last); int next = (neighbors[last].first != prev) ? neighbors[last].first : neighbors[last].second; prev = last; last = next; } } void buildDistanceCache() { auto it = order.begin(); int prev = *it; int dist = 0; nbdistanceCache[*it] = 0; it++; while (it != order.end()) { dist += nbdistance[make_pair(prev, *it)]; nbdistanceCache[*it] = dist; prev = *it; it++; } } void answerQueries() { // cerr << "answerQueries\n"; buildDistanceCache(); for (int i = 1; i <= m; i++) { // cerr << "query " << i << ":" << queryFrom[i] << "-" << queryTo[i] << "\n"; int dist = abs(nbdistanceCache[queryFrom[i]] - nbdistanceCache[queryTo[i]]); cout << min(n-dist, dist) << "\n"; } } } node0; int main() { slaves = NumberOfNodes(); nid = MyNodeId(); n = NumberOfStudents(); m = NumberOfQueries(); for (int i = 1; i <= m; i++) { queryFrom[i] = QueryFrom(i); queryTo[i] = QueryTo(i); qPoints.insert(queryFrom[i]); qPoints.insert(queryTo[i]); } int i = 0; for (set<int>::iterator it = qPoints.begin(); it != qPoints.end(); it++, i++) { if (i % slaves == nid) { calcNeighbors(*it); } } sendDetected(); detected.clear(); if (nid == 0) { for (int i = 0; i < slaves; i++) { Receive(i); int k = GetInt(i); for (int j = 0; j < k; j++) { int sid = GetInt(i), nb = GetInt(i), d = GetInt(i); // cerr << sid << " " << nb << "=" << d << "\n"; if (nb) { nbdistance[make_pair(sid, nb)] = d; nbdistance[make_pair(nb, sid)] = d; if (neighbors.find(sid) != neighbors.end()) { neighbors[sid].second = nb; } else { neighbors[sid] = make_pair(nb, 0); } } } } // cerr << "neighbors:\n"; // for (auto x : neighbors) { // cerr << x.first << "->" << x.second << '\n'; // } // cerr << '\n'; node0.buildOrder(); // for (auto x : order) { // cerr << x << " "; // } // cerr << '\n'; node0.answerQueries(); } 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 | #include <algorithm> #include <cstdlib> #include <iostream> #include <map> #include <set> #include <queue> #include "kollib.h" #include "message.h" using namespace std; typedef pair<int, int> pii; const int mmax = 200+5; int slaves, nid; int n, m; set<int> qPoints; int queryFrom[mmax], queryTo[mmax]; deque<int> order; map<pii, int> nbdistance; map<int, pii> neighbors; vector< pair<pii, int> > detected; map<int, int> nbdistanceCache; ostream& operator<<(ostream& out, const pii& p) { out << '(' << p.first << ',' << p.second << ')'; return out; } inline int neighbor(int sid, int away) { return (FirstNeighbor(sid) != away) ? FirstNeighbor(sid) : SecondNeighbor(sid); } pii nextNeighbor(int nb, int away) { int d = 1; while (qPoints.find(nb) == qPoints.end()) { int e = neighbor(nb, away); away = nb; nb = e; d++; if (d > (n/2 + 2)) { nb = 0; break; } } return make_pair(nb, d); } void calcNeighbors(int sid) { pii nb1 = nextNeighbor(FirstNeighbor(sid), sid); detected.push_back(make_pair(make_pair(sid, nb1.first), nb1.second)); pii nb2 = nextNeighbor(SecondNeighbor(sid), sid); detected.push_back(make_pair(make_pair(sid, nb2.first), nb2.second)); } void sendDetected() { PutInt(0, detected.size()); for (auto dd : detected) { PutInt(0, dd.first.first); PutInt(0, dd.first.second); PutInt(0, dd.second); } Send(0); } struct { void buildOrder() { int first = *qPoints.begin(); int last = neighbors[first].first, prev = first; order.push_back(first); while (last && last != first) { // cerr << "+" << last << "\n"; order.push_back(last); int next = (neighbors[last].first != prev) ? neighbors[last].first : neighbors[last].second; prev = last; last = next; } last = neighbors[first].second, prev = first; while (last && last != first) { // cerr << "+" << last << "\n"; order.push_front(last); int next = (neighbors[last].first != prev) ? neighbors[last].first : neighbors[last].second; prev = last; last = next; } } void buildDistanceCache() { auto it = order.begin(); int prev = *it; int dist = 0; nbdistanceCache[*it] = 0; it++; while (it != order.end()) { dist += nbdistance[make_pair(prev, *it)]; nbdistanceCache[*it] = dist; prev = *it; it++; } } void answerQueries() { // cerr << "answerQueries\n"; buildDistanceCache(); for (int i = 1; i <= m; i++) { // cerr << "query " << i << ":" << queryFrom[i] << "-" << queryTo[i] << "\n"; int dist = abs(nbdistanceCache[queryFrom[i]] - nbdistanceCache[queryTo[i]]); cout << min(n-dist, dist) << "\n"; } } } node0; int main() { slaves = NumberOfNodes(); nid = MyNodeId(); n = NumberOfStudents(); m = NumberOfQueries(); for (int i = 1; i <= m; i++) { queryFrom[i] = QueryFrom(i); queryTo[i] = QueryTo(i); qPoints.insert(queryFrom[i]); qPoints.insert(queryTo[i]); } int i = 0; for (set<int>::iterator it = qPoints.begin(); it != qPoints.end(); it++, i++) { if (i % slaves == nid) { calcNeighbors(*it); } } sendDetected(); detected.clear(); if (nid == 0) { for (int i = 0; i < slaves; i++) { Receive(i); int k = GetInt(i); for (int j = 0; j < k; j++) { int sid = GetInt(i), nb = GetInt(i), d = GetInt(i); // cerr << sid << " " << nb << "=" << d << "\n"; if (nb) { nbdistance[make_pair(sid, nb)] = d; nbdistance[make_pair(nb, sid)] = d; if (neighbors.find(sid) != neighbors.end()) { neighbors[sid].second = nb; } else { neighbors[sid] = make_pair(nb, 0); } } } } // cerr << "neighbors:\n"; // for (auto x : neighbors) { // cerr << x.first << "->" << x.second << '\n'; // } // cerr << '\n'; node0.buildOrder(); // for (auto x : order) { // cerr << x << " "; // } // cerr << '\n'; node0.answerQueries(); } return 0; } |