#include <stdlib.h> #include <stdio.h> #include "message.h" // int NumberOfNodes(); // 10 <= NoN <= 100 // int MyNodeId(); // 0 <= x < NoN // void PutChar(int target, char value); // void PutInt(int target, int value); // void PutLL(int target, long long value); // void Send(int target); // int Receive(int source); // -1 -> returns source // char GetChar(int source); // int GetInt(int source); // long long GetLL(int source); #include "kollib.h" // int NumberOfStudents(); // 3 <= n <= 1,000,000,000 // int FirstNeighbor(int i); // 1 <= i <= n, first[i] < second[i] // int SecondNeighbor(int i); // 1 <= i <= n // int NumberOfQueries(); // 0 <= m <= 200 // int QueryFrom(int i); // 1 <= i <= m // int QueryTo(int i); // 1 <= i <= m int NoN; // 10 <= NoN <= 100 int Node; // 0 <= Node < NoN int N; // 3 <= N <= 1,000,000,000 int M; // 0 <= M <= 200 #define MAXPOINTS 800 int P[MAXPOINTS]; int PfDst[MAXPOINTS]; int PfCnt[MAXPOINTS]; int PfPtr[MAXPOINTS]; int PrDst[MAXPOINTS]; int PrCnt[MAXPOINTS]; int PrPtr[MAXPOINTS]; int pc; int Q1[200]; int Q2[200]; int Q1p[200]; int Q2p[200]; int slow_solve(int i) { int from = Q1[i]; int to = Q2[i]; int prev = -1; int cnt; for (cnt = 0; from != to; ++cnt) { int neigh = FirstNeighbor(from); if (neigh == prev) neigh = SecondNeighbor(from); prev = from; from = neigh; }; return cnt; } int quick_solve(int i) { int prev = -1; int f = Q1[i]; int fp = Q1p[i]; int t = Q2[i]; //int tp = Q2p[i]; int cnt = 0; while (f != t) { #ifdef DUMP if (cnt < 10) fprintf(stderr, "at %d@%d looking for %d (cnt %d)\n", f, fp, t, cnt); #endif /* DUMP */ if (PfDst[fp] != prev) { prev = f; f = PfDst[fp]; cnt += PfCnt[fp]; fp = PfPtr[fp]; } else { prev = f; f = PrDst[fp]; cnt += PrCnt[fp]; fp = PrPtr[fp]; }; }; return cnt; //return slow_solve(i); }; // int rand(void); // The rand() function returns a pseudo-random integer in the range 0 to // RAND_MAX inclusive (i.e., the mathematical range [0, RAND_MAX]). // void srand(unsigned int seed); // The srand() function sets its argument as the seed for a new sequence // of pseudo-random integers to be returned by rand(). These sequences // are repeatable by calling srand() with the same seed value. int random_student (void) { int v1 = rand() % 997; int v2 = rand() % 991; int v3 = rand() % 983; // nie musi byc piekna dystrybucja, to jest 'random enough' int v = v1 * 1000000 + v2 * 1000 + v3; v %= N; return 1 + v; } static inline int cmp(const int * x, const int * y) { if (*x < *y) return -1; if (*x > *y) return +1; return 0; } int void_incr_cmp(const void * x, const void * y) { return cmp((const int*)x, (const int*)y); } static inline int Find(int v) { int i; for (i = 0; i < pc; ++i) if (P[i] == v) return i; return -1; } // HSIZE must be prime #define HSIZE 8191 int H[HSIZE]; static inline int _hash1(int i) { return i % HSIZE; }; static inline int _hash2(int i) { return 1 + (i % 257); // >=1, oraz <HSIZE }; static inline void Mark(int i) { int h1 = _hash1(i); int h2 = _hash2(i); int h = h1; for (;;) { // not infinite because hash table never full if (H[h] == i) return; if (!H[h]) break; h += h2; }; H[h] = i; } static inline int DontHave(int i) { int h1 = _hash1(i); int h2 = _hash2(i); int h = h1; for (;;) { // not infinite because hash table never full if (H[h] == i) return 0; if (!H[h]) return 1; h += h2; }; } int main (void) { int i, j, prev; NoN = NumberOfNodes(); Node = MyNodeId(); N = NumberOfStudents(); M = NumberOfQueries(); if (M == 0) return 0; for (i = 0; i < M; ++i) { Q1[i] = QueryFrom(i + 1); Q2[i] = QueryTo(i + 1); P[pc++] = Q1[i]; P[pc++] = Q2[i]; }; srand(0); while (pc < MAXPOINTS) { P[pc++] = random_student(); }; qsort(P, pc, sizeof(P[0]), void_incr_cmp); j = pc; pc = 0; prev = -1; for (i = 0; i < j; ++i) { if (P[i] != prev) { prev = P[pc++] = P[i]; }; }; for (i = 0; i < pc; ++i) Mark(P[i]); //----TILL HERE ALL NODES---- for (i = Node; i < pc; i += NoN) { int cnt, cur, prev; // dir; #ifdef DUMP fprintf(stderr, "Node %d Process %d\n", Node, i); #endif /* DUMP */ // one way cnt = 0; cur = P[i]; prev = SecondNeighbor(cur); //dir = +1; do { int neigh = FirstNeighbor(cur); if (neigh != prev) { //dir = +1; } else { neigh = SecondNeighbor(cur); //dir = -1; }; prev = cur; cur = neigh; ++cnt; } while (DontHave(cur)); PutInt(0, cur); PutInt(0, cnt); PutInt(0, Find(cur)); // the other way cnt = 0; cur = P[i]; prev = FirstNeighbor(cur); //dir = -1; do { int neigh = FirstNeighbor(cur); if (neigh != prev) { //dir = +1; } else { neigh = SecondNeighbor(cur); //dir = -1; }; prev = cur; cur = neigh; ++cnt; } while (DontHave(cur)); PutInt(0, cur); PutInt(0, cnt); PutInt(0, Find(cur)); // --- Send(0); }; //----FIRST NODE ONLY--- if (Node != 0) return 0; for (i = 0; i < pc; ++i) { int f = Receive(i % NoN); PfDst[i] = GetInt(f); PfCnt[i] = GetInt(f); PfPtr[i] = GetInt(f); PrDst[i] = GetInt(f); PrCnt[i] = GetInt(f); PrPtr[i] = GetInt(f); }; #ifdef DUMP fprintf(stderr, "pc = %d\n", pc); for (i = 0; i < pc; ++i) { fprintf(stderr, "P[%d] = %d (%d@%d|%d) (%d@%d|%d)\n", i, P[i], PfDst[i], PfCnt[i], PfPtr[i], PrDst[i], PrCnt[i], PrPtr[i]); }; #endif /* DUMP */ for (i = 0; i < M; ++i) { Q1p[i] = Find(Q1[i]); Q2p[i] = Find(Q2[i]); }; for (i = 0; i < M; ++i) { int s = quick_solve(i); if (s + s > N) s = N - s; #ifdef DUMP fprintf(stderr, "[%d@%d .. %d@%d] %d\n", Q1[i], Q1p[i], Q2[i], Q2p[i], s); #endif /* DUMP */ printf("%d\n", s); }; 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 | #include <stdlib.h> #include <stdio.h> #include "message.h" // int NumberOfNodes(); // 10 <= NoN <= 100 // int MyNodeId(); // 0 <= x < NoN // void PutChar(int target, char value); // void PutInt(int target, int value); // void PutLL(int target, long long value); // void Send(int target); // int Receive(int source); // -1 -> returns source // char GetChar(int source); // int GetInt(int source); // long long GetLL(int source); #include "kollib.h" // int NumberOfStudents(); // 3 <= n <= 1,000,000,000 // int FirstNeighbor(int i); // 1 <= i <= n, first[i] < second[i] // int SecondNeighbor(int i); // 1 <= i <= n // int NumberOfQueries(); // 0 <= m <= 200 // int QueryFrom(int i); // 1 <= i <= m // int QueryTo(int i); // 1 <= i <= m int NoN; // 10 <= NoN <= 100 int Node; // 0 <= Node < NoN int N; // 3 <= N <= 1,000,000,000 int M; // 0 <= M <= 200 #define MAXPOINTS 800 int P[MAXPOINTS]; int PfDst[MAXPOINTS]; int PfCnt[MAXPOINTS]; int PfPtr[MAXPOINTS]; int PrDst[MAXPOINTS]; int PrCnt[MAXPOINTS]; int PrPtr[MAXPOINTS]; int pc; int Q1[200]; int Q2[200]; int Q1p[200]; int Q2p[200]; int slow_solve(int i) { int from = Q1[i]; int to = Q2[i]; int prev = -1; int cnt; for (cnt = 0; from != to; ++cnt) { int neigh = FirstNeighbor(from); if (neigh == prev) neigh = SecondNeighbor(from); prev = from; from = neigh; }; return cnt; } int quick_solve(int i) { int prev = -1; int f = Q1[i]; int fp = Q1p[i]; int t = Q2[i]; //int tp = Q2p[i]; int cnt = 0; while (f != t) { #ifdef DUMP if (cnt < 10) fprintf(stderr, "at %d@%d looking for %d (cnt %d)\n", f, fp, t, cnt); #endif /* DUMP */ if (PfDst[fp] != prev) { prev = f; f = PfDst[fp]; cnt += PfCnt[fp]; fp = PfPtr[fp]; } else { prev = f; f = PrDst[fp]; cnt += PrCnt[fp]; fp = PrPtr[fp]; }; }; return cnt; //return slow_solve(i); }; // int rand(void); // The rand() function returns a pseudo-random integer in the range 0 to // RAND_MAX inclusive (i.e., the mathematical range [0, RAND_MAX]). // void srand(unsigned int seed); // The srand() function sets its argument as the seed for a new sequence // of pseudo-random integers to be returned by rand(). These sequences // are repeatable by calling srand() with the same seed value. int random_student (void) { int v1 = rand() % 997; int v2 = rand() % 991; int v3 = rand() % 983; // nie musi byc piekna dystrybucja, to jest 'random enough' int v = v1 * 1000000 + v2 * 1000 + v3; v %= N; return 1 + v; } static inline int cmp(const int * x, const int * y) { if (*x < *y) return -1; if (*x > *y) return +1; return 0; } int void_incr_cmp(const void * x, const void * y) { return cmp((const int*)x, (const int*)y); } static inline int Find(int v) { int i; for (i = 0; i < pc; ++i) if (P[i] == v) return i; return -1; } // HSIZE must be prime #define HSIZE 8191 int H[HSIZE]; static inline int _hash1(int i) { return i % HSIZE; }; static inline int _hash2(int i) { return 1 + (i % 257); // >=1, oraz <HSIZE }; static inline void Mark(int i) { int h1 = _hash1(i); int h2 = _hash2(i); int h = h1; for (;;) { // not infinite because hash table never full if (H[h] == i) return; if (!H[h]) break; h += h2; }; H[h] = i; } static inline int DontHave(int i) { int h1 = _hash1(i); int h2 = _hash2(i); int h = h1; for (;;) { // not infinite because hash table never full if (H[h] == i) return 0; if (!H[h]) return 1; h += h2; }; } int main (void) { int i, j, prev; NoN = NumberOfNodes(); Node = MyNodeId(); N = NumberOfStudents(); M = NumberOfQueries(); if (M == 0) return 0; for (i = 0; i < M; ++i) { Q1[i] = QueryFrom(i + 1); Q2[i] = QueryTo(i + 1); P[pc++] = Q1[i]; P[pc++] = Q2[i]; }; srand(0); while (pc < MAXPOINTS) { P[pc++] = random_student(); }; qsort(P, pc, sizeof(P[0]), void_incr_cmp); j = pc; pc = 0; prev = -1; for (i = 0; i < j; ++i) { if (P[i] != prev) { prev = P[pc++] = P[i]; }; }; for (i = 0; i < pc; ++i) Mark(P[i]); //----TILL HERE ALL NODES---- for (i = Node; i < pc; i += NoN) { int cnt, cur, prev; // dir; #ifdef DUMP fprintf(stderr, "Node %d Process %d\n", Node, i); #endif /* DUMP */ // one way cnt = 0; cur = P[i]; prev = SecondNeighbor(cur); //dir = +1; do { int neigh = FirstNeighbor(cur); if (neigh != prev) { //dir = +1; } else { neigh = SecondNeighbor(cur); //dir = -1; }; prev = cur; cur = neigh; ++cnt; } while (DontHave(cur)); PutInt(0, cur); PutInt(0, cnt); PutInt(0, Find(cur)); // the other way cnt = 0; cur = P[i]; prev = FirstNeighbor(cur); //dir = -1; do { int neigh = FirstNeighbor(cur); if (neigh != prev) { //dir = +1; } else { neigh = SecondNeighbor(cur); //dir = -1; }; prev = cur; cur = neigh; ++cnt; } while (DontHave(cur)); PutInt(0, cur); PutInt(0, cnt); PutInt(0, Find(cur)); // --- Send(0); }; //----FIRST NODE ONLY--- if (Node != 0) return 0; for (i = 0; i < pc; ++i) { int f = Receive(i % NoN); PfDst[i] = GetInt(f); PfCnt[i] = GetInt(f); PfPtr[i] = GetInt(f); PrDst[i] = GetInt(f); PrCnt[i] = GetInt(f); PrPtr[i] = GetInt(f); }; #ifdef DUMP fprintf(stderr, "pc = %d\n", pc); for (i = 0; i < pc; ++i) { fprintf(stderr, "P[%d] = %d (%d@%d|%d) (%d@%d|%d)\n", i, P[i], PfDst[i], PfCnt[i], PfPtr[i], PrDst[i], PrCnt[i], PrPtr[i]); }; #endif /* DUMP */ for (i = 0; i < M; ++i) { Q1p[i] = Find(Q1[i]); Q2p[i] = Find(Q2[i]); }; for (i = 0; i < M; ++i) { int s = quick_solve(i); if (s + s > N) s = N - s; #ifdef DUMP fprintf(stderr, "[%d@%d .. %d@%d] %d\n", Q1[i], Q1p[i], Q2[i], Q2p[i], s); #endif /* DUMP */ printf("%d\n", s); }; return 0; } |