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
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include "message.h"
#include "kollib.h"

enum Response{ process, shuffle, stop };

struct StudentsPair
{
	StudentsPair()
		: stud1(-1), stud2(-1){}

	StudentsPair(long stud1, long stud2)
		: stud1(stud1), stud2(stud2){}

	long stud1;
	long stud2;
};

int main()
{
	long myNode = MyNodeId();
	long queriesNum = NumberOfQueries();

	if(queriesNum == 0 || myNode != 0)
		return 0;

	std::unordered_map<long, long> studConnMap;
	std::vector<StudentsPair> studentPairs;
	studentPairs.reserve(queriesNum);

	for(long i = 0; i < queriesNum; ++i)
	{
		auto s = StudentsPair(QueryFrom(i), QueryTo(i));
		studentPairs.push_back(s);
		studConnMap[s.stud1];
		studConnMap[s.stud2];
	}

	long totToCheck = studConnMap.size();
	
	long tot = NumberOfStudents();
	long currStudent = 1;
	long prevStudent = currStudent;
	long n;
	auto end = studConnMap.end();
	auto f = studConnMap.find(currStudent);
	if( f != end)
	{
		(*f).second = 0;
		--totToCheck;
	}

	for(long i = 1; i < queriesNum && totToCheck > 0; ++i)
	{
		n = FirstNeighbor(currStudent);
		if(n == prevStudent)
			n = SecondNeighbor(currStudent);
		
		prevStudent = currStudent;
		currStudent = n;

		f = studConnMap.find(currStudent);
		if(f != end)
		{
			(*f).second = i;
			--totToCheck;
		}		
	}

	for(auto& pair : studentPairs)
	{
		long pos1 = (*studConnMap.find(pair.stud1)).second;
		long pos2 = (*studConnMap.find(pair.stud2)).second;

		printf("%ld\n", std::max(pos1, pos2) - std::min(pos1, pos2));
	}
}