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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "kollib.h"
#include "message.h"

//#define	DEBUG

#define	MAX_QUERIES 200

#ifdef	DEBUG
#define	DBG(x)	x
#else
#define	DBG(x)
#endif

#define	get_next(next, u, prev) do { \
	next = FirstNeighbor(u); \
	if (next == prev) \
		next = SecondNeighbor(u); \
} while (0)

/*
int get_next(int u, int prev)
{
	int next = FirstNeighbor(u);
	if (next == prev)
		next = SecondNeighbor(u);
	return next;
}
*/

int solve(int q)
{
	int u = QueryFrom(q);
	int v = QueryTo(q);
	int r = 0;
	DBG(fprintf(stderr, "=== %d->%d\n", u, v));
	if (u != v) {
		int ulp = u;
		int urp = u;
		int ul = FirstNeighbor(u);
		int ur = SecondNeighbor(u);
		assert(ul < ur);
		r++;
		DBG(fprintf(stderr, "L:%d R:%d\n", ul, ur));
		while (ul != v && ur != v) {
			int next;
			get_next(next, ul, ulp);
			ulp = ul;
			ul = next;
			get_next(next, ur, urp);
			urp = ur;
			ur = next;
			r++;
			DBG(fprintf(stderr, "L:%d R:%d\n", ul, ur));
		}
	}
	DBG(fprintf(stderr, "=== %d->%d : %d\n", u, v, r));
	return r;
}

void worker(void)
{
	DBG(fprintf(stderr, "WORKER START\n"));

	for (;;) {
		int q, r;

		Receive(0);
		q = GetInt(0);
		if (!q) {
			break;
		}

		r = solve(q);

		PutInt(0, r);
		Send(0);
	}

	DBG(fprintf(stderr, "WORKER END\n"));
}

void do_all(int n)
{
	int i;

	DBG(fprintf(stderr, "START\n"));

	for (i = 1; i <= n; i++) {
		int r = solve(i);
		printf("%d\n", r);
	}

	DBG(fprintf(stderr, "END\n"));
}

typedef enum {
	UNRESOLVED,
	SOLVING,
	SOLVED
} query_state_t;

query_state_t solved[MAX_QUERIES + 1];
int result[MAX_QUERIES + 1];

void arbiter(int n, int workers)
{
	int to_do = 0;
	int solved_cnt = 0;
	int *busy = calloc(sizeof(*busy), workers + 1);	// od 1 do workers; w tablicy nr zadania lub 0, jeśli worker wolny
	int busy_cnt = 0;
	int i;

	DBG(fprintf(stderr, "ARBITER START\n"));

	while (solved_cnt < n) {
		DBG(fprintf(stderr, "%d/%d zadań rozwiązanych, %d/%d roboli zajętych\n", solved_cnt, n, busy_cnt, workers));

		if (!to_do) {
			if (solved_cnt + busy_cnt < n) {
				// znajdź zadanie do zlecenia
				for (to_do = 1; to_do <= n; to_do++) {
					if (solved[to_do] == UNRESOLVED) {
						break;
					}
				}
				DBG(fprintf(stderr, "zadanie do zlecenia = %d\n", to_do));
				assert(to_do <= n);
			}
		}

		if (to_do && busy_cnt < workers) {
			// znajdź robola
			for (i = 1; i <= workers; i++) {
				if (!busy[i]) {
					break;
				}
			}
			assert(i <= workers);
			DBG(fprintf(stderr, "zlecam %d robolowi %d\n", to_do, i));
			// zleć zadanie
			busy_cnt++;
			busy[i] = to_do;
			solved[to_do] = SOLVING;
			PutInt(i, to_do);
			to_do = 0;
			Send(i);
		} else {
			int q;
			// czytamy wynik
			DBG(fprintf(stderr, "czekam na jakiś wynik\n"));
			i = Receive(-1);
			q = busy[i];
			assert(q);
			assert(solved[q] == SOLVING);
			busy[i] = 0;
			busy_cnt--;
			assert(busy_cnt >= 0);
			result[q] = GetInt(i);
			solved_cnt++;
			assert(solved_cnt <= n);
			solved[q] = SOLVED;
			DBG(fprintf(stderr, "dostałem odpowiedź %d na %d od %d\n", result[q], q, i));
		}
	}

	// pozabijaj roboli
	for (i = 1; i <= workers; i++) {
		PutInt(i, 0);
		Send(i);
	}

	// wypisz wyniki
	for (i = 1; i <= n; i++) {
		assert(solved[i] == SOLVED);
		printf("%d\n", result[i]);
	}

	DBG(fprintf(stderr, "ARBITER END\n"));
}

int main(void)
{
	if (MyNodeId()) {
		worker();
	} else {
		int n = NumberOfQueries();
		int workers = NumberOfNodes();

		if (workers > 1) {
			arbiter(n, workers - 1);
		} else {
			do_all(n);
		}
	}

	return 0;
}