#include "kollib.h" #include "message.h" #include <cstdlib> #include <cstdio> #include <unordered_set> #include <vector> #include <utility> #include <algorithm> //#define deb(...) printf(__VA_ARGS__) #define deb(...) using namespace std; inline int rand_int() { const int hehe = (1 << 15) - 1; int a = rand(), b = rand(); return ((a & hehe) << 15) | (b & hehe); } inline int losuj(const int a, const int b) { return (rand_int() % (b - a + 1)) + a; } inline int get_nast(int w, int nie) { const int a = FirstNeighbor(w); return (a == nie) ? SecondNeighbor(w) : a; } int n, m; int ilu_nas, ilu_pracujacych; int ja; int ile_fajnych; int god; unordered_set<int> s_poczatki; vector<int> poczatki; unordered_set<int> wazne; void pracuj() { int w = poczatki[ja / 2]; int nie = (ja % 2) ? FirstNeighbor(w) : SecondNeighbor(w); vector<pair<int, int> > v; v.push_back(make_pair(w, 0)); int l = 0; do { l++; nie = get_nast(w, nie); swap(nie, w); if(wazne.find(w) != wazne.end()) { v.push_back(make_pair(w, l)); l = 0; } } while(s_poczatki.find(w) == s_poczatki.end()); if(v.back().first != w) v.push_back(make_pair(w, l)); PutInt(god, (int) v.size()); for(pair<int, int> p : v) { PutInt(god, p.first); PutInt(god, p.second); } Send(god); } void lacz() { vector<vector<pair<int, int> > > v; for(int i = 0; i < ilu_pracujacych; i++) { int kto = Receive(-1); deb("--------+ %d\n", kto); v.resize(v.size() + 1); vector<pair<int, int> > &g = v.back(); int ile = GetInt(kto); g.resize(ile); for(int i = 0; i < ile; i++) { g[i].first = GetInt(kto); g[i].second = GetInt(kto); deb("%d %d\n", g[i].first, g[i].second); } v.push_back(g); vector<pair<int, int> > &f = v.back(); reverse(f.begin(), f.end()); for(int i = f.size() - 1; i > 0; i--) f[i].second = f[i - 1].second; f[0].second = 0; } deb("wektory:\n"); for(vector<pair<int, int> > &we : v) { deb("|:"); for(pair<int, int> p : we) deb(" (%d, %d)", p.first, p.second); deb("\n"); } vector<pair<int, int> > kolko; for(const pair<int, int> &p : v[0]) kolko.push_back(p); int poc = kolko[0].first; int ost = kolko.back().first; int nie = kolko[kolko.size() - 2].first; while(poc != ost) { for(const vector<pair<int, int> > &we : v) { if(we[0].first == ost && we[1].first != nie) { for(vector<pair<int, int> >::const_iterator i = we.begin() + 1; i != we.end(); i++) kolko.push_back(*i); break; } } ost = kolko.back().first; nie = kolko[kolko.size() - 2].first; } deb("kolko:"); for(pair<int, int> p : kolko) deb(" (%d, %d)", p.first, p.second); deb("\n"); for(int i = 1; i <= m; i++) { int a = QueryFrom(i); int b = QueryTo(i); if(a == b) { printf("0\n"); continue; } int odl = 0; for(vector<pair<int, int> >::const_iterator j = kolko.begin(); j != kolko.end(); j++) { if((*j).first == b) swap(a, b); if((*j).first == a) { for(j++; j != kolko.end(); j++) { odl += (*j).second; if((*j).first == b) break; } break; } } printf("%d\n", min(odl, n - odl)); } } int main() { n = NumberOfStudents(); ja = MyNodeId(); ilu_nas = NumberOfNodes(); ilu_pracujacych = (min(ilu_nas, 2 * n - 4) / 2) * 2; god = ilu_nas - 1; ile_fajnych = ilu_pracujacych / 2 + 1; srand(~ n - n * n ^ n & n + n | n); deb("n = %d, ja = %d, ilu_nas = %d, ilu_pracujacych = %d, god = %d, ile_fajnych = %d\n", n, ja, ilu_nas, ilu_pracujacych, god, ile_fajnych); // generuje poczatki while(s_poczatki.size() != ile_fajnych) s_poczatki.insert(losuj(1, n)); for(int poc : s_poczatki) poczatki.push_back(poc); deb("poczatki: "); for(int poc : s_poczatki) deb("%d ", poc); deb("\n"); m = NumberOfQueries(); for(int i = 1; i <= m; i++) { wazne.insert(QueryFrom(i)); wazne.insert(QueryTo(i)); } if(ja < ilu_pracujacych) pracuj(); if(ja == god) lacz(); 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 | #include "kollib.h" #include "message.h" #include <cstdlib> #include <cstdio> #include <unordered_set> #include <vector> #include <utility> #include <algorithm> //#define deb(...) printf(__VA_ARGS__) #define deb(...) using namespace std; inline int rand_int() { const int hehe = (1 << 15) - 1; int a = rand(), b = rand(); return ((a & hehe) << 15) | (b & hehe); } inline int losuj(const int a, const int b) { return (rand_int() % (b - a + 1)) + a; } inline int get_nast(int w, int nie) { const int a = FirstNeighbor(w); return (a == nie) ? SecondNeighbor(w) : a; } int n, m; int ilu_nas, ilu_pracujacych; int ja; int ile_fajnych; int god; unordered_set<int> s_poczatki; vector<int> poczatki; unordered_set<int> wazne; void pracuj() { int w = poczatki[ja / 2]; int nie = (ja % 2) ? FirstNeighbor(w) : SecondNeighbor(w); vector<pair<int, int> > v; v.push_back(make_pair(w, 0)); int l = 0; do { l++; nie = get_nast(w, nie); swap(nie, w); if(wazne.find(w) != wazne.end()) { v.push_back(make_pair(w, l)); l = 0; } } while(s_poczatki.find(w) == s_poczatki.end()); if(v.back().first != w) v.push_back(make_pair(w, l)); PutInt(god, (int) v.size()); for(pair<int, int> p : v) { PutInt(god, p.first); PutInt(god, p.second); } Send(god); } void lacz() { vector<vector<pair<int, int> > > v; for(int i = 0; i < ilu_pracujacych; i++) { int kto = Receive(-1); deb("--------+ %d\n", kto); v.resize(v.size() + 1); vector<pair<int, int> > &g = v.back(); int ile = GetInt(kto); g.resize(ile); for(int i = 0; i < ile; i++) { g[i].first = GetInt(kto); g[i].second = GetInt(kto); deb("%d %d\n", g[i].first, g[i].second); } v.push_back(g); vector<pair<int, int> > &f = v.back(); reverse(f.begin(), f.end()); for(int i = f.size() - 1; i > 0; i--) f[i].second = f[i - 1].second; f[0].second = 0; } deb("wektory:\n"); for(vector<pair<int, int> > &we : v) { deb("|:"); for(pair<int, int> p : we) deb(" (%d, %d)", p.first, p.second); deb("\n"); } vector<pair<int, int> > kolko; for(const pair<int, int> &p : v[0]) kolko.push_back(p); int poc = kolko[0].first; int ost = kolko.back().first; int nie = kolko[kolko.size() - 2].first; while(poc != ost) { for(const vector<pair<int, int> > &we : v) { if(we[0].first == ost && we[1].first != nie) { for(vector<pair<int, int> >::const_iterator i = we.begin() + 1; i != we.end(); i++) kolko.push_back(*i); break; } } ost = kolko.back().first; nie = kolko[kolko.size() - 2].first; } deb("kolko:"); for(pair<int, int> p : kolko) deb(" (%d, %d)", p.first, p.second); deb("\n"); for(int i = 1; i <= m; i++) { int a = QueryFrom(i); int b = QueryTo(i); if(a == b) { printf("0\n"); continue; } int odl = 0; for(vector<pair<int, int> >::const_iterator j = kolko.begin(); j != kolko.end(); j++) { if((*j).first == b) swap(a, b); if((*j).first == a) { for(j++; j != kolko.end(); j++) { odl += (*j).second; if((*j).first == b) break; } break; } } printf("%d\n", min(odl, n - odl)); } } int main() { n = NumberOfStudents(); ja = MyNodeId(); ilu_nas = NumberOfNodes(); ilu_pracujacych = (min(ilu_nas, 2 * n - 4) / 2) * 2; god = ilu_nas - 1; ile_fajnych = ilu_pracujacych / 2 + 1; srand(~ n - n * n ^ n & n + n | n); deb("n = %d, ja = %d, ilu_nas = %d, ilu_pracujacych = %d, god = %d, ile_fajnych = %d\n", n, ja, ilu_nas, ilu_pracujacych, god, ile_fajnych); // generuje poczatki while(s_poczatki.size() != ile_fajnych) s_poczatki.insert(losuj(1, n)); for(int poc : s_poczatki) poczatki.push_back(poc); deb("poczatki: "); for(int poc : s_poczatki) deb("%d ", poc); deb("\n"); m = NumberOfQueries(); for(int i = 1; i <= m; i++) { wazne.insert(QueryFrom(i)); wazne.insert(QueryTo(i)); } if(ja < ilu_pracujacych) pracuj(); if(ja == god) lacz(); return 0; } |