#include "kollib.h" #include "message.h" #include <cstdio> #include <cmath> #include <ctime> #include <cstdlib> #include <algorithm> #include <deque> #include <unordered_set> #include <unordered_map> #define LOG(args...) //#define LOG(args...) fprintf(stderr, args) #define FOR(i, n) for(int i = 0, __n = (n); i<__n; i++) using namespace std; typedef pair<int,int> para; //#define TICK(s) fprintf(stderr, "%s: %.2f\n", s, clock()/(CLOCKS_PER_SEC*1.0)) #define TICK(args...) void sendVector(const vector<int> & V, int target) { PutInt(target, V.size()); FOR(i, V.size()) PutInt(target, V[i]); Send(target); } void receiveVector(vector<int>& V, int from) { int f = Receive(from); int n = GetInt(from); FOR(i, n) V.push_back(GetInt(from)); } void broadcast(vector<int> & V) { int nodes = NumberOfNodes(); int id = MyNodeId(); // printf("broadcast me\n"); if(id != 0) { receiveVector(V, id>>1); } if(id<<1 < nodes) sendVector(V, id<<1); if(1+(id<<1) < nodes) sendVector(V, 1+(id<<1)); } // // int getDotsCount1() { // int n = NumberOfStudents(); // int q = NumberOfQueries(); // int cnt = NumberOfNodes() * 40; // if(cnt > n/10) cnt = n/10; // if(cnt < 2*q) cnt = 2*q; // if(cnt > n) cnt = n; // // // printf("dot count %d\n", cnt); // return cnt; // // } void initializeDots(vector<int> & V) { int n = NumberOfStudents(); int q = NumberOfQueries(); // int cnt = getDotsCount1(); unordered_set<int> S; int id = MyNodeId(); if(id == 0) { FOR(i, (20 + NumberOfNodes()) * 30) { S.insert(rand()%n + 1); } FOR(i, q) { int a = QueryFrom(i+1); int b = QueryTo(i+1); S.insert(a); S.insert(b); } // while(S.size() < cnt) { // S.insert(rand()%n + 1); // } for(auto it = S.begin(); it != S.end(); it++) { V.push_back(*it); } broadcast(V); } else { broadcast(V); } } para subsearch(unordered_set<int>& M, int x, int prev) { int x1 = x; int d = 0; while(1) { int a = FirstNeighbor(x); if(a == prev) { a = SecondNeighbor(x);; } prev = x; x = a; d++; if(M.find(x) != M.end()) return para(x, d); } } struct edge { int a, b, dist; edge(int a, int b, int d):a(a), b(b), dist(d) {}; void Put(int to) { PutInt(to, a); PutInt(to, b); PutInt(to, dist); } static edge Get(int from) { int a = GetInt(from), b = GetInt(from), c=GetInt(from); return edge(a,b,c); } }; pair<edge,edge> search(unordered_set<int> & M, int x) { // // if(x < 1 || x > NumberOfStudents()) { // printf("DUPA %d\n", x); // } int a = FirstNeighbor(x); int b = SecondNeighbor(x); para a1 = subsearch(M, x, a); // printf("found %d->%d (%d)\n", x, a1.first, a1.second); para a2 = subsearch(M, x, b); // printf("found %d->%d (%d)\n", x, a2.first, a2.second); return make_pair(edge(x, a1.first, a1.second), edge(x, a2.first, a2.second)); } void algo(const vector<int> & V, vector<edge>& E) { unordered_set<int> M; int d = V.size(); FOR(i, d) { M.insert(V[i]);; } int id = MyNodeId(); int nodes = NumberOfNodes(); for(int i = id; i<d; i+=nodes) { int x = V[i]; // printf("%d> %d\n", i, x); pair<edge,edge> e = search(M, x); E.push_back(e.first); E.push_back(e.second); } } void gatherEdges(vector<edge> & E) { int id = MyNodeId(); int nodes = NumberOfNodes(); if(id != 0) { PutInt(0, E.size()); FOR(i, E.size()){ E[i].Put(0); } Send(0); E.clear(); } else { for(int node = 1; node < nodes; node++) { Receive(node); int n = GetInt(node); FOR(i, n) { E.push_back(edge::Get(node)); } } } } struct node { int id; int q; vector<para> E; node(int id):id(id),q(0) {} node():id(-1){} void removeEdge(int id2) { // printf("rem edge %d->%d\n", id, id2); FOR(i, E.size()) { if(E[i].first == id2) { swap(E[i], E[E.size()-1]); E.pop_back(); return; } } // printf("not removed\n"); } void addEdge(int id2, int d) { // printf("add edge %d->%d\n", id, id2); E.push_back(para(id2,d)); } }; int searchQuery(unordered_map<int,node> & N, int a, int b) { int id = a; int prev = -1; int dist = 0; while(id != b) { node & n = N[id]; int next = n.E[0].first, d = n.E[0].second; if(next == prev) { next = n.E[1].first; d = n.E[1].second; } dist += d; prev = id; id = next; } return dist; } int main() { // printf("welcome \n"); srand(time(0)); vector<int> V; TICK("start"); initializeDots(V); TICK("init"); // printf("initialized\n"); vector<edge> E; algo(V, E); TICK("algo"); // printf("algoed\n"); gatherEdges(E); TICK("gather"); // printf("gathered %d\n", E.size()); if(MyNodeId() == 0) { unordered_map<int,node> N; FOR(i,V.size()) { N[V[i]] = node(V[i]); } int q = NumberOfQueries(); // for(auto it = N.begin(); it != N.end(); it++){ // node & nd = it->second; // printf("node %d)\n", nd.id); // } // FOR(i, q) { int a = QueryFrom(i+1); int b = QueryTo(i+1); N[a].q = 1; N[b].q = 1; } FOR(i, E.size()) { int a = E[i].a, b= E[i].b; int d = E[i].dist; // printf("edge %d->%d (%d)\n", a,b,d); N[a].addEdge(b,d); //N[b].addEdge(a,d); } // printf("d \n"); // for(auto it = N.begin(); it != N.end(); it++){ // node & nd = it->second; // printf("node %d[%d]) ->%d(%d) ->%d(%d) <%d>\n", nd.id, nd.q, nd.E[0].first, nd.E[0].second, nd.E[1].first, nd.E[1].second, nd.E.size()); // } FOR(i, V.size()) { int id = V[i]; node & nd = N[id]; if(nd.q) continue; int d = nd.E[0].second + nd.E[1].second; int a = nd.E[0].first, b = nd.E[1].first; N[a].removeEdge(id); N[b].removeEdge(id); N[a].addEdge(b,d); N[b].addEdge(a,d); N.erase(id); // printf("ERASE %d\n", id); } // printf("d2 %d\n", N.size());; // FOR(i, q) printf("%d) %d->%d\n", i+1, QueryFrom(i+1), QueryTo(i+1)); // // for(auto it = N.begin(); it != N.end(); it++){ // node & nd = it->second; // // printf("node %d/%d[%d]) ->%d(%d) ->%d(%d) <%d>\n", it->first, nd.id, nd.q, nd.E[0].first, nd.E[0].second, nd.E[1].first, nd.E[1].second, nd.E.size()); // } int n = NumberOfStudents(); FOR(i, q) { int res = searchQuery(N, QueryFrom(i+1), QueryTo(i+1)); printf("%d\n", min(n-res, res)); } } 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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | #include "kollib.h" #include "message.h" #include <cstdio> #include <cmath> #include <ctime> #include <cstdlib> #include <algorithm> #include <deque> #include <unordered_set> #include <unordered_map> #define LOG(args...) //#define LOG(args...) fprintf(stderr, args) #define FOR(i, n) for(int i = 0, __n = (n); i<__n; i++) using namespace std; typedef pair<int,int> para; //#define TICK(s) fprintf(stderr, "%s: %.2f\n", s, clock()/(CLOCKS_PER_SEC*1.0)) #define TICK(args...) void sendVector(const vector<int> & V, int target) { PutInt(target, V.size()); FOR(i, V.size()) PutInt(target, V[i]); Send(target); } void receiveVector(vector<int>& V, int from) { int f = Receive(from); int n = GetInt(from); FOR(i, n) V.push_back(GetInt(from)); } void broadcast(vector<int> & V) { int nodes = NumberOfNodes(); int id = MyNodeId(); // printf("broadcast me\n"); if(id != 0) { receiveVector(V, id>>1); } if(id<<1 < nodes) sendVector(V, id<<1); if(1+(id<<1) < nodes) sendVector(V, 1+(id<<1)); } // // int getDotsCount1() { // int n = NumberOfStudents(); // int q = NumberOfQueries(); // int cnt = NumberOfNodes() * 40; // if(cnt > n/10) cnt = n/10; // if(cnt < 2*q) cnt = 2*q; // if(cnt > n) cnt = n; // // // printf("dot count %d\n", cnt); // return cnt; // // } void initializeDots(vector<int> & V) { int n = NumberOfStudents(); int q = NumberOfQueries(); // int cnt = getDotsCount1(); unordered_set<int> S; int id = MyNodeId(); if(id == 0) { FOR(i, (20 + NumberOfNodes()) * 30) { S.insert(rand()%n + 1); } FOR(i, q) { int a = QueryFrom(i+1); int b = QueryTo(i+1); S.insert(a); S.insert(b); } // while(S.size() < cnt) { // S.insert(rand()%n + 1); // } for(auto it = S.begin(); it != S.end(); it++) { V.push_back(*it); } broadcast(V); } else { broadcast(V); } } para subsearch(unordered_set<int>& M, int x, int prev) { int x1 = x; int d = 0; while(1) { int a = FirstNeighbor(x); if(a == prev) { a = SecondNeighbor(x);; } prev = x; x = a; d++; if(M.find(x) != M.end()) return para(x, d); } } struct edge { int a, b, dist; edge(int a, int b, int d):a(a), b(b), dist(d) {}; void Put(int to) { PutInt(to, a); PutInt(to, b); PutInt(to, dist); } static edge Get(int from) { int a = GetInt(from), b = GetInt(from), c=GetInt(from); return edge(a,b,c); } }; pair<edge,edge> search(unordered_set<int> & M, int x) { // // if(x < 1 || x > NumberOfStudents()) { // printf("DUPA %d\n", x); // } int a = FirstNeighbor(x); int b = SecondNeighbor(x); para a1 = subsearch(M, x, a); // printf("found %d->%d (%d)\n", x, a1.first, a1.second); para a2 = subsearch(M, x, b); // printf("found %d->%d (%d)\n", x, a2.first, a2.second); return make_pair(edge(x, a1.first, a1.second), edge(x, a2.first, a2.second)); } void algo(const vector<int> & V, vector<edge>& E) { unordered_set<int> M; int d = V.size(); FOR(i, d) { M.insert(V[i]);; } int id = MyNodeId(); int nodes = NumberOfNodes(); for(int i = id; i<d; i+=nodes) { int x = V[i]; // printf("%d> %d\n", i, x); pair<edge,edge> e = search(M, x); E.push_back(e.first); E.push_back(e.second); } } void gatherEdges(vector<edge> & E) { int id = MyNodeId(); int nodes = NumberOfNodes(); if(id != 0) { PutInt(0, E.size()); FOR(i, E.size()){ E[i].Put(0); } Send(0); E.clear(); } else { for(int node = 1; node < nodes; node++) { Receive(node); int n = GetInt(node); FOR(i, n) { E.push_back(edge::Get(node)); } } } } struct node { int id; int q; vector<para> E; node(int id):id(id),q(0) {} node():id(-1){} void removeEdge(int id2) { // printf("rem edge %d->%d\n", id, id2); FOR(i, E.size()) { if(E[i].first == id2) { swap(E[i], E[E.size()-1]); E.pop_back(); return; } } // printf("not removed\n"); } void addEdge(int id2, int d) { // printf("add edge %d->%d\n", id, id2); E.push_back(para(id2,d)); } }; int searchQuery(unordered_map<int,node> & N, int a, int b) { int id = a; int prev = -1; int dist = 0; while(id != b) { node & n = N[id]; int next = n.E[0].first, d = n.E[0].second; if(next == prev) { next = n.E[1].first; d = n.E[1].second; } dist += d; prev = id; id = next; } return dist; } int main() { // printf("welcome \n"); srand(time(0)); vector<int> V; TICK("start"); initializeDots(V); TICK("init"); // printf("initialized\n"); vector<edge> E; algo(V, E); TICK("algo"); // printf("algoed\n"); gatherEdges(E); TICK("gather"); // printf("gathered %d\n", E.size()); if(MyNodeId() == 0) { unordered_map<int,node> N; FOR(i,V.size()) { N[V[i]] = node(V[i]); } int q = NumberOfQueries(); // for(auto it = N.begin(); it != N.end(); it++){ // node & nd = it->second; // printf("node %d)\n", nd.id); // } // FOR(i, q) { int a = QueryFrom(i+1); int b = QueryTo(i+1); N[a].q = 1; N[b].q = 1; } FOR(i, E.size()) { int a = E[i].a, b= E[i].b; int d = E[i].dist; // printf("edge %d->%d (%d)\n", a,b,d); N[a].addEdge(b,d); //N[b].addEdge(a,d); } // printf("d \n"); // for(auto it = N.begin(); it != N.end(); it++){ // node & nd = it->second; // printf("node %d[%d]) ->%d(%d) ->%d(%d) <%d>\n", nd.id, nd.q, nd.E[0].first, nd.E[0].second, nd.E[1].first, nd.E[1].second, nd.E.size()); // } FOR(i, V.size()) { int id = V[i]; node & nd = N[id]; if(nd.q) continue; int d = nd.E[0].second + nd.E[1].second; int a = nd.E[0].first, b = nd.E[1].first; N[a].removeEdge(id); N[b].removeEdge(id); N[a].addEdge(b,d); N[b].addEdge(a,d); N.erase(id); // printf("ERASE %d\n", id); } // printf("d2 %d\n", N.size());; // FOR(i, q) printf("%d) %d->%d\n", i+1, QueryFrom(i+1), QueryTo(i+1)); // // for(auto it = N.begin(); it != N.end(); it++){ // node & nd = it->second; // // printf("node %d/%d[%d]) ->%d(%d) ->%d(%d) <%d>\n", it->first, nd.id, nd.q, nd.E[0].first, nd.E[0].second, nd.E[1].first, nd.E[1].second, nd.E.size()); // } int n = NumberOfStudents(); FOR(i, q) { int res = searchQuery(N, QueryFrom(i+1), QueryTo(i+1)); printf("%d\n", min(n-res, res)); } } return 0; } |