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
/*
 * main.cpp
 *
 *  Created on: 15-05-2014
 *      Author: adasdeb
 */

#include <iostream>
#include "kollib.h"
#include "message.h"

struct qrange
{
	int fq;
	int lq;

	qrange(int comn, int comi, int quen)
	{
		int quePerCom = quen/comn;
		if(quePerCom == 0)
			quePerCom = 1;
		if((comi+1) <= quen)
		{
			fq = comi*quePerCom + 1;
			lq = (comi == (comn - 1) ? quen : (comi + 1)*quePerCom);
		}
		else
		{
			fq = 1;
			lq = 0;
		}
	}
};

struct inf
{
	int n;
	int ln;
	int rn;

	inf(int n)
		:n(n), ln(FirstNeighbor(n)), rn(SecondNeighbor(n)) {}
};

int solveq(int fr, int to);
void solve(qrange range);

int main(void)
{
	qrange range(NumberOfNodes(), MyNodeId(), NumberOfQueries());

	solve(range);

	if(MyNodeId() == 0)
	{
		int comn = 	NumberOfNodes(), quen = NumberOfQueries();

		for(int i = 0; i < comn; i++)
		{
			qrange range = qrange(comn, i, quen);
			Receive(i);
			for(int cq = range.fq; cq <= range.lq; cq++)
				std::cout << GetInt(i) << "\n";
		}
	}

	return 0;
}

int solveq(int fr, int to)
{
	int time = 1;
	if(fr == to)
		return 0;

	inf inf1p(fr),
		inf2p(fr);
	inf inf1(FirstNeighbor(fr)),
		inf2(SecondNeighbor(fr));

	while((inf1.n != to) && (inf2.n != to))
	{
		if(inf1.ln == inf1p.n)
		{
			inf1p = inf1;
			inf1 = inf(inf1.rn);
		}
		else
		{
			inf1p = inf1;
			inf1 = inf(inf1.ln);
		}
		if(inf2.ln == inf2p.n)
		{
			inf2p = inf2;
			inf2 = inf(inf2.rn);
		}
		else
		{
			inf2p = inf2;
			inf2 = inf(inf2.ln);
		}
		++time;
	}

	return time;
}

void solve(qrange range)
{
	for(int cq = range.fq; cq <= range.lq; cq++)
		PutInt(0,solveq(QueryFrom(cq), QueryTo(cq)));
	Send(0);
	return;
}