#include <string> #include <vector> #include <map> #include <cmath> #include <algorithm> #include <cstdio> #include <set> #include <cstring> #include <list> #include <iostream> #include <cstdarg> #include <cassert> #include "message.h" #include "kollib.h" using namespace std; #define FOR(i,n) for(int i = 0; i < n; i++) #define REP(i,n) for(int i = 1; i <= n; i++) #define FORI(it,n)for(typeof(n.begin()) it = n.begin(); it != n.end(); it++) #define frs first #define sec second #define psh push_back #define mkp make_pair typedef long long LL; typedef long double LD; const int INF = 2147483647; const int MAX = 100100; const int MAX_NODES = 110; const int MAX_Q = 210; int N, NR; #define FINISH 0x0 #define WHO 0x1 #define NEIGH 0x2 #define ORIENT 0x3 #define POS 0x4 #define OFFSET 0x5 int AN[] = {0, 2, 2, 1, 2, 1}; void send(int nr, int cmd, ...) { va_list args; va_start(args, cmd); PutInt(nr, cmd); for(int i = 0; i < AN[cmd]; i++) { PutInt(nr, va_arg(args, int)); } va_end(args); Send(nr); } int receive(int nr, int *cmd, ...) { int ret; va_list args; va_start(args, cmd); ret = Receive(nr); *cmd = GetInt(ret); for(int i = 0; i < AN[*cmd]; i++) { *(va_arg(args, int*)) = GetInt(ret); } va_end(args); return ret; } struct Vertex { int nr, ile; Vertex *next; }; struct Cycle_Buf { Vertex T[MAX_NODES]; int size; void push(int nr, int ile) { T[size].nr = nr; T[size].ile = ile; T[size].next = T + (size + 1); size ++; } void complete() { T[size - 1].next = T; } Vertex *begin() { return T; } } L; int n, m, off; map<int, int> P, M; int W[MAX_NODES]; vector<pair<int,int>> G[MAX_NODES]; int query(int from, int to) { int fpos = INF, tpos = INF, cmd; Vertex *vtx = L.begin(); do { send(vtx->nr, POS, from); receive(vtx->nr, &cmd, &fpos); assert(cmd == POS); if(fpos != INF) break; vtx = vtx->next; } while(vtx->nr != L.begin()->nr); fprintf(stderr, "From: %d is in node %d on pos %d\n", from, vtx->nr, fpos); int start = vtx->nr, war = 0; do { send(vtx->nr, POS, to); receive(vtx->nr, &cmd, &tpos); assert(cmd == POS); if(tpos != INF) { fprintf(stderr, "To: %d is in node %d on pos %d\n", to, vtx->nr, tpos); return abs(war + tpos - fpos); } war += vtx->ile; vtx = vtx->next; } while(vtx->nr != start); return INF; } void dig(int v, int u, int &ile, int &nr) { ile = 1; int w; auto ptr = P.find(u); for(ptr = P.find(u); ptr == P.end(); ptr = P.find(u)) { ile++; w = FirstNeighbor(u); if(w == v) w = SecondNeighbor(u); v = u; u = w; } nr = ptr->sec; } void dig(int v, int u) { int pos = 0; if(M.find(v) != M.end()) M[v] = pos; int w; auto ptr = P.find(u); for(; ptr == P.end(); ptr = P.find(u)) { pos ++; if(M.find(u) != M.end()) M[u] = pos; w = FirstNeighbor(u); if(w == v) w = SecondNeighbor(u); v = u; u = w; } } int FP[MAX_Q], TP[MAX_Q]; int DL[MAX_NODES]; void solve_simple() { REP(i,m) { M[QueryFrom(i)] = INF; M[QueryTo(i)] = INF; } int u = FirstNeighbor(1); P[1] = 0; dig(1, u); REP(i,m) { u = abs(M[QueryFrom(i)] - M[QueryTo(i)]); printf("%d\n", min(u, n- u)); } } int main() { N = NumberOfNodes(); NR = MyNodeId(); m = NumberOfQueries(); n = NumberOfStudents(); if(NR == 0) { if( n < 1000 ) { solve_simple(); return 0; } srand(62734558); int rnd = rand() % n + 1; while(P.size() < N - 1) { if(P.find(rnd) == P.end()) { W[P.size() + 1] = rnd; P[rnd] = P.size() + 1; } rnd = rand() % n + 1; } REP(j, N - 1) { send(1, WHO, j, W[j]); } int u,v,ile,cmd; REP(i, (N - 1) * 2) { u = receive(-1, &cmd, &v, &ile); assert(cmd == NEIGH); G[u].psh(mkp(v,ile)); } int id = 1, next = G[1][0].frs, t; ile = G[1][0].sec; do { send(id, ORIENT, next); L.push(id, ile); for(t = 0; t < 2; t++) if(G[next][t].frs != id) break; id = next; next = G[id][t].frs; ile = G[id][t].sec; } while(id != 1); L.complete(); int sum = 0; Vertex *ptr = L.begin(); id = ptr->nr; do { send(ptr->nr, OFFSET, sum); // fprintf(stderr, "W[%d] = %d, ile = %d offset = %d\n", // ptr->nr, W[ptr->nr], ptr->ile, sum); sum += ptr->ile; ptr = ptr->next; } while(ptr->nr != id); //obsluga query int to, from, pos, ans; REP(i,2*m) { receive(-1, &cmd, &to, &pos); assert(cmd == POS); M[to] = pos; } REP(i,m) { u = abs(M[QueryFrom(i)] - M[QueryTo(i)]); printf("%d\n", min(u, n - u)); } // REP(i, N - 1) send(i, FINISH); } else { // Other nodes if( n < 1000) return 0; int myP, nr, stud, cmd, u ,v, to, nextNR, prevNR, war; REP(i, N - 1) { receive(NR - 1, &cmd, &nr, &stud); assert(cmd == WHO); if(nr == NR) myP = stud; P[stud] = nr; W[nr] = stud; } if(NR != N - 1) { REP(i, N - 1) { send(NR + 1, WHO, i, W[i]); } } REP(i,m) { M[QueryTo(i)] = INF; M[QueryFrom(i)] = INF; } int nr1, nr2, ile1, ile2, u1, u2; vector<int> V1, V2; vector<int> *V; u1 = FirstNeighbor(myP); dig(myP, u1, ile1, nr1); send(0, NEIGH, nr1, ile1); u2 = SecondNeighbor(myP); dig(myP, u2, ile2, nr2); send(0, NEIGH, nr2, ile2); receive(0, &cmd, &nr); assert(cmd == ORIENT); nextNR = nr; if(nr == nr1) u = u1; if(nr == nr2) u = u2; dig(myP, u); //zdobycie offsetow receive(0, &cmd, &off); assert(cmd == OFFSET); REP(i,m) { u = M[QueryTo(i)]; if(u != INF) { // fprintf(stderr, "%d is on pos %d\n", QueryTo(i), u + off); send(0, POS, QueryTo(i), u + off); } u = M[QueryFrom(i)]; if(u != INF) { // fprintf(stderr, "%d is on pos %d\n", QueryFrom(i), u + off); send(0, POS, QueryFrom(i), u + off); } } } }
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 | #include <string> #include <vector> #include <map> #include <cmath> #include <algorithm> #include <cstdio> #include <set> #include <cstring> #include <list> #include <iostream> #include <cstdarg> #include <cassert> #include "message.h" #include "kollib.h" using namespace std; #define FOR(i,n) for(int i = 0; i < n; i++) #define REP(i,n) for(int i = 1; i <= n; i++) #define FORI(it,n)for(typeof(n.begin()) it = n.begin(); it != n.end(); it++) #define frs first #define sec second #define psh push_back #define mkp make_pair typedef long long LL; typedef long double LD; const int INF = 2147483647; const int MAX = 100100; const int MAX_NODES = 110; const int MAX_Q = 210; int N, NR; #define FINISH 0x0 #define WHO 0x1 #define NEIGH 0x2 #define ORIENT 0x3 #define POS 0x4 #define OFFSET 0x5 int AN[] = {0, 2, 2, 1, 2, 1}; void send(int nr, int cmd, ...) { va_list args; va_start(args, cmd); PutInt(nr, cmd); for(int i = 0; i < AN[cmd]; i++) { PutInt(nr, va_arg(args, int)); } va_end(args); Send(nr); } int receive(int nr, int *cmd, ...) { int ret; va_list args; va_start(args, cmd); ret = Receive(nr); *cmd = GetInt(ret); for(int i = 0; i < AN[*cmd]; i++) { *(va_arg(args, int*)) = GetInt(ret); } va_end(args); return ret; } struct Vertex { int nr, ile; Vertex *next; }; struct Cycle_Buf { Vertex T[MAX_NODES]; int size; void push(int nr, int ile) { T[size].nr = nr; T[size].ile = ile; T[size].next = T + (size + 1); size ++; } void complete() { T[size - 1].next = T; } Vertex *begin() { return T; } } L; int n, m, off; map<int, int> P, M; int W[MAX_NODES]; vector<pair<int,int>> G[MAX_NODES]; int query(int from, int to) { int fpos = INF, tpos = INF, cmd; Vertex *vtx = L.begin(); do { send(vtx->nr, POS, from); receive(vtx->nr, &cmd, &fpos); assert(cmd == POS); if(fpos != INF) break; vtx = vtx->next; } while(vtx->nr != L.begin()->nr); fprintf(stderr, "From: %d is in node %d on pos %d\n", from, vtx->nr, fpos); int start = vtx->nr, war = 0; do { send(vtx->nr, POS, to); receive(vtx->nr, &cmd, &tpos); assert(cmd == POS); if(tpos != INF) { fprintf(stderr, "To: %d is in node %d on pos %d\n", to, vtx->nr, tpos); return abs(war + tpos - fpos); } war += vtx->ile; vtx = vtx->next; } while(vtx->nr != start); return INF; } void dig(int v, int u, int &ile, int &nr) { ile = 1; int w; auto ptr = P.find(u); for(ptr = P.find(u); ptr == P.end(); ptr = P.find(u)) { ile++; w = FirstNeighbor(u); if(w == v) w = SecondNeighbor(u); v = u; u = w; } nr = ptr->sec; } void dig(int v, int u) { int pos = 0; if(M.find(v) != M.end()) M[v] = pos; int w; auto ptr = P.find(u); for(; ptr == P.end(); ptr = P.find(u)) { pos ++; if(M.find(u) != M.end()) M[u] = pos; w = FirstNeighbor(u); if(w == v) w = SecondNeighbor(u); v = u; u = w; } } int FP[MAX_Q], TP[MAX_Q]; int DL[MAX_NODES]; void solve_simple() { REP(i,m) { M[QueryFrom(i)] = INF; M[QueryTo(i)] = INF; } int u = FirstNeighbor(1); P[1] = 0; dig(1, u); REP(i,m) { u = abs(M[QueryFrom(i)] - M[QueryTo(i)]); printf("%d\n", min(u, n- u)); } } int main() { N = NumberOfNodes(); NR = MyNodeId(); m = NumberOfQueries(); n = NumberOfStudents(); if(NR == 0) { if( n < 1000 ) { solve_simple(); return 0; } srand(62734558); int rnd = rand() % n + 1; while(P.size() < N - 1) { if(P.find(rnd) == P.end()) { W[P.size() + 1] = rnd; P[rnd] = P.size() + 1; } rnd = rand() % n + 1; } REP(j, N - 1) { send(1, WHO, j, W[j]); } int u,v,ile,cmd; REP(i, (N - 1) * 2) { u = receive(-1, &cmd, &v, &ile); assert(cmd == NEIGH); G[u].psh(mkp(v,ile)); } int id = 1, next = G[1][0].frs, t; ile = G[1][0].sec; do { send(id, ORIENT, next); L.push(id, ile); for(t = 0; t < 2; t++) if(G[next][t].frs != id) break; id = next; next = G[id][t].frs; ile = G[id][t].sec; } while(id != 1); L.complete(); int sum = 0; Vertex *ptr = L.begin(); id = ptr->nr; do { send(ptr->nr, OFFSET, sum); // fprintf(stderr, "W[%d] = %d, ile = %d offset = %d\n", // ptr->nr, W[ptr->nr], ptr->ile, sum); sum += ptr->ile; ptr = ptr->next; } while(ptr->nr != id); //obsluga query int to, from, pos, ans; REP(i,2*m) { receive(-1, &cmd, &to, &pos); assert(cmd == POS); M[to] = pos; } REP(i,m) { u = abs(M[QueryFrom(i)] - M[QueryTo(i)]); printf("%d\n", min(u, n - u)); } // REP(i, N - 1) send(i, FINISH); } else { // Other nodes if( n < 1000) return 0; int myP, nr, stud, cmd, u ,v, to, nextNR, prevNR, war; REP(i, N - 1) { receive(NR - 1, &cmd, &nr, &stud); assert(cmd == WHO); if(nr == NR) myP = stud; P[stud] = nr; W[nr] = stud; } if(NR != N - 1) { REP(i, N - 1) { send(NR + 1, WHO, i, W[i]); } } REP(i,m) { M[QueryTo(i)] = INF; M[QueryFrom(i)] = INF; } int nr1, nr2, ile1, ile2, u1, u2; vector<int> V1, V2; vector<int> *V; u1 = FirstNeighbor(myP); dig(myP, u1, ile1, nr1); send(0, NEIGH, nr1, ile1); u2 = SecondNeighbor(myP); dig(myP, u2, ile2, nr2); send(0, NEIGH, nr2, ile2); receive(0, &cmd, &nr); assert(cmd == ORIENT); nextNR = nr; if(nr == nr1) u = u1; if(nr == nr2) u = u2; dig(myP, u); //zdobycie offsetow receive(0, &cmd, &off); assert(cmd == OFFSET); REP(i,m) { u = M[QueryTo(i)]; if(u != INF) { // fprintf(stderr, "%d is on pos %d\n", QueryTo(i), u + off); send(0, POS, QueryTo(i), u + off); } u = M[QueryFrom(i)]; if(u != INF) { // fprintf(stderr, "%d is on pos %d\n", QueryFrom(i), u + off); send(0, POS, QueryFrom(i), u + off); } } } } |