#include "kollib.h" #include <iostream> #include <message.h> #include <set> #include <vector> #include <stdlib.h> /* srand, rand */ #include <time.h> /* time */ #include <map> #include <algorithm> using namespace std; const int INF = 1000000009; const int repeat = 20; const int n = NumberOfNodes(); const int students = NumberOfStudents(); int p; void mainC() { srand (time(NULL)); int q = NumberOfQueries(); pair<int, int> Q[q]; p =( (2*q+repeat*(n-1) ) / (n-1) ) * (n-1); set<int> S; for(int i = 1; i<=q; i++) { int a = QueryFrom(i), b=QueryTo(i); if(S.count(a) == 0) S.insert(a); if(S.count(b) == 0) S.insert(b); Q[i] = make_pair(a, b); } while(S.size() < p) { int x = rand()%students; x++; if(S.count(x) == 0) S.insert(x); } vector<int> V (S.begin(), S.end()); random_shuffle(V.begin(), V.end()); //for(int i = 0; i<V.size(); i++) printf("%d ", V[i]); //printf("\n"); //printf("%d\n", p); map<int, int> M; for(int i = 0; i<V.size(); i++) M.insert(make_pair(V[i], i)); for(int i = 1; i<n; i++) { PutInt(i, V.size()); for(int j = 0; j<V.size(); j++) PutInt(i, V[j]); Send(i); } map<int, pair<pair<int, int>, pair<int, int> > > G; for(int j = 0; j<p/(n-1); j++) { for(int i = 1; i<n; i++) { int inst = Receive(-1); int x = GetInt(inst); int y = GetInt(inst); int d = GetInt(inst); //printf("Odleglosc z %d->%d do %d->%d wynosi %d\n", x, M[x], y, M[y], d); int y2 = GetInt(inst); int d2 = GetInt(inst); G.insert(make_pair(M[x], make_pair(make_pair(M[y], d), make_pair(M[y2], d2)))); //printf("Odleglosc z %d do %d wynosi %d\n", x, y, d); } } /*for(int i = 0; i<p; i++) { for(int j = 0; j<p; j++) printf("%d ", G[i][j]); printf("\n"); }*/ int D[p+1]; D[0] = 0; int w = 0; int s = G[0].first.first; D[s] = G[0].first.second; //printf("XXXXXXXXXXXXXX\n"); for(int i = 1; i<p-1; i++) { int x = G[s].first.first; if(x == w) { x = G[s].second.first; D[x] = D[s] + G[s].second.second; } else D[x] = D[s] + G[s].first.second; w = s; s = x; } //printf("XXXXXXXXXXXXXX\n"); //for(int i = 0; i<p; i++) printf("%d ", D[i]); //printf("\n"); for(int i = 1; i<=q; i++) { int x = abs(D[M[Q[i].first]] - D[M[Q[i].second]]); printf("%d\n", min(x, students-x)); } } void otherC() { Receive(0); int p = GetInt(0); set<int> S; vector<int> V; for(int i = 0; i<p; i++) { int x = GetInt(0); S.insert(x); V.push_back(x); } //printf("Maszyna %d przyjęła %d i zajmuje się %d\n", myNumber, S.size(), V[myNumber]); for(int k = 0; k<(p/(n-1)); k++) { int myNumber = k*(n-1)+MyNodeId()-1; int where = V[myNumber], sup = FirstNeighbor(V[myNumber]); int counter = 1; while(S.count(sup) == 0) { int a = FirstNeighbor(sup); if(a == where) a = SecondNeighbor(sup); where = sup; sup = a; counter++; } PutInt(0, V[myNumber]); PutInt(0, sup); PutInt(0, counter); where = V[myNumber]; sup = SecondNeighbor(V[myNumber]); counter = 1; while(S.count(sup) == 0) { int a = FirstNeighbor(sup); if(a == where) a = SecondNeighbor(sup); where = sup; sup = a; counter++; } PutInt(0, sup); PutInt(0, counter); Send(0); } } int main() { if(students < 100000) { if(MyNodeId() != 0) return 0; int where = 1, sup = FirstNeighbor(1); int counter = 1; int students = NumberOfStudents(); int D[students+1]; D[1] = 0; while(sup != 1) { D[sup] = counter; int a = FirstNeighbor(sup); if(a == where) a = SecondNeighbor(sup); where = sup; sup = a; counter++; } int q = NumberOfQueries(); for(int i = 1; i<=q; i++) { int a = QueryFrom(i), b=QueryTo(i); int x = abs(D[a] - D[b]); printf("%d\n", min(x, students-x)); } return 0; } if(MyNodeId() == 0) mainC(); else otherC(); 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 | #include "kollib.h" #include <iostream> #include <message.h> #include <set> #include <vector> #include <stdlib.h> /* srand, rand */ #include <time.h> /* time */ #include <map> #include <algorithm> using namespace std; const int INF = 1000000009; const int repeat = 20; const int n = NumberOfNodes(); const int students = NumberOfStudents(); int p; void mainC() { srand (time(NULL)); int q = NumberOfQueries(); pair<int, int> Q[q]; p =( (2*q+repeat*(n-1) ) / (n-1) ) * (n-1); set<int> S; for(int i = 1; i<=q; i++) { int a = QueryFrom(i), b=QueryTo(i); if(S.count(a) == 0) S.insert(a); if(S.count(b) == 0) S.insert(b); Q[i] = make_pair(a, b); } while(S.size() < p) { int x = rand()%students; x++; if(S.count(x) == 0) S.insert(x); } vector<int> V (S.begin(), S.end()); random_shuffle(V.begin(), V.end()); //for(int i = 0; i<V.size(); i++) printf("%d ", V[i]); //printf("\n"); //printf("%d\n", p); map<int, int> M; for(int i = 0; i<V.size(); i++) M.insert(make_pair(V[i], i)); for(int i = 1; i<n; i++) { PutInt(i, V.size()); for(int j = 0; j<V.size(); j++) PutInt(i, V[j]); Send(i); } map<int, pair<pair<int, int>, pair<int, int> > > G; for(int j = 0; j<p/(n-1); j++) { for(int i = 1; i<n; i++) { int inst = Receive(-1); int x = GetInt(inst); int y = GetInt(inst); int d = GetInt(inst); //printf("Odleglosc z %d->%d do %d->%d wynosi %d\n", x, M[x], y, M[y], d); int y2 = GetInt(inst); int d2 = GetInt(inst); G.insert(make_pair(M[x], make_pair(make_pair(M[y], d), make_pair(M[y2], d2)))); //printf("Odleglosc z %d do %d wynosi %d\n", x, y, d); } } /*for(int i = 0; i<p; i++) { for(int j = 0; j<p; j++) printf("%d ", G[i][j]); printf("\n"); }*/ int D[p+1]; D[0] = 0; int w = 0; int s = G[0].first.first; D[s] = G[0].first.second; //printf("XXXXXXXXXXXXXX\n"); for(int i = 1; i<p-1; i++) { int x = G[s].first.first; if(x == w) { x = G[s].second.first; D[x] = D[s] + G[s].second.second; } else D[x] = D[s] + G[s].first.second; w = s; s = x; } //printf("XXXXXXXXXXXXXX\n"); //for(int i = 0; i<p; i++) printf("%d ", D[i]); //printf("\n"); for(int i = 1; i<=q; i++) { int x = abs(D[M[Q[i].first]] - D[M[Q[i].second]]); printf("%d\n", min(x, students-x)); } } void otherC() { Receive(0); int p = GetInt(0); set<int> S; vector<int> V; for(int i = 0; i<p; i++) { int x = GetInt(0); S.insert(x); V.push_back(x); } //printf("Maszyna %d przyjęła %d i zajmuje się %d\n", myNumber, S.size(), V[myNumber]); for(int k = 0; k<(p/(n-1)); k++) { int myNumber = k*(n-1)+MyNodeId()-1; int where = V[myNumber], sup = FirstNeighbor(V[myNumber]); int counter = 1; while(S.count(sup) == 0) { int a = FirstNeighbor(sup); if(a == where) a = SecondNeighbor(sup); where = sup; sup = a; counter++; } PutInt(0, V[myNumber]); PutInt(0, sup); PutInt(0, counter); where = V[myNumber]; sup = SecondNeighbor(V[myNumber]); counter = 1; while(S.count(sup) == 0) { int a = FirstNeighbor(sup); if(a == where) a = SecondNeighbor(sup); where = sup; sup = a; counter++; } PutInt(0, sup); PutInt(0, counter); Send(0); } } int main() { if(students < 100000) { if(MyNodeId() != 0) return 0; int where = 1, sup = FirstNeighbor(1); int counter = 1; int students = NumberOfStudents(); int D[students+1]; D[1] = 0; while(sup != 1) { D[sup] = counter; int a = FirstNeighbor(sup); if(a == where) a = SecondNeighbor(sup); where = sup; sup = a; counter++; } int q = NumberOfQueries(); for(int i = 1; i<=q; i++) { int a = QueryFrom(i), b=QueryTo(i); int x = abs(D[a] - D[b]); printf("%d\n", min(x, students-x)); } return 0; } if(MyNodeId() == 0) mainC(); else otherC(); return 0; } |