#include <map> #include <iostream> #include "kollib.h" #include "message.h" #include <vector> #include <algorithm> #include <set> #include <unordered_map> #include <unordered_set> using namespace std; #define I unsigned int #define DEBUG(x) #define DEBUGC(x) I n; int nodes, myNode, m; I myrand[10]={1630,13716,5189,30134,15641,28833,31924,10109,7472,20578}; #define big 2000000000 #define perKomp 20 I getRand(I x) { I w = 16849; x++; while(x>0) { w*=myrand[x%10]; w++; w%=n; x/=10; } return w+1; } typedef pair<I, I> Kier; struct hash2{ long long operator()(const Kier k) const{ long long w = big; w*=k.first; w+=k.second; return w; } }; typedef unordered_set<I> Points; typedef unordered_map<Kier, pair<I, Kier>, hash2 > Wynik; Points points; Wynik wyniki; template<class T> std::basic_ostream<T>& operator<<(std::basic_ostream<T>& out, const Kier&k) { out << "(" << k.first << "->" << k.second << ")"; return out; } I getPoint(I i) { if(i < 2*m) { if(i&1) return QueryTo(i/2 +1); return QueryFrom(i/2 + 1); } else return getRand(i); } I getOdl(Kier k, I last) { Wynik::iterator it; I odl=0; do { it = wyniki.find(k); if(it == wyniki.end()) { cout << "ERROR getOdl\n"; return 0; } DEBUG(cout << "serv getOdl:" << k << " odl: " << it->second.first << " " << it->second.second << "\n"); odl+=it->second.first; odl%=n; k = it->second.second; I num = FirstNeighbor(k.first); if(num == k.second) { num = SecondNeighbor(k.first); } k.second = num; } while(it->second.second.first != last); return odl; } int main() { nodes = NumberOfNodes(); myNode = MyNodeId(); n = NumberOfStudents(); m = NumberOfQueries(); if(m==0) return 0; int ile_points = max(nodes*5,2*m); for(int i=0;i<ile_points;i++) { points.insert(getPoint(i)); } DEBUG( cout << "points: "; for(Points::iterator it=points.begin();it!=points.end();it++) { cout << *it << " "; } cout << "\n"; ) if(myNode != 0) { Kier kk,kp; I num,ile=0; kk.second = kk.first = 0; kp = kk; while(1) { PutInt(0, kp.first); PutInt(0, kp.second); PutInt(0, ile); PutInt(0, kk.second); PutInt(0, kk.first); Send(0); Receive(0); kp.first = GetInt(0); kp.second = GetInt(0); kk=kp; if(kp.first == 0) return 0; ile=1; while(points.find(kk.second) == points.end()) { DEBUGC(cout << "cli : " << kk << "\n"); num = FirstNeighbor(kk.second); if(num == kk.first) { num = SecondNeighbor(kk.second); } kk.first = kk.second; kk.second = num; ile++; }; } } else { Kier kk,kp; I ile=0, komp; set<Kier> doZrob; DEBUG(cout << "serv DoZrob: "); for(int i=0;i<ile_points;i++) { I p = getPoint(i); Kier k1(p,FirstNeighbor(p)),k2(p,SecondNeighbor(p)); doZrob.insert(k1); doZrob.insert(k2); DEBUG(cout << k1 << " " << k2 << " "); } DEBUG(cout << "\n"); int runKomp = nodes - 1; while(runKomp) { komp = Receive(-1); kp.first = GetInt(komp); kp.second = GetInt(komp); ile = GetInt(komp); kk.first = GetInt(komp); kk.second = GetInt(komp); DEBUG(cout << "serv got: " << komp << " " << kp << " ile: " << ile << " " << kk << "\n"); if(kp.first) { doZrob.erase(kk); doZrob.erase(kp); wyniki.insert(make_pair(kp, make_pair(ile,kk))); wyniki.insert(make_pair(kk, make_pair(ile,kp))); } if(!doZrob.empty()){ kp = *doZrob.begin(); doZrob.erase(doZrob.begin()); } else { kp.second = kp.first = 0; runKomp--; DEBUG(cout << "serv runKomp: " << runKomp << "\n"); } DEBUG(cout << "serv sed: " << komp << " " << kp << "\n"); PutInt(komp, kp.first); PutInt(komp, kp.second); Send(komp); } for(int i=0;i<m;i++) { int ip,ik; ip = QueryFrom(i+1); ik = QueryTo(i+1); Kier k1 = make_pair(ip,FirstNeighbor(ip)); Kier k2 = make_pair(ip,SecondNeighbor(ip)); DEBUG(cout << "serv find: " << k1 << " last: " << ik << "\n"); I od1 = getOdl(k1, ik); DEBUG(cout << "serv find: " << k2 << " last: " << ik << "\n"); I od2 = getOdl(k2, ik); cout << min(od1,od2) << "\n"; } } 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 | #include <map> #include <iostream> #include "kollib.h" #include "message.h" #include <vector> #include <algorithm> #include <set> #include <unordered_map> #include <unordered_set> using namespace std; #define I unsigned int #define DEBUG(x) #define DEBUGC(x) I n; int nodes, myNode, m; I myrand[10]={1630,13716,5189,30134,15641,28833,31924,10109,7472,20578}; #define big 2000000000 #define perKomp 20 I getRand(I x) { I w = 16849; x++; while(x>0) { w*=myrand[x%10]; w++; w%=n; x/=10; } return w+1; } typedef pair<I, I> Kier; struct hash2{ long long operator()(const Kier k) const{ long long w = big; w*=k.first; w+=k.second; return w; } }; typedef unordered_set<I> Points; typedef unordered_map<Kier, pair<I, Kier>, hash2 > Wynik; Points points; Wynik wyniki; template<class T> std::basic_ostream<T>& operator<<(std::basic_ostream<T>& out, const Kier&k) { out << "(" << k.first << "->" << k.second << ")"; return out; } I getPoint(I i) { if(i < 2*m) { if(i&1) return QueryTo(i/2 +1); return QueryFrom(i/2 + 1); } else return getRand(i); } I getOdl(Kier k, I last) { Wynik::iterator it; I odl=0; do { it = wyniki.find(k); if(it == wyniki.end()) { cout << "ERROR getOdl\n"; return 0; } DEBUG(cout << "serv getOdl:" << k << " odl: " << it->second.first << " " << it->second.second << "\n"); odl+=it->second.first; odl%=n; k = it->second.second; I num = FirstNeighbor(k.first); if(num == k.second) { num = SecondNeighbor(k.first); } k.second = num; } while(it->second.second.first != last); return odl; } int main() { nodes = NumberOfNodes(); myNode = MyNodeId(); n = NumberOfStudents(); m = NumberOfQueries(); if(m==0) return 0; int ile_points = max(nodes*5,2*m); for(int i=0;i<ile_points;i++) { points.insert(getPoint(i)); } DEBUG( cout << "points: "; for(Points::iterator it=points.begin();it!=points.end();it++) { cout << *it << " "; } cout << "\n"; ) if(myNode != 0) { Kier kk,kp; I num,ile=0; kk.second = kk.first = 0; kp = kk; while(1) { PutInt(0, kp.first); PutInt(0, kp.second); PutInt(0, ile); PutInt(0, kk.second); PutInt(0, kk.first); Send(0); Receive(0); kp.first = GetInt(0); kp.second = GetInt(0); kk=kp; if(kp.first == 0) return 0; ile=1; while(points.find(kk.second) == points.end()) { DEBUGC(cout << "cli : " << kk << "\n"); num = FirstNeighbor(kk.second); if(num == kk.first) { num = SecondNeighbor(kk.second); } kk.first = kk.second; kk.second = num; ile++; }; } } else { Kier kk,kp; I ile=0, komp; set<Kier> doZrob; DEBUG(cout << "serv DoZrob: "); for(int i=0;i<ile_points;i++) { I p = getPoint(i); Kier k1(p,FirstNeighbor(p)),k2(p,SecondNeighbor(p)); doZrob.insert(k1); doZrob.insert(k2); DEBUG(cout << k1 << " " << k2 << " "); } DEBUG(cout << "\n"); int runKomp = nodes - 1; while(runKomp) { komp = Receive(-1); kp.first = GetInt(komp); kp.second = GetInt(komp); ile = GetInt(komp); kk.first = GetInt(komp); kk.second = GetInt(komp); DEBUG(cout << "serv got: " << komp << " " << kp << " ile: " << ile << " " << kk << "\n"); if(kp.first) { doZrob.erase(kk); doZrob.erase(kp); wyniki.insert(make_pair(kp, make_pair(ile,kk))); wyniki.insert(make_pair(kk, make_pair(ile,kp))); } if(!doZrob.empty()){ kp = *doZrob.begin(); doZrob.erase(doZrob.begin()); } else { kp.second = kp.first = 0; runKomp--; DEBUG(cout << "serv runKomp: " << runKomp << "\n"); } DEBUG(cout << "serv sed: " << komp << " " << kp << "\n"); PutInt(komp, kp.first); PutInt(komp, kp.second); Send(komp); } for(int i=0;i<m;i++) { int ip,ik; ip = QueryFrom(i+1); ik = QueryTo(i+1); Kier k1 = make_pair(ip,FirstNeighbor(ip)); Kier k2 = make_pair(ip,SecondNeighbor(ip)); DEBUG(cout << "serv find: " << k1 << " last: " << ik << "\n"); I od1 = getOdl(k1, ik); DEBUG(cout << "serv find: " << k2 << " last: " << ik << "\n"); I od2 = getOdl(k2, ik); cout << min(od1,od2) << "\n"; } } return 0; } |