#include <vector> #include <iostream> #include <algorithm> #include <tuple> #include <set> #include <map> #include <queue> #include "kollib.h" #include "message.h" using namespace std; #define pb push_back #define mt make_tuple #define st first #define nd second typedef long long ll; typedef pair<int,int> pii; /* int Receive(int source){return 0;}; int GetInt(int source){return 0;}; void PutInt(int target, int value){;} int NumberOfNodes(){return 0;} int MyNodeId(){return 0;} void Send(int target){} int NumberOfStudents(){return 0;} int FirstNeighbor(int i){return 0;} int SecondNeighbor(int i){return 0;} int NumberOfQueries(){return 0;} int QueryFrom(int i){return 0;} int QueryTo(int i){return 0;} */ #define poka(x) cerr << #x << " = " << x << endl; #define ID 0 int main(){ int mni = MyNodeId(); int non = NumberOfNodes(); int nos = NumberOfStudents(); srand(50); vector<int> vpocz(non); set<int> spocz; // cerr << "A"; for(int i = 0; i < non; ++i){ ll x = 0; ll b = 1; while(x < ll(nos)){ x *= b; x += ll(rand()); b *= ll(RAND_MAX) + 1; } x = x % ll(nos) + 1; vpocz[i] = x; if(i == mni and spocz.count(x)) return 0; spocz.insert(x); } spocz.erase(vpocz[mni]); //if(mni == ID){ poka(mni); for(int i = 0; i < vpocz.size(); ++i) cerr << vpocz[i] << " "; cerr << endl; } vector<int> lewo; lewo.pb(vpocz[mni]); // cerr << "B"; while(!spocz.count(lewo.back()) and lewo.size() < nos){ // poka(lewo.back()); int fn = FirstNeighbor(lewo.back()); int sn = SecondNeighbor(lewo.back()); if(lewo.size() == 1) {lewo.pb(min(sn,fn)); continue;} if(sn == lewo[lewo.size()-2]) lewo.pb(fn); else lewo.pb(sn); } // if(mni == ID){ poka(mni); poka("lewo"); for(int i = 0; i < lewo.size(); ++i) cerr << lewo[i] << " "; cerr << endl; } // poka("Z"); vector<int> prawo; prawo.pb(vpocz[mni]); while(!spocz.count(prawo.back()) and prawo.size() < nos){ int fn = FirstNeighbor(prawo.back()); int sn = SecondNeighbor(prawo.back()); if(prawo.size() == 1) {prawo.pb(max(sn,fn)); continue;} if(sn == prawo[prawo.size()-2]) prawo.pb(fn); else prawo.pb(sn); } // cerr << "C"; int noq = NumberOfQueries(); set<int> se; vector<int> from(noq+1), to(noq+1); for(int i = 1; i <= noq; ++i){ from[i] = QueryFrom(i); to[i] = QueryTo(i); se.insert(from[i]); se.insert(to[i]); } vector<pii> znamlewo; for(int i = 0; i < lewo.size(); ++i){ if(se.count(lewo[i])) znamlewo.pb(pii(lewo[i], i)); } vector<pii> znamprawo; for(int i = 0; i < prawo.size(); ++i){ if(se.count(prawo[i])) znamprawo.pb(pii(prawo[i], i)); } PutInt(0, *lewo.begin()); PutInt(0, prawo.back()); PutInt(0, znamlewo.size()+1); for(int i = 0; i < znamlewo.size(); ++i){ PutInt(0, znamlewo[i].st); PutInt(0, znamlewo[i].nd); } PutInt(0, lewo.back()); PutInt(0, lewo.size()-1); if(mni == 0){ PutInt(0, *prawo.begin()); PutInt(0, prawo.back()); PutInt(0, 0); } else{ PutInt(0, *prawo.begin()); PutInt(0, prawo.back()); PutInt(0, znamprawo.size()+1); for(int i = 0; i < znamprawo.size(); ++i){ PutInt(0, znamprawo[i].st); PutInt(0, znamprawo[i].nd); } PutInt(0, prawo.back()); PutInt(0, prawo.size()-1); } Send(0); // if(mni == 0){ poka(mni); poka("prawo"); for(int i = 0; i < prawo.size(); ++i) cerr << prawo[i] << " "; cerr << endl; } if(mni) return 0; map<int,vector<pii> > g; for(int i = 0; i < spocz.size()+1; ++i){ int ins = Receive(-1); // poka(ins); for(int iii = 2; iii--;){ int a = GetInt(ins); int b = GetInt(ins); int n = GetInt(ins); // poka(a); poka(b); poka(n); for(int k = 0; k < n; ++k){ int v = GetInt(ins); int d = GetInt(ins); g[a].push_back(pii(v,d)); // poka("nast");poka(a); poka(v); poka(d); } } } const int inf = 1000*1000*1000 + 9; map<int,int> d; for(auto it1 = g.begin(); it1 != g.end(); ++it1){ int skad = it1->first; d[skad] = inf; for(auto it2 = (it1->second).begin(); it2 != (it1->second).end(); ++it2){ int dokad = it2->first; d[dokad] = inf; } } queue<int> q; set<int> byl; int v0 = vpocz[0]; /* for(int i = 0; i < g[v0].size(); ++i){ int x = g[v0][i].st; int o = g[v0][i].nd; cerr << "xxx" << " "<< v0 << "->" << x << " " << o << endl; } */ d[v0] = 0; q.push(v0); while(!q.empty()){ int v = q.front(); q.pop(); if(byl.count(v)) continue; byl.insert(v); for(int i = 0; i < g[v].size(); ++i){ int x = g[v][i].st; int o = g[v][i].nd; if(d[x] > d[v] + o) d[x] = d[v] + o; // cerr << "" << v << "->" << x << " " << o << endl; q.push(x); //poka(x); } } for(int i = 1; i <= noq; ++i){ int f = d[from[i]]; int t = d[to[i]]; cout << min(abs(f-t),nos - abs(f-t)) << "\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 198 199 200 201 | #include <vector> #include <iostream> #include <algorithm> #include <tuple> #include <set> #include <map> #include <queue> #include "kollib.h" #include "message.h" using namespace std; #define pb push_back #define mt make_tuple #define st first #define nd second typedef long long ll; typedef pair<int,int> pii; /* int Receive(int source){return 0;}; int GetInt(int source){return 0;}; void PutInt(int target, int value){;} int NumberOfNodes(){return 0;} int MyNodeId(){return 0;} void Send(int target){} int NumberOfStudents(){return 0;} int FirstNeighbor(int i){return 0;} int SecondNeighbor(int i){return 0;} int NumberOfQueries(){return 0;} int QueryFrom(int i){return 0;} int QueryTo(int i){return 0;} */ #define poka(x) cerr << #x << " = " << x << endl; #define ID 0 int main(){ int mni = MyNodeId(); int non = NumberOfNodes(); int nos = NumberOfStudents(); srand(50); vector<int> vpocz(non); set<int> spocz; // cerr << "A"; for(int i = 0; i < non; ++i){ ll x = 0; ll b = 1; while(x < ll(nos)){ x *= b; x += ll(rand()); b *= ll(RAND_MAX) + 1; } x = x % ll(nos) + 1; vpocz[i] = x; if(i == mni and spocz.count(x)) return 0; spocz.insert(x); } spocz.erase(vpocz[mni]); //if(mni == ID){ poka(mni); for(int i = 0; i < vpocz.size(); ++i) cerr << vpocz[i] << " "; cerr << endl; } vector<int> lewo; lewo.pb(vpocz[mni]); // cerr << "B"; while(!spocz.count(lewo.back()) and lewo.size() < nos){ // poka(lewo.back()); int fn = FirstNeighbor(lewo.back()); int sn = SecondNeighbor(lewo.back()); if(lewo.size() == 1) {lewo.pb(min(sn,fn)); continue;} if(sn == lewo[lewo.size()-2]) lewo.pb(fn); else lewo.pb(sn); } // if(mni == ID){ poka(mni); poka("lewo"); for(int i = 0; i < lewo.size(); ++i) cerr << lewo[i] << " "; cerr << endl; } // poka("Z"); vector<int> prawo; prawo.pb(vpocz[mni]); while(!spocz.count(prawo.back()) and prawo.size() < nos){ int fn = FirstNeighbor(prawo.back()); int sn = SecondNeighbor(prawo.back()); if(prawo.size() == 1) {prawo.pb(max(sn,fn)); continue;} if(sn == prawo[prawo.size()-2]) prawo.pb(fn); else prawo.pb(sn); } // cerr << "C"; int noq = NumberOfQueries(); set<int> se; vector<int> from(noq+1), to(noq+1); for(int i = 1; i <= noq; ++i){ from[i] = QueryFrom(i); to[i] = QueryTo(i); se.insert(from[i]); se.insert(to[i]); } vector<pii> znamlewo; for(int i = 0; i < lewo.size(); ++i){ if(se.count(lewo[i])) znamlewo.pb(pii(lewo[i], i)); } vector<pii> znamprawo; for(int i = 0; i < prawo.size(); ++i){ if(se.count(prawo[i])) znamprawo.pb(pii(prawo[i], i)); } PutInt(0, *lewo.begin()); PutInt(0, prawo.back()); PutInt(0, znamlewo.size()+1); for(int i = 0; i < znamlewo.size(); ++i){ PutInt(0, znamlewo[i].st); PutInt(0, znamlewo[i].nd); } PutInt(0, lewo.back()); PutInt(0, lewo.size()-1); if(mni == 0){ PutInt(0, *prawo.begin()); PutInt(0, prawo.back()); PutInt(0, 0); } else{ PutInt(0, *prawo.begin()); PutInt(0, prawo.back()); PutInt(0, znamprawo.size()+1); for(int i = 0; i < znamprawo.size(); ++i){ PutInt(0, znamprawo[i].st); PutInt(0, znamprawo[i].nd); } PutInt(0, prawo.back()); PutInt(0, prawo.size()-1); } Send(0); // if(mni == 0){ poka(mni); poka("prawo"); for(int i = 0; i < prawo.size(); ++i) cerr << prawo[i] << " "; cerr << endl; } if(mni) return 0; map<int,vector<pii> > g; for(int i = 0; i < spocz.size()+1; ++i){ int ins = Receive(-1); // poka(ins); for(int iii = 2; iii--;){ int a = GetInt(ins); int b = GetInt(ins); int n = GetInt(ins); // poka(a); poka(b); poka(n); for(int k = 0; k < n; ++k){ int v = GetInt(ins); int d = GetInt(ins); g[a].push_back(pii(v,d)); // poka("nast");poka(a); poka(v); poka(d); } } } const int inf = 1000*1000*1000 + 9; map<int,int> d; for(auto it1 = g.begin(); it1 != g.end(); ++it1){ int skad = it1->first; d[skad] = inf; for(auto it2 = (it1->second).begin(); it2 != (it1->second).end(); ++it2){ int dokad = it2->first; d[dokad] = inf; } } queue<int> q; set<int> byl; int v0 = vpocz[0]; /* for(int i = 0; i < g[v0].size(); ++i){ int x = g[v0][i].st; int o = g[v0][i].nd; cerr << "xxx" << " "<< v0 << "->" << x << " " << o << endl; } */ d[v0] = 0; q.push(v0); while(!q.empty()){ int v = q.front(); q.pop(); if(byl.count(v)) continue; byl.insert(v); for(int i = 0; i < g[v].size(); ++i){ int x = g[v][i].st; int o = g[v][i].nd; if(d[x] > d[v] + o) d[x] = d[v] + o; // cerr << "" << v << "->" << x << " " << o << endl; q.push(x); //poka(x); } } for(int i = 1; i <= noq; ++i){ int f = d[from[i]]; int t = d[to[i]]; cout << min(abs(f-t),nos - abs(f-t)) << "\n"; } return 0; } |