#include "kollib.h" #include "message.h" #include <iostream> #include <set> #include <map> #include <vector> using namespace std; #define STARTER 0 #define STOPER 1 #define DIR_FIRST 1 #define DIR_SECOND 2 #define IDX_FROM 0 #define IDX_TO 1 typedef enum { READY, START, SOLUTION, STOP } MSG_TYPE; void sendREADY(int to); void receiveREADY(int from); void sendSTOP(int to); void sendSTOPAll(); void sendSTART(int to, int begin, int dir); void receiveSTART(int from); void sendSOLUTION(int to, int begin, int end, int dist); void receiveSOLUTION(int from); set<int> students; void sendREADY(int to) { PutInt(to, READY); Send(to); } void receiveREADY(int from) { static set<int>::iterator student = students.begin(); static int dir = DIR_FIRST; if (student != students.end()) { sendSTART(from, *student, dir); if (dir == DIR_FIRST) { dir = DIR_SECOND; } else { dir = DIR_FIRST; ++student; } } } void sendSTOP(int to) { PutInt(to, STOP); Send(to); } void sendSTOPAll() { for(int i=0;i<NumberOfNodes();i++)sendSTOP(i); } void sendSTART(int to, int begin, int dir) { PutInt(to, START); PutInt(to, begin); PutInt(to, dir); Send(to); } void receiveSTART(int from) { int orig_begin = GetInt(from); int prev = orig_begin; int dir = GetInt(from); int begin = dir == DIR_FIRST?FirstNeighbor(prev):SecondNeighbor(prev); int dist=1; while (students.find(begin)==students.end()) { int n1 = FirstNeighbor(begin); int n2 = SecondNeighbor(begin); if (n1 != prev) { prev=begin; begin=n1; } else { prev=begin; begin=n2; } dist++; } sendSOLUTION(STOPER,orig_begin,begin,min(dist,NumberOfStudents()-dist)); sendREADY(from); } void sendSOLUTION(int to, int begin, int end, int dist) { PutInt(to, SOLUTION); PutInt(to, begin); PutInt(to, end); PutInt(to, dist); Send(to); } map<int,map<int,int> > sols; int get_dist1(int f, int t) { int b=-1; int s=0; while (f!=t) { int n1 = sols[f].begin()->first; int s1 = sols[f].begin()->second; int n2 = (++(sols[f].begin()))->first; int s2 = (++(sols[f].begin()))->second; if (n1 != b) { b=f; f=n1; s+=s1; } else { b=f; f=n2; s+=s2; } } return min(s,NumberOfStudents()-s); } int get_dist2(int f, int t) { int b=-1; int s=0; while (f!=t) { int n1 = sols[f].begin()->first; int s1 = sols[f].begin()->second; int n2 = (++(sols[f].begin()))->first; int s2 = (++(sols[f].begin()))->second; if (n2!=b) { b=f; f=n2; s+=s2; } else { b=f; f=n1; s+=s1; } } return min(s,NumberOfStudents()-s); } void receiveSOLUTION(int from) { int begin = GetInt(from); int end = GetInt(from); int dist = GetInt(from); //cout << "received solution begin:"<<begin<<" end:"<<end<<" dist:"<<dist<<endl; sols[begin][end]=dist; sols[end][begin]=dist; static int i=0; i++; //printf("solution nr%d\n",i); if(i==students.size()*2) { /* for(map<int,map<int,int> >::iterator it = sols.begin();it!=sols.end();++it) { printf("sols %d: [%d,%d,%d,%d]\n",it->first,it->second.begin()->first,it->second.begin()->second,(++(it->second.begin()))->first,(++(it->second.begin()))->second); } */ int q=1; while (q<=NumberOfQueries()) { int f = QueryFrom(q); int t = QueryTo(q); int s1 = get_dist1(f,t); int s2 = get_dist2(f,t); printf("%d\n",min(s1,s2)); q++; } sendSTOPAll(); } } int receive() { int from = Receive(-1); MSG_TYPE msg_type = (MSG_TYPE)GetInt(from); switch(msg_type) { case READY: receiveREADY(from); break; case START: receiveSTART(from); break; case SOLUTION: receiveSOLUTION(from); break; case STOP: return -1; break; } return 0; } void init() { for (int i=0;i<NumberOfQueries();i++) { students.insert(QueryFrom(i+1)); students.insert(QueryTo(i+1)); } for (int i=0;i<NumberOfNodes()*2;i++) { int r = (abs(i*(i+100007)+9432898523))%NumberOfStudents() + 1; students.insert(r); } } void start() { while (1) { if(receive()<0)return; } } void work() { sendREADY(STARTER); while (1) { if(receive()<0)return; } } void stop() { while (1) { if(receive()<0)return; } } int main() { if(NumberOfQueries()==0) return 0; init(); if (MyNodeId() == STARTER) start(); else if (MyNodeId() == STOPER) stop(); else work(); 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 | #include "kollib.h" #include "message.h" #include <iostream> #include <set> #include <map> #include <vector> using namespace std; #define STARTER 0 #define STOPER 1 #define DIR_FIRST 1 #define DIR_SECOND 2 #define IDX_FROM 0 #define IDX_TO 1 typedef enum { READY, START, SOLUTION, STOP } MSG_TYPE; void sendREADY(int to); void receiveREADY(int from); void sendSTOP(int to); void sendSTOPAll(); void sendSTART(int to, int begin, int dir); void receiveSTART(int from); void sendSOLUTION(int to, int begin, int end, int dist); void receiveSOLUTION(int from); set<int> students; void sendREADY(int to) { PutInt(to, READY); Send(to); } void receiveREADY(int from) { static set<int>::iterator student = students.begin(); static int dir = DIR_FIRST; if (student != students.end()) { sendSTART(from, *student, dir); if (dir == DIR_FIRST) { dir = DIR_SECOND; } else { dir = DIR_FIRST; ++student; } } } void sendSTOP(int to) { PutInt(to, STOP); Send(to); } void sendSTOPAll() { for(int i=0;i<NumberOfNodes();i++)sendSTOP(i); } void sendSTART(int to, int begin, int dir) { PutInt(to, START); PutInt(to, begin); PutInt(to, dir); Send(to); } void receiveSTART(int from) { int orig_begin = GetInt(from); int prev = orig_begin; int dir = GetInt(from); int begin = dir == DIR_FIRST?FirstNeighbor(prev):SecondNeighbor(prev); int dist=1; while (students.find(begin)==students.end()) { int n1 = FirstNeighbor(begin); int n2 = SecondNeighbor(begin); if (n1 != prev) { prev=begin; begin=n1; } else { prev=begin; begin=n2; } dist++; } sendSOLUTION(STOPER,orig_begin,begin,min(dist,NumberOfStudents()-dist)); sendREADY(from); } void sendSOLUTION(int to, int begin, int end, int dist) { PutInt(to, SOLUTION); PutInt(to, begin); PutInt(to, end); PutInt(to, dist); Send(to); } map<int,map<int,int> > sols; int get_dist1(int f, int t) { int b=-1; int s=0; while (f!=t) { int n1 = sols[f].begin()->first; int s1 = sols[f].begin()->second; int n2 = (++(sols[f].begin()))->first; int s2 = (++(sols[f].begin()))->second; if (n1 != b) { b=f; f=n1; s+=s1; } else { b=f; f=n2; s+=s2; } } return min(s,NumberOfStudents()-s); } int get_dist2(int f, int t) { int b=-1; int s=0; while (f!=t) { int n1 = sols[f].begin()->first; int s1 = sols[f].begin()->second; int n2 = (++(sols[f].begin()))->first; int s2 = (++(sols[f].begin()))->second; if (n2!=b) { b=f; f=n2; s+=s2; } else { b=f; f=n1; s+=s1; } } return min(s,NumberOfStudents()-s); } void receiveSOLUTION(int from) { int begin = GetInt(from); int end = GetInt(from); int dist = GetInt(from); //cout << "received solution begin:"<<begin<<" end:"<<end<<" dist:"<<dist<<endl; sols[begin][end]=dist; sols[end][begin]=dist; static int i=0; i++; //printf("solution nr%d\n",i); if(i==students.size()*2) { /* for(map<int,map<int,int> >::iterator it = sols.begin();it!=sols.end();++it) { printf("sols %d: [%d,%d,%d,%d]\n",it->first,it->second.begin()->first,it->second.begin()->second,(++(it->second.begin()))->first,(++(it->second.begin()))->second); } */ int q=1; while (q<=NumberOfQueries()) { int f = QueryFrom(q); int t = QueryTo(q); int s1 = get_dist1(f,t); int s2 = get_dist2(f,t); printf("%d\n",min(s1,s2)); q++; } sendSTOPAll(); } } int receive() { int from = Receive(-1); MSG_TYPE msg_type = (MSG_TYPE)GetInt(from); switch(msg_type) { case READY: receiveREADY(from); break; case START: receiveSTART(from); break; case SOLUTION: receiveSOLUTION(from); break; case STOP: return -1; break; } return 0; } void init() { for (int i=0;i<NumberOfQueries();i++) { students.insert(QueryFrom(i+1)); students.insert(QueryTo(i+1)); } for (int i=0;i<NumberOfNodes()*2;i++) { int r = (abs(i*(i+100007)+9432898523))%NumberOfStudents() + 1; students.insert(r); } } void start() { while (1) { if(receive()<0)return; } } void work() { sendREADY(STARTER); while (1) { if(receive()<0)return; } } void stop() { while (1) { if(receive()<0)return; } } int main() { if(NumberOfQueries()==0) return 0; init(); if (MyNodeId() == STARTER) start(); else if (MyNodeId() == STOPER) stop(); else work(); return 0; } |