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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include "message.h"
#include "kollib.h"
using namespace std;


/***************Deklaracje dla kazdego Node'a********************/
const int MAXVPN = 1000;		//Vertex Per Node - ilosc wierzcholkow do przeszukania na kazdy

struct NEIGHBOR
{
	int id;
	int first_id, first_dist;
	int second_id, second_dist;
};

NEIGHBOR Neighbor[MAXVPN];
int NodeNum, VertNum, QueriesNum, NodeId;
set<int> POI;				//Point Of Interest - zbior wierzcholkow, ktore nas interesuja
vector<int> POIv;			//Point Of Interest vector - wektor wierzcholkow POI

void SearchForNext(int, int, NEIGHBOR&);
/***************Deklaracje dla Node0*****************************/



int main()
{
	NodeNum = NumberOfNodes();
	NodeId = MyNodeId();
	VertNum = NumberOfStudents();
	QueriesNum = NumberOfQueries();
	int VPN;
	if(VertNum < 1E6) VPN = 100;
	else VPN = MAXVPN;
	
	//Wstawianie szukanych wierzcholkow do zbioru
	for(int i=1; i<=QueriesNum; i++)
	{
		POI.insert(QueryFrom(i));
		POIv.push_back(QueryFrom(i));
		POI.insert(QueryTo(i));
		POIv.push_back(QueryTo(i));
	}
	//Uzupelnianie zbioru do NodeNum*VPN wierzcholkow
	srand(NodeNum+VertNum+18);
	while(POIv.size() < NodeNum * VPN)
	{
		int next = rand()%VertNum + 1;
		POI.insert(next);
		POIv.push_back(next);
	}
	//Przeszukiwanie grafu rozpoczynajac od wierzcholkow przypisanych 
	//danemu procesorowi wedlug num = NodeID + i*NodeNum, gdzie i=<0, VPN>
	for(int i=0, vert_id; i<VPN; i++)
	{
		vert_id = POIv[NodeId + i*NodeNum];
		Neighbor[i].id = vert_id;
		
		SearchForNext(vert_id, FirstNeighbor(vert_id), Neighbor[i]);
		SearchForNext(vert_id, SecondNeighbor(vert_id), Neighbor[i]);
	}
	//Wyslanie informacji o grafie do Node0
	if(NodeId > 0)
	{
		for(int i=0; i<VPN; i++)
		{
			PutInt(0, Neighbor[i].first_id);
			PutInt(0, Neighbor[i].first_dist);
			
			PutInt(0, Neighbor[i].second_id);
			PutInt(0, Neighbor[i].second_dist);
		}
		Send(0);
	}
	/*********************************************************************************************/
	//Zebranie informacji przez Node0 i wypisanie odpowiedzi
	else
	{
		map<int, struct NEIGHBOR> graph;
		//Pobranie informacji od pozostalych Node'ow
		for(int i=1; i<NodeNum; i++)
		{		
			int source_id, vert_id;	
			source_id = Receive(-1);
			for(int j=0; j<VPN; j++)
			{
				vert_id = POIv[source_id + j*NodeNum];
				
				graph[vert_id].id = vert_id;
				graph[vert_id].first_id = GetInt(source_id);
				graph[vert_id].first_dist = GetInt(source_id);
				graph[vert_id].second_id = GetInt(source_id);
				graph[vert_id].second_dist = GetInt(source_id);
			}			
		}
		//Pobranie informacji od Node0
		for(int i=0; i<VPN; i++)
			graph[Neighbor[i].id] = Neighbor[i];
		//Dla kazdego POI generuje odlegosc od POIv[0]
		//odleglosc ta przechowywana jest w graph[].id
		int curr = POIv[0], prev, distance = 0;
		graph[curr].id = distance;
		prev = curr;
		distance += graph[curr].first_dist;
		curr = graph[curr].first_id;
		
		while(curr != POIv[0])
		{			
			graph[curr].id = distance;
			if(graph[curr].first_id != prev)
			{
				prev = curr;
				distance += graph[curr].first_dist;
				curr = graph[curr].first_id;
			}
			else
			{
				prev = curr;
				distance += graph[curr].second_dist;
				curr = graph[curr].second_id;
			}
		}		
		//Odpowiedz na zapytania
		for(int i=1, a, b, res; i<=QueriesNum; ++i)
		{
			a = graph[QueryFrom(i)].id;
			b = graph[QueryTo(i)].id;
			res = min(abs(a-b), VertNum-abs(a-b));
			printf("%d\n", res);						
		}
	}
	
	
	return 0;
}


void SearchForNext(int prev, int curr, NEIGHBOR &nbh)
{
	int distance = 1;
	
	while(POI.find(curr) == POI.end())
	{
		if(FirstNeighbor(curr) != prev)
		{
			prev = curr; 
			curr = FirstNeighbor(curr);
		}
		else
		{
			prev = curr;
			curr = SecondNeighbor(curr);
		}
		distance++;
	}
	if(nbh.first_id == 0)
	{
		nbh.first_id = curr;
		nbh.first_dist = distance;
	}
	else
	{
		nbh.second_id = curr;
		nbh.second_dist = distance;
	}
}