program Kola; {$mode objfpc}{$H+} uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} Classes, message, kollib; Type TDat=Array[0..1024] of Longint; TProcess=Array[0..1024] of Longint; var Inst:Longint; Stud:Longint; R:TDat; Process:TProcess; Function Min(A,B:Longint):Longint; Begin If A<B Then Result:=A Else Result:=B; End; Function Max(A,B:Longint):Longint; Begin If A>B Then Result:=A Else Result:=B; End; Procedure SendQuery; var Loop,M:Longint; QSend,QRecive:Longint; QFrom,QTo:Longint; A,Anserw:Longint; Nodes,Students:Longint; Begin Nodes:=NumberOfNodes-1; Students:=NumberOfStudents; M:=Min(NumberOfQueries,Nodes); For Loop:=1 to M do Begin QFrom:=QueryFrom(Loop); QTo :=QueryTo (Loop); PutInt(Loop,QFrom); PutInt(Loop,QTo ); Send (Loop ); Process[Loop]:=Loop; End; For Loop:=M+1 to NumberOfQueries do Begin Anserw:=Receive(-1); A:=GetInt(Anserw); R[Process[Anserw]]:=Min(A,Students-A); QFrom:=QueryFrom(Loop); QTo :=QueryTo (Loop); PutInt( Anserw, QFrom); PutInt( Anserw, QTo ); Send ( Anserw ); Process[Anserw]:=Loop; End; For Loop:=1 to M do Begin Anserw:=Receive(-1); A:=GetInt(Anserw); R[Process[Anserw]]:=Min(A,Students-A); PutInt( Anserw, -1 ); PutInt( Anserw, -1 ); Send ( Anserw ); End; For Loop:=M+1 to Nodes do Begin PutInt( Loop, -1 ); PutInt( Loop, -1 ); Send ( Loop ); End; For Loop:=1 to NumberOfQueries do WriteLn(R[Loop]); End; Procedure ReceiveQuery; Var Step,Curent,Last,Left,Rigth,Source,Target:Longint; Begin Repeat Receive(0); Source:=GetInt(0); Target:=GetInt(0); Last :=Source; Curent:=Source; Step :=0; If Source>-1 Then Begin If Source<>Target Then While (Curent<>Target) do Begin Step := Step+1; Left := FirstNeighbor (Curent); Rigth:= SecondNeighbor(Curent); If Left<>Last Then Begin Last :=Curent; Curent:=Left; End Else Begin Last :=Curent; Curent:=Rigth; End; End; PutInt(0,Step); Send(0); End; Until Source=-1; End; begin Inst:=MyNodeId; If Inst=0 Then SendQuery Else ReceiveQuery; end.
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 | program Kola; {$mode objfpc}{$H+} uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} Classes, message, kollib; Type TDat=Array[0..1024] of Longint; TProcess=Array[0..1024] of Longint; var Inst:Longint; Stud:Longint; R:TDat; Process:TProcess; Function Min(A,B:Longint):Longint; Begin If A<B Then Result:=A Else Result:=B; End; Function Max(A,B:Longint):Longint; Begin If A>B Then Result:=A Else Result:=B; End; Procedure SendQuery; var Loop,M:Longint; QSend,QRecive:Longint; QFrom,QTo:Longint; A,Anserw:Longint; Nodes,Students:Longint; Begin Nodes:=NumberOfNodes-1; Students:=NumberOfStudents; M:=Min(NumberOfQueries,Nodes); For Loop:=1 to M do Begin QFrom:=QueryFrom(Loop); QTo :=QueryTo (Loop); PutInt(Loop,QFrom); PutInt(Loop,QTo ); Send (Loop ); Process[Loop]:=Loop; End; For Loop:=M+1 to NumberOfQueries do Begin Anserw:=Receive(-1); A:=GetInt(Anserw); R[Process[Anserw]]:=Min(A,Students-A); QFrom:=QueryFrom(Loop); QTo :=QueryTo (Loop); PutInt( Anserw, QFrom); PutInt( Anserw, QTo ); Send ( Anserw ); Process[Anserw]:=Loop; End; For Loop:=1 to M do Begin Anserw:=Receive(-1); A:=GetInt(Anserw); R[Process[Anserw]]:=Min(A,Students-A); PutInt( Anserw, -1 ); PutInt( Anserw, -1 ); Send ( Anserw ); End; For Loop:=M+1 to Nodes do Begin PutInt( Loop, -1 ); PutInt( Loop, -1 ); Send ( Loop ); End; For Loop:=1 to NumberOfQueries do WriteLn(R[Loop]); End; Procedure ReceiveQuery; Var Step,Curent,Last,Left,Rigth,Source,Target:Longint; Begin Repeat Receive(0); Source:=GetInt(0); Target:=GetInt(0); Last :=Source; Curent:=Source; Step :=0; If Source>-1 Then Begin If Source<>Target Then While (Curent<>Target) do Begin Step := Step+1; Left := FirstNeighbor (Curent); Rigth:= SecondNeighbor(Curent); If Left<>Last Then Begin Last :=Curent; Curent:=Left; End Else Begin Last :=Curent; Curent:=Rigth; End; End; PutInt(0,Step); Send(0); End; Until Source=-1; End; begin Inst:=MyNodeId; If Inst=0 Then SendQuery Else ReceiveQuery; end. |