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
// 1 maszyna
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include "message.h"
/*
    int NumberOfNodes();
    int MyNodeId();
    void PutInt(int target, int value);
    void Send(int target);
    int Receive(int source);
    int GetInt(int src);
 */
#include "kollib.h"
/*
    int NumberOfStudents();
    int FirstNeighbor(int i);
    int SecondNeighbor(int i);
    int NumberOfQueries();
    int QueryFrom(int i);
    int QueryTo(int i);
 */
using namespace std;

struct query {
    int from;
    int to;
};

int main() {
    if (MyNodeId()==0) {
        int i,n,nq,start,prev,curr,first,val;
        query q;
        set<int>::iterator it;
        set<int> qNodes;
        map<int,int> fNodes;
        vector<query> qs;
        qs.clear(); qNodes.clear(); fNodes.clear();
        
        n=NumberOfStudents();
        nq=NumberOfQueries();
        if (nq>0) {
            for (i=1; i<=nq; i++) {
                q.from=QueryFrom(i);
                q.to=QueryTo(i);
                qNodes.insert(q.from);
                qNodes.insert(q.to);
                qs.push_back(q);
            }
            
            it=qNodes.begin();
            start=*it;
            i=0;
            qNodes.erase(it);
            fNodes[start]=i;
            
            prev=start;
            curr=FirstNeighbor(start);
            i++;
            while (!qNodes.empty()) {
                it=qNodes.find(curr);
                if (it!=qNodes.end()) {
                    qNodes.erase(it);
                    fNodes[curr]=i;
                }
                first=FirstNeighbor(curr);
                if (prev==first) {
                    prev=curr;
                    curr=SecondNeighbor(curr);
                }
                else {
                    prev=curr;
                    curr=first;
                }
                i++;
            }
            for (i=0; i<nq; i++) {
                val=fNodes[qs[i].from]-fNodes[qs[i].to];
                if (val<0) {
                    val = -1 * val;
                }
                if (val>n/2) {
                    val=n-val;
                }
                cout << val << endl;
            }
        }
    }
    return 0;
}