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<stdio.h>
#include<kollib.h>
#include<message.h>
#include<stdlib.h>

main() {
	int l_uczniow = NumberOfStudents();
	int l_zadan = NumberOfQueries();

	int l_instancji = NumberOfNodes();

	int id = MyNodeId();
	int akt_test = 0;
	int working = 0;

	if (id == 0) {
		int* instancje = (int*) malloc(sizeof(int)*l_instancji);
		int* rozw = (int*) malloc(sizeof(int)*l_zadan);
		for(int i=0;i<l_instancji;i++) {
			if (akt_test > l_zadan) { goto DUPA2; }
			working++;
			PutInt(i+1,++akt_test);
			PutInt(i+1,0);
			Send(i+1);
		}
DUPA2:		while(working) {
			int out = Receive(-1);
			int wynik = GetInt(out);
			int ktory = GetInt(out);
			working--;
			rozw[ktory-1] = wynik;
			if (akt_test <= l_zadan) {
				PutInt(out,++akt_test);
				PutInt(out,0);
				Send(out);
				working++;
			}
		}
		for (int i=0;i<l_instancji;i++) {
			PutInt(i+1,-1);
			PutInt(i+1,0);
			Send(i+1);
		}
		for (int i=0;i<l_zadan;i++) {
			printf("%d\n", (l_uczniow-rozw[i] > rozw[i])?rozw[i]:(l_uczniow-rozw[i]));
		}
	} else {
DUPA:		int entry = Receive(0);
		int value = GetInt(entry);
		int order = GetInt(entry);
		if (value < 0) { return 0; }
		int from = QueryFrom(value);
		int to = QueryTo(value);
		int i = 0;
		int now = 0; int before = 0;
		while(1) {
			i++;
			now = (order ? FirstNeighbor(from) : SecondNeighbor(from));
			order = 0;
			if (now == before) {
				now = SecondNeighbor(before);
			}
			if (now == to) {
				PutInt(0,i);
				PutInt(0,value);
				Send(0);
				goto DUPA;
			}
		}
	}
}