#include "kollib.h"
#include "message.h"
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <cmath>
using namespace std;
typedef vector<int> VI;
typedef long long LL;
typedef pair<int, int> pii;
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define MP(x,y) make_pair(x,y)
#define ALL(x) (x).begin(), (x).end()
#define FT first
#define ST second
#define FOREACH(i, c) for(typeof((c.begin())) i = (c).begin() ; i != (c).end(); ++i)
#define PB(x) push_back((x))
#define SZ(x) (int)x.size()
#define LN(x) (int)(x).length()
void algo(int testId, VI& res) {
int dist = 0;
int from = QueryFrom(testId + 1);
int to = QueryTo(testId + 1);
int cur0 = from, cur1 = from;
int prev0 = from, prev1 = from;
while(cur0 != to && cur1 != to) {
++dist;
int fs0 = FirstNeighbor(cur0);
int se0 = SecondNeighbor(cur0);
int tmp0 = (fs0 != prev0) ? fs0 : se0;
prev0 = cur0; cur0 = tmp0;
int fs1 = FirstNeighbor(cur1);
int se1 = SecondNeighbor(cur1);
int tmp1 = (se1 != prev1) ? se1 : fs1;
prev1 = cur1; cur1 = tmp1;
}
if(MyNodeId() > 0) PutInt(0, dist);
else res[testId] = dist;
}
int main() {
ios_base::sync_with_stdio(false);
int id = MyNodeId();
int nn = NumberOfNodes();
int ns = NumberOfStudents();
int m = NumberOfQueries();
int perNode = m / nn;
VI results(m,-1);
REP(i, perNode)
algo(id * perNode + i, results);
if(id > 0) Send(0);
if(id == 0) {
int res = m - perNode * nn;
REP(i, res)
algo(perNode * nn + i, results);
//barrier
REP(i, nn - 1) {
int fN = Receive(i + 1);
REP(j, perNode) {
int dist = GetInt(fN);
results[j + fN * perNode] = dist;
}
}
REP(i, SZ(results)) cout << results[i] << "\n";
}
}
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 | #include "kollib.h" #include "message.h" #include <iostream> #include <algorithm> #include <string> #include <vector> #include <map> #include <cmath> using namespace std; typedef vector<int> VI; typedef long long LL; typedef pair<int, int> pii; #define REP(x, n) for(int x = 0; x < (n); ++x) #define MP(x,y) make_pair(x,y) #define ALL(x) (x).begin(), (x).end() #define FT first #define ST second #define FOREACH(i, c) for(typeof((c.begin())) i = (c).begin() ; i != (c).end(); ++i) #define PB(x) push_back((x)) #define SZ(x) (int)x.size() #define LN(x) (int)(x).length() void algo(int testId, VI& res) { int dist = 0; int from = QueryFrom(testId + 1); int to = QueryTo(testId + 1); int cur0 = from, cur1 = from; int prev0 = from, prev1 = from; while(cur0 != to && cur1 != to) { ++dist; int fs0 = FirstNeighbor(cur0); int se0 = SecondNeighbor(cur0); int tmp0 = (fs0 != prev0) ? fs0 : se0; prev0 = cur0; cur0 = tmp0; int fs1 = FirstNeighbor(cur1); int se1 = SecondNeighbor(cur1); int tmp1 = (se1 != prev1) ? se1 : fs1; prev1 = cur1; cur1 = tmp1; } if(MyNodeId() > 0) PutInt(0, dist); else res[testId] = dist; } int main() { ios_base::sync_with_stdio(false); int id = MyNodeId(); int nn = NumberOfNodes(); int ns = NumberOfStudents(); int m = NumberOfQueries(); int perNode = m / nn; VI results(m,-1); REP(i, perNode) algo(id * perNode + i, results); if(id > 0) Send(0); if(id == 0) { int res = m - perNode * nn; REP(i, res) algo(perNode * nn + i, results); //barrier REP(i, nn - 1) { int fN = Receive(i + 1); REP(j, perNode) { int dist = GetInt(fN); results[j + fN * perNode] = dist; } } REP(i, SZ(results)) cout << results[i] << "\n"; } } |
English