#include <cstdio> #include <cstring> #include <cmath> #include <cassert> #include <iostream> #include <algorithm> #include <iterator> #include <string> #include <vector> #include <queue> #include <bitset> #include <utility> #include <stack> using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef vector<int> VI; #define MP make_pair #define FOR(v,p,k) for(int v=(p);v<=(k);++v) #define FORD(v,p,k) for(int v=(p);v>=(k);--v) #define REP(i,n) for(int i=0;i<(n);++i) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define PB push_back #define ST first #define ND second #define SIZE(x) (int)x.size() #define ALL(c) c.begin(),c.end() #define ODD(x) ((x)%2) #define EVEN(x) (!(ODD(x))) #include "kollib.h" #include "message.h" int p = 100000; #include <set> #include <map> VI points; set<int> pointsSet; struct query { int from, to; }; query queries[209]; struct student { int nr; PII distantNeighbors[2]; int dst; student(int nr_, PII n1_, PII n2_) { nr = nr_; distantNeighbors[0] = n1_; distantNeighbors[1] = n2_; dst = 0; } student(int nr_, int node) { nr = nr_; int d,n; d = GetInt(node); n = GetInt(node); distantNeighbors[0] = MP(d,n); d = GetInt(node); n = GetInt(node); distantNeighbors[1] = MP(d,n); dst = 0; } }; int main() { int nodesNumber = NumberOfNodes(); int nodeID = MyNodeId(); int n = NumberOfStudents(); p = min(p,n); points.reserve(p); int queriesNumber = NumberOfQueries(); if (queriesNumber == 0) return 0; FOR(i,1,queriesNumber) { int from = QueryFrom(i); int to = QueryTo(i); queries[i-1].from = from; queries[i-1].to = to; if (pointsSet.count(from) == 0) { pointsSet.insert(from); points.PB(from); } if (pointsSet.count(to) == 0) { pointsSet.insert(to); points.PB(to); } } if (n <= 2*p) { int i = 1; while (points.size() < p) { if (pointsSet.count(i) == 0) { pointsSet.insert(i); points.PB(i); } ++i; } } else { unsigned a = 1664525; unsigned c = 1013904223; unsigned x = 1400271458; while (points.size() < p) { int cand = (x%n)+1; x = x*a + c; if (pointsSet.count(cand)) continue; pointsSet.insert(cand); points.PB(cand); } } vector<student> students; students.reserve(nodeID? p/nodesNumber + 1 : p); for (int i = nodeID; i < p; i += nodesNumber) { int start = points[i]; int next = FirstNeighbor(start); int prev = start; int dist = 1; while (pointsSet.count(next) == 0) { int candNext = FirstNeighbor(next); if (candNext == prev) { candNext = SecondNeighbor(next); } prev = next; next = candNext; ++dist; } int dist1 = dist; int next1 = next; next = SecondNeighbor(start); prev = start; dist = 1; while (pointsSet.count(next) == 0) { int candNext = FirstNeighbor(next); if (candNext == prev) { candNext = SecondNeighbor(next); } prev = next; next = candNext; ++dist; } students.PB(student(start, MP(dist1, next1), MP(dist, next))); } if (nodeID > 0) { if (students.size() == 0) return 0; for (int i=0; i<students.size(); i++) { const student& s = students[i]; PutInt(0, s.distantNeighbors[0].first); PutInt(0, s.distantNeighbors[0].second); PutInt(0, s.distantNeighbors[1].first); PutInt(0, s.distantNeighbors[1].second); } Send(0); return 0; } else { while(students.size() < p) { int connectedNode = Receive(-1); for (int i = connectedNode; i < p; i += nodesNumber) { int start = points[i]; students.PB(student(start, connectedNode)); } } } //TODO optymalniej map<int, int> studentIndex; REP(i,p) { studentIndex[students[i].nr] = i; } int next = 0; int prev = next; int dist = 0; REP(i,p-1) { int candNext = studentIndex[students[next].distantNeighbors[0].second]; if (candNext == prev) { candNext = studentIndex[students[next].distantNeighbors[1].second]; dist += students[next].distantNeighbors[1].first; } else { dist += students[next].distantNeighbors[0].first; } students[candNext].dst = dist; prev = next; next = candNext; } REP(i,queriesNumber) { int distFrom = students[studentIndex[queries[i].from]].dst; int distTo = students[studentIndex[queries[i].to]].dst; int ret = abs(distTo - distFrom); printf("%d\n", min(ret, n-ret)); } 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 | #include <cstdio> #include <cstring> #include <cmath> #include <cassert> #include <iostream> #include <algorithm> #include <iterator> #include <string> #include <vector> #include <queue> #include <bitset> #include <utility> #include <stack> using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef vector<int> VI; #define MP make_pair #define FOR(v,p,k) for(int v=(p);v<=(k);++v) #define FORD(v,p,k) for(int v=(p);v>=(k);--v) #define REP(i,n) for(int i=0;i<(n);++i) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define PB push_back #define ST first #define ND second #define SIZE(x) (int)x.size() #define ALL(c) c.begin(),c.end() #define ODD(x) ((x)%2) #define EVEN(x) (!(ODD(x))) #include "kollib.h" #include "message.h" int p = 100000; #include <set> #include <map> VI points; set<int> pointsSet; struct query { int from, to; }; query queries[209]; struct student { int nr; PII distantNeighbors[2]; int dst; student(int nr_, PII n1_, PII n2_) { nr = nr_; distantNeighbors[0] = n1_; distantNeighbors[1] = n2_; dst = 0; } student(int nr_, int node) { nr = nr_; int d,n; d = GetInt(node); n = GetInt(node); distantNeighbors[0] = MP(d,n); d = GetInt(node); n = GetInt(node); distantNeighbors[1] = MP(d,n); dst = 0; } }; int main() { int nodesNumber = NumberOfNodes(); int nodeID = MyNodeId(); int n = NumberOfStudents(); p = min(p,n); points.reserve(p); int queriesNumber = NumberOfQueries(); if (queriesNumber == 0) return 0; FOR(i,1,queriesNumber) { int from = QueryFrom(i); int to = QueryTo(i); queries[i-1].from = from; queries[i-1].to = to; if (pointsSet.count(from) == 0) { pointsSet.insert(from); points.PB(from); } if (pointsSet.count(to) == 0) { pointsSet.insert(to); points.PB(to); } } if (n <= 2*p) { int i = 1; while (points.size() < p) { if (pointsSet.count(i) == 0) { pointsSet.insert(i); points.PB(i); } ++i; } } else { unsigned a = 1664525; unsigned c = 1013904223; unsigned x = 1400271458; while (points.size() < p) { int cand = (x%n)+1; x = x*a + c; if (pointsSet.count(cand)) continue; pointsSet.insert(cand); points.PB(cand); } } vector<student> students; students.reserve(nodeID? p/nodesNumber + 1 : p); for (int i = nodeID; i < p; i += nodesNumber) { int start = points[i]; int next = FirstNeighbor(start); int prev = start; int dist = 1; while (pointsSet.count(next) == 0) { int candNext = FirstNeighbor(next); if (candNext == prev) { candNext = SecondNeighbor(next); } prev = next; next = candNext; ++dist; } int dist1 = dist; int next1 = next; next = SecondNeighbor(start); prev = start; dist = 1; while (pointsSet.count(next) == 0) { int candNext = FirstNeighbor(next); if (candNext == prev) { candNext = SecondNeighbor(next); } prev = next; next = candNext; ++dist; } students.PB(student(start, MP(dist1, next1), MP(dist, next))); } if (nodeID > 0) { if (students.size() == 0) return 0; for (int i=0; i<students.size(); i++) { const student& s = students[i]; PutInt(0, s.distantNeighbors[0].first); PutInt(0, s.distantNeighbors[0].second); PutInt(0, s.distantNeighbors[1].first); PutInt(0, s.distantNeighbors[1].second); } Send(0); return 0; } else { while(students.size() < p) { int connectedNode = Receive(-1); for (int i = connectedNode; i < p; i += nodesNumber) { int start = points[i]; students.PB(student(start, connectedNode)); } } } //TODO optymalniej map<int, int> studentIndex; REP(i,p) { studentIndex[students[i].nr] = i; } int next = 0; int prev = next; int dist = 0; REP(i,p-1) { int candNext = studentIndex[students[next].distantNeighbors[0].second]; if (candNext == prev) { candNext = studentIndex[students[next].distantNeighbors[1].second]; dist += students[next].distantNeighbors[1].first; } else { dist += students[next].distantNeighbors[0].first; } students[candNext].dst = dist; prev = next; next = candNext; } REP(i,queriesNumber) { int distFrom = students[studentIndex[queries[i].from]].dst; int distTo = students[studentIndex[queries[i].to]].dst; int ret = abs(distTo - distFrom); printf("%d\n", min(ret, n-ret)); } return 0; } |