#include "kollib.h" #include "message.h" #include <cstdio> #include <algorithm> using namespace std; int n, m, k; int solve(int no) { int b = QueryFrom(no); int e = QueryTo(no); int l = 0; int po = 0; while (b != e) { int nx = (SecondNeighbor(b) == po ? FirstNeighbor(b) : SecondNeighbor(b)); po = b; b = nx, l++; } return min(l, n-l); } int out[1000]; int main() { n = NumberOfStudents(); m = NumberOfQueries(); k = NumberOfNodes()-1; if (MyNodeId() > 0) { vector<pair<int, int> > Q; for (int i = MyNodeId()-1; i <= m; i += k) if (i > 0) { Q.push_back(make_pair(i, solve(i))); } PutInt(0, Q.size()); Send(0); for (int i = 0; i < Q.size(); i++) { PutInt(0, Q[i].first); Send(0); PutInt(0, Q[i].second); Send(0); } } else { for (int i = 1; i < NumberOfNodes(); i++) { int ile = 0; Receive(i); ile = GetInt(i); for (int j = 0; j < ile; j++) { Receive(i); int A = GetInt(i); Receive(i); int B = GetInt(i); out[A] = B; } } for (int i = 1; i <= m; i++) printf("%d\n", out[i]); } 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 | #include "kollib.h" #include "message.h" #include <cstdio> #include <algorithm> using namespace std; int n, m, k; int solve(int no) { int b = QueryFrom(no); int e = QueryTo(no); int l = 0; int po = 0; while (b != e) { int nx = (SecondNeighbor(b) == po ? FirstNeighbor(b) : SecondNeighbor(b)); po = b; b = nx, l++; } return min(l, n-l); } int out[1000]; int main() { n = NumberOfStudents(); m = NumberOfQueries(); k = NumberOfNodes()-1; if (MyNodeId() > 0) { vector<pair<int, int> > Q; for (int i = MyNodeId()-1; i <= m; i += k) if (i > 0) { Q.push_back(make_pair(i, solve(i))); } PutInt(0, Q.size()); Send(0); for (int i = 0; i < Q.size(); i++) { PutInt(0, Q[i].first); Send(0); PutInt(0, Q[i].second); Send(0); } } else { for (int i = 1; i < NumberOfNodes(); i++) { int ile = 0; Receive(i); ile = GetInt(i); for (int j = 0; j < ile; j++) { Receive(i); int A = GetInt(i); Receive(i); int B = GetInt(i); out[A] = B; } } for (int i = 1; i <= m; i++) printf("%d\n", out[i]); } return 0; } |