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
#include <cstdio>
#include <vector>
#include <algorithm>

#include "kollib.h"
#include "message.h"

#define MAXM 203

typedef std::pair<int,int> PI;

int id;
int n,m,p;
int D[MAXM];
std::vector<PI> R;

int main()
{
	id = MyNodeId();
	n = NumberOfStudents();
	m = NumberOfQueries();
	p = NumberOfNodes();
	
	
	for(int q = id+1; q <= m; q+=p)
	{
		int from = QueryFrom(q);
		int to = QueryTo(q);
		if(from == to)
		{
			R.push_back({q,0});
			//printf("%d %d\n",q,0);
			continue;
		}
		int d = 1;
		int v0 = from,u0 = from;
		int v = FirstNeighbor(v0);
		int u = SecondNeighbor(u0);
		while(v != to && u != to)
		{
			++d;
			v0 = FirstNeighbor(v) == v0 ? SecondNeighbor(v) : FirstNeighbor(v);
			std::swap(v,v0);
			u0 = FirstNeighbor(u) == u0 ? SecondNeighbor(u) : FirstNeighbor(u);
			std::swap(u,u0);
		}
		R.push_back({q,d});
		//printf("%d %d\n",q,d);
	}
	
	if(id != 0)
	{
		PutInt(0,(int)R.size());
		for(auto& pi : R) PutInt(0,pi.first),PutInt(0,pi.second);
		Send(0);
	}
	else
	{
		int count = m;
		for(auto& pi : R) D[pi.first] = pi.second,--count;
		while(count)
		{
			int src = Receive(-1);
			int rsize = GetInt(src);
			count -= rsize;
			while(rsize--)
			{
				int q = GetInt(src);
				D[q] = GetInt(src);
			}
		}
		for(int i = 1; i <= m; ++i) printf("%d\n",D[i]);
	}
	
	return 0;
}