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
#include "kollib.h"
#include "message.h"
#include <stdio.h>
#include <set>
#include <vector>
#include <map>
#include <utility>

inline int hash(const int num, const long long int stud) {
    long long int param = num / 2;
    return ((param * 1021 * 7919 * 317 + 127) % stud) + 1;
}

inline int master_id(const int nodes) {
    return (nodes%2 == 0)? 0 : nodes-1;
}

inline bool has(const std::set<int> & s, const int n) {
    return s.find(n) != s.end();
}

inline bool has(const std::map<int, std::vector< std::pair<int, int> > > & s, const int n) {
    return s.find(n) != s.end();
}


int nodes, students, qnum, id, master, milestone;
std::set<int> milestones, wanted;
std::vector< std::pair<int, int> > queries, dist;
std::map<int, std::vector< std::pair<int, int> > > graph;

void prepare() {
    nodes = NumberOfNodes();
    students = NumberOfStudents();
    id = MyNodeId();
    master = master_id(nodes);
    milestone = hash(id, students);
    qnum = NumberOfQueries();
    for (int i = 0; i < qnum; ++i) {
        int from, to;
        from = QueryFrom(i+1);
        to = QueryTo(i+1);
        queries.push_back(std::make_pair(from, to));
        wanted.insert(from);
        wanted.insert(to);
    }
    for (int i = 0; i < nodes; i += 2) {
        milestones.insert(hash(i, students));
    }
}

void discover() {
    int dir, step = 1;
    if (id % 2 == 0) {
        dir = FirstNeighbor(milestone);
    } else {
        dir = SecondNeighbor(milestone);
    }
    if (has(wanted, dir) && !has(milestones, dir)) {
        //printf("Znalazlem %d!\n", dir);
        dist.push_back(std::make_pair(dir, step));
    }
    int s = dir, prev = milestone;
    while (!has(milestones, s)) {
        ++step;
        int first = FirstNeighbor(s);
        int second = SecondNeighbor(s);
        if (first == prev) {
            prev = s;
            s = second;
        } else {
            prev = s;
            s = first;
        }
        if (has(wanted, s) && !has(milestones, s)) {
            //printf("Znalazlem %d!\n", s);
            dist.push_back(std::make_pair(s, step));
        }
    }
    
    PutInt(master, milestone);
    PutInt(master, s);
    PutInt(master, step);
    PutInt(master, dist.size());
    for (int i = 0; i < dist.size(); ++i) {
        PutInt(master, dist[i].first);
        PutInt(master, dist[i].second);
    }
    Send(master);
}

void runmaster() {
    int how_many = (nodes%2 == 1)? (nodes-1) : nodes;
    /*
    for (int i = 0; i < nodes; i += 2) {
        graph[hash(i, students)] = std::vector< std::pair<int, int> >();
    }
    */
    for (int i = 0; i < how_many; ++i) {
        int source = Receive(-1);
        int start = GetInt(source);
        int end = GetInt(source);
        int steps = GetInt(source);
        int found = GetInt(source);
        //printf("Wiesci od %d: przeszedl %d-%d, zanalazl %d typow\n", source, start, end, found);
        std::vector< std::pair<int, int> > v(found);
        for (int j = 0; j < found; ++j) {
            v[j].first = GetInt(source);
            v[j].second = GetInt(source);
        }
        if (start < end) {
            if (found == 0) {
                graph[start].push_back(std::make_pair(end, steps));
                graph[end].push_back(std::make_pair(start, steps));
            } else {
                graph[start].push_back(std::make_pair(v[0].first, v[0].second));
                graph[end].push_back(std::make_pair(v[found-1].first, steps - v[found-1].second));
                graph[v[0].first].push_back(std::make_pair(start, v[0].second));
                graph[v[found-1].first].push_back(std::make_pair(end, steps - v[found-1].second));
                for (int j = 1; j < found; ++j) {
                    int d = v[j].second - v[j-1].second;
                    int s = v[j].first, prev = v[j-1].first;
                    graph[s].push_back(std::make_pair(prev, d));
                    graph[prev].push_back(std::make_pair(s, d));
                }
            }
        }
    }
    
    for (int i = 0; i < qnum; ++i) {
        int from = queries[i].first, to = queries[i].second;
        int d = 0;
        int temp = from;
        int prev = -1;
        while (temp != to) {
            int which = 0;
            if (graph[temp][which].first == prev) 
                ++which;
            prev = temp;
            d += graph[temp][which].second;
            temp = graph[temp][which].first;
        }
        if (d > students/2)
            d = students - d;
        printf("%d\n", d);
    }
}

int main() {
    prepare();
    if (nodes % 2 == 0 || id != nodes - 1) {
        discover();
    }
    if (id == master) {
        runmaster();
    }
    return 0;
}