#include <cstdio> #include <algorithm> #include "kollib.h" #include "message.h" using namespace std; #define FOR(v,p,k) for(int v=p;v<=k;++v) #define FORD(v,p,k) for(int v=p;v>=k;--v) #define REP(i,n) for(int i=0;i<(n);++i) #define ALL(c) (c).begin(),(c).end() #define ZERO(x) memset(x,0,sizeof(x)) typedef long long LL; typedef unsigned long long ULL; const int INF = 1000000000; const int MAXM = 200; pair<int, pair<int, int>> Q[MAXM]; int S[MAXM]; int main(int argc, char **args) { int N = NumberOfStudents(); int numberOfNodes = NumberOfNodes(); int myNodeId = MyNodeId(); int qCnt = 0; int queries = NumberOfQueries(); REP(i, queries) { int from = QueryFrom(i + 1); int to = QueryTo(i + 1); Q[qCnt++] = make_pair(min(from, to), make_pair(max(from, to), i)); } sort(Q, Q + qCnt); if (myNodeId > 0) { while (true) { Receive(0); int q = GetInt(0); if (q == -1) break; int from = Q[q].first; int to = Q[q].second.first; int res = 0; int prev = from; int x = from; while (x != to) { int fn = FirstNeighbor(x); if (fn != prev) { prev = x; x = fn; } else { prev = x; x = SecondNeighbor(x); } res++; } PutInt(0, q); PutInt(0, res); Send(0); } } else { int sCnt = 0; int counter = 0; FOR(i, 1, numberOfNodes - 1) { if (sCnt < qCnt) { if (sCnt > 0 && Q[sCnt].first == Q[sCnt - 1].first && Q[sCnt].second.first == Q[sCnt - 1].second.first) sCnt++; else { PutInt(i, sCnt); Send(i); sCnt++; counter++; } } } while (counter) { int node = Receive(-1); int s = GetInt(node); int v = GetInt(node); counter--; S[Q[s].second.second] = min(v, N - v); while (s < qCnt - 1 && Q[s].first == Q[s + 1].first && Q[s].second.first == Q[s + 1].second.first) { S[Q[s + 1].second.second] = S[Q[s].second.second]; s++; } if (sCnt < qCnt) { PutInt(node, sCnt); Send(node); sCnt++; counter++; } } FOR(i, 1, numberOfNodes - 1) { PutInt(i, -1); Send(i); } REP(i, qCnt) printf("%d\n", S[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 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 | #include <cstdio> #include <algorithm> #include "kollib.h" #include "message.h" using namespace std; #define FOR(v,p,k) for(int v=p;v<=k;++v) #define FORD(v,p,k) for(int v=p;v>=k;--v) #define REP(i,n) for(int i=0;i<(n);++i) #define ALL(c) (c).begin(),(c).end() #define ZERO(x) memset(x,0,sizeof(x)) typedef long long LL; typedef unsigned long long ULL; const int INF = 1000000000; const int MAXM = 200; pair<int, pair<int, int>> Q[MAXM]; int S[MAXM]; int main(int argc, char **args) { int N = NumberOfStudents(); int numberOfNodes = NumberOfNodes(); int myNodeId = MyNodeId(); int qCnt = 0; int queries = NumberOfQueries(); REP(i, queries) { int from = QueryFrom(i + 1); int to = QueryTo(i + 1); Q[qCnt++] = make_pair(min(from, to), make_pair(max(from, to), i)); } sort(Q, Q + qCnt); if (myNodeId > 0) { while (true) { Receive(0); int q = GetInt(0); if (q == -1) break; int from = Q[q].first; int to = Q[q].second.first; int res = 0; int prev = from; int x = from; while (x != to) { int fn = FirstNeighbor(x); if (fn != prev) { prev = x; x = fn; } else { prev = x; x = SecondNeighbor(x); } res++; } PutInt(0, q); PutInt(0, res); Send(0); } } else { int sCnt = 0; int counter = 0; FOR(i, 1, numberOfNodes - 1) { if (sCnt < qCnt) { if (sCnt > 0 && Q[sCnt].first == Q[sCnt - 1].first && Q[sCnt].second.first == Q[sCnt - 1].second.first) sCnt++; else { PutInt(i, sCnt); Send(i); sCnt++; counter++; } } } while (counter) { int node = Receive(-1); int s = GetInt(node); int v = GetInt(node); counter--; S[Q[s].second.second] = min(v, N - v); while (s < qCnt - 1 && Q[s].first == Q[s + 1].first && Q[s].second.first == Q[s + 1].second.first) { S[Q[s + 1].second.second] = S[Q[s].second.second]; s++; } if (sCnt < qCnt) { PutInt(node, sCnt); Send(node); sCnt++; counter++; } } FOR(i, 1, numberOfNodes - 1) { PutInt(i, -1); Send(i); } REP(i, qCnt) printf("%d\n", S[i]); } return 0; } |