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
#include "kollib.h"
#include "message.h"
#include <vector>
#include <iostream>
#include <set>
using namespace std;

int calculatePath(int from, int to, int number, set<int> path){
	path.insert(from);
	if (from == to) return number;
	
	int firstN = FirstNeighbor(from);
	int secondN = SecondNeighbor(from);	

	if (path.find(firstN)==path.end()){
		return calculatePath(firstN, to, number+1, path);
	}else {
		return calculatePath(secondN, to, number+1, path);
	}
}

int main() {
	set<int> path1;
	set<int> path2;
	int incr;
	if (NumberOfQueries() % NumberOfNodes()==0) incr = NumberOfQueries() / NumberOfNodes();
	else incr = NumberOfQueries() / NumberOfNodes() + 1; 

	for (int i = MyNodeId()*incr+1; i <= MyNodeId()*incr+incr; i++) {
		if (i>NumberOfQueries()) break;
		int from = QueryFrom(i);
		int to = QueryTo(i);

		int minutes = 0;
		if (from != to){
			path1.insert(from);
			path2.insert(from);
			int x = calculatePath(FirstNeighbor(from), to, 1, path1);		
			int y = calculatePath(SecondNeighbor(from), to, 1, path2);
			minutes = min(x, y);
		}
		PutInt(0, minutes);
		path1.erase(path1.begin(), path1.end());
		path2.erase(path2.begin(), path2.end());
	}
	Send(0);
		
	if (MyNodeId() == 0){
		vector<vector<int> > odp(NumberOfNodes());
		int licznik = 1;
		while(true){
			int instancja = Receive(-1);
			for (int i=0 ; i<incr ; i++){
				int nr = GetInt(instancja);
				odp[instancja].push_back(nr);
				licznik++;
			}
			if (licznik == NumberOfQueries()) break;	
		}		
		licznik = 1;
		for (int k=0 ; k<odp.size() ; k++){
			vector<int> v = odp[k];
			for (int p = 0 ; p < v.size() ; p++){
				cout<<v[p]<<endl;
				licznik++;
				if (licznik == NumberOfQueries()) break;
			}
		}
	}
	return 0;
}