#include "kollib.h" #include "message.h" #include <algorithm> #include <cstdio> //~ #define E(arg...) fprintf(stderr, arg) #define E(arg...) using namespace std; typedef unsigned long long ULL; const int M = 200; const int K = 1e6 + 2*M; struct prng { static const ULL A = 6364136223846793005llu; static const ULL C = 1442695040888963407llu; ULL x; void seed(ULL s) { x = s; } int operator()(int f, int t) { x = A*x + C; return x % (t-f+1) + f; } }; struct graph { int n, v[K+1], t[K+1][2], l[K+1][2], d[K+1]; void addv(int x) { v[++n] = x; } void adde(int a, int b, int len) { a = find(a); b = find(b); bool o1 = t[a][0], o2 = t[b][0]; t[a][o1] = b; l[a][o1] = len; t[b][o2] = a; l[b][o2] = len; } void uniq() { sort(v+1, v+1+n); n = unique(v+1, v+1+n)-v-1; } void visit() { int at = 1; d[1] = 1; for (int i=0; i<n-1; ++i) { bool o = d[t[at][0]]; d[t[at][o]] = d[at] + l[at][o]; at = t[at][o]; } E("after visit:"); E("\nv[i]:"); for (int i=1; i<=n; ++i) E(" %d", v[i]); E("\nt[i]:"); for (int i=1; i<=n; ++i) E(" (%d, %d)", t[i][0], t[i][1]); E("\nd[i]:"); for (int i=1; i<=n; ++i) E(" %d", d[i]); E("\n"); } int find(int x) { int p = lower_bound(v+1, v+1+n, x) - v; E("p = %d in find; n = %d\n", p, n); if (p > n || v[p] != x) return 0; else return p; } int dist(int a, int b) { E("dist\n"); int r = abs(d[find(a)] - d[find(b)]); E("enddist\n"); return r; } }; int NN, ID, n, m; graph g; prng rnd; void read() { NN = NumberOfNodes(); ID = MyNodeId(); n = NumberOfStudents(); m = NumberOfQueries(); int k = max(10, 2*m + n/1000); rnd.seed(ULL(NN)*n*127); for (int i=1; i<=m; ++i) g.addv(QueryFrom(i)), g.addv(QueryTo(i)); while (g.n < k) g.addv(rnd(1, n)); g.uniq(); } void last() { for (int i=0; i<NN-1; ++i) { int f = Receive(-1); while (1) { int x = GetInt(f); if (!x) break; int y = GetInt(f), l = GetInt(f); g.adde(x, y, l); } } g.visit(); for (int i=1; i<=m; ++i) { int r = g.dist(QueryFrom(i), QueryTo(i)); printf("%d\n", min(r, n-r)); } } int walk(int x, int y) { int begin = x, l = 1; while (!g.find(y)) { int z = FirstNeighbor(y); if (x == z) z = SecondNeighbor(y); x = y; y = z; ++l; } if (begin < y) { PutInt(NN-1, begin); PutInt(NN-1, y); PutInt(NN-1, l); } } void notlast() { int n = (g.n+NN-2) / (NN-1); int a = ID*n+1; int b = min(g.n+1, a+n); while (a < b) { int x = g.v[a++]; walk(x, FirstNeighbor(x)); walk(x, SecondNeighbor(x)); } PutInt(NN-1, 0); Send(NN-1); } int main() { read(); if (ID == NN-1) last(); else notlast(); 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 | #include "kollib.h" #include "message.h" #include <algorithm> #include <cstdio> //~ #define E(arg...) fprintf(stderr, arg) #define E(arg...) using namespace std; typedef unsigned long long ULL; const int M = 200; const int K = 1e6 + 2*M; struct prng { static const ULL A = 6364136223846793005llu; static const ULL C = 1442695040888963407llu; ULL x; void seed(ULL s) { x = s; } int operator()(int f, int t) { x = A*x + C; return x % (t-f+1) + f; } }; struct graph { int n, v[K+1], t[K+1][2], l[K+1][2], d[K+1]; void addv(int x) { v[++n] = x; } void adde(int a, int b, int len) { a = find(a); b = find(b); bool o1 = t[a][0], o2 = t[b][0]; t[a][o1] = b; l[a][o1] = len; t[b][o2] = a; l[b][o2] = len; } void uniq() { sort(v+1, v+1+n); n = unique(v+1, v+1+n)-v-1; } void visit() { int at = 1; d[1] = 1; for (int i=0; i<n-1; ++i) { bool o = d[t[at][0]]; d[t[at][o]] = d[at] + l[at][o]; at = t[at][o]; } E("after visit:"); E("\nv[i]:"); for (int i=1; i<=n; ++i) E(" %d", v[i]); E("\nt[i]:"); for (int i=1; i<=n; ++i) E(" (%d, %d)", t[i][0], t[i][1]); E("\nd[i]:"); for (int i=1; i<=n; ++i) E(" %d", d[i]); E("\n"); } int find(int x) { int p = lower_bound(v+1, v+1+n, x) - v; E("p = %d in find; n = %d\n", p, n); if (p > n || v[p] != x) return 0; else return p; } int dist(int a, int b) { E("dist\n"); int r = abs(d[find(a)] - d[find(b)]); E("enddist\n"); return r; } }; int NN, ID, n, m; graph g; prng rnd; void read() { NN = NumberOfNodes(); ID = MyNodeId(); n = NumberOfStudents(); m = NumberOfQueries(); int k = max(10, 2*m + n/1000); rnd.seed(ULL(NN)*n*127); for (int i=1; i<=m; ++i) g.addv(QueryFrom(i)), g.addv(QueryTo(i)); while (g.n < k) g.addv(rnd(1, n)); g.uniq(); } void last() { for (int i=0; i<NN-1; ++i) { int f = Receive(-1); while (1) { int x = GetInt(f); if (!x) break; int y = GetInt(f), l = GetInt(f); g.adde(x, y, l); } } g.visit(); for (int i=1; i<=m; ++i) { int r = g.dist(QueryFrom(i), QueryTo(i)); printf("%d\n", min(r, n-r)); } } int walk(int x, int y) { int begin = x, l = 1; while (!g.find(y)) { int z = FirstNeighbor(y); if (x == z) z = SecondNeighbor(y); x = y; y = z; ++l; } if (begin < y) { PutInt(NN-1, begin); PutInt(NN-1, y); PutInt(NN-1, l); } } void notlast() { int n = (g.n+NN-2) / (NN-1); int a = ID*n+1; int b = min(g.n+1, a+n); while (a < b) { int x = g.v[a++]; walk(x, FirstNeighbor(x)); walk(x, SecondNeighbor(x)); } PutInt(NN-1, 0); Send(NN-1); } int main() { read(); if (ID == NN-1) last(); else notlast(); return 0; } |