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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//Maciej Poleski
#ifdef DEBUG
#define _GLIBCXX_CONCEPT_CHECKS
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cassert>
namespace
{
namespace Wrapper
{
std::ifstream in;
std::ofstream out;
}
void init(int argc, char **argv)
{
    if(argc != 3)
    {
        std::cerr << "Potrzeba dokładnie dwóch argumentów\n";
        std::abort();
    }
    Wrapper::in.open(argv[1]);
    Wrapper::out.open(argv[2]);
}
}
#define check(x) assert(x)
#else
#ifndef NDEBUG
#define NDEBUG
#endif
#define check(x)
#include <iostream>
namespace
{
namespace Wrapper
{
std::istream &in = std::cin;
std::ostream &out = std::cout;
}
}
#endif

#include <cstdint>

namespace
{
namespace Wrapper
{
typedef std::uint_fast64_t uint_fast64_t;
typedef std::uint_fast32_t uint_fast32_t;
typedef std::uint_fast16_t uint_fast16_t;
typedef std::uint_fast8_t uint_fast8_t;

typedef std::uint64_t uint64_t;
typedef std::uint32_t uint32_t;
typedef std::uint16_t uint16_t;
typedef std::uint8_t uint8_t;

typedef std::int_fast64_t int_fast64_t;
typedef std::int_fast32_t int_fast32_t;
typedef std::int_fast16_t int_fast16_t;
typedef std::int_fast8_t int_fast8_t;

typedef std::int64_t int64_t;
typedef std::int32_t int32_t;
typedef std::int16_t int16_t;
typedef std::int8_t int8_t;

typedef std::size_t size_t;
}

}

#include <string>
#include <algorithm>
#include <limits>
#include <locale>
#include <cstring>
#include <utility>
#include <cstdlib>
#include <random>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <iomanip>
#include <set>
#include <map>
#include <memory>
#include <functional>
#include <unordered_map>
#include <unordered_set>
#include <complex>
#include <type_traits>

#include "message.h"
#include "kollib.h"

namespace
{
using namespace Wrapper;

// Typ identyfikatorów wejściowych węzłów (studentów)
typedef uint_fast32_t studentId_t;

// Oznacza że już nie ma więcej informacji o odległościach między studentami w wiadomości.
static constexpr studentId_t endOfStudents = 0;

/* Format wiadomości:
 *
 * Dowolnie wiele razy:
 * student_a : int
 * student_b : int
 * dist(student_a,student_b) : int
 * student_c : int
 * dist(student_a,student_c) : int
 *
 * oraz
 *
 * >endOfStudents< : int (koniec wiadomości)
 */

// Typ odległości między studentami w cyklu wejściowym
typedef studentId_t student_distance_t;

// Typ identyfikatorów zamapowanych węzłów (zredukowanego cyklu)
typedef uint_fast32_t internal_node_t;

// Typ odległości w zamapowanym cyklu.
typedef student_distance_t internal_distance_t;

// Ile dodatkowych węzłów zostanie stworzonych (wylosowanych)
static constexpr internal_node_t numberOfAdditionalStudents = 1000;

// Jeżeli cykl jest mniejszy - przejdź od razu do części sekwencyjnej
static constexpr uint_fast32_t minimumSizeToDistribute = 10000;

struct Edge
{
    internal_node_t destinatioNode;
    internal_distance_t distance;
};

struct Node
{
    internal_distance_t distance;
    Edge edges[2];
};

inline static void solution()
{
    using std::swap;
    const auto numberOfStudents = NumberOfStudents();
    const auto numberOfQueries = NumberOfQueries();
    const auto myNodeId = MyNodeId();

    if(numberOfStudents <= minimumSizeToDistribute)
    {
        // Bez mapowania, bez sieci
        if(myNodeId != 0)
        {
            return;
        }
        Node *nodes = new Node[numberOfStudents + 1];
        for(internal_node_t i = 1; i <= numberOfStudents; ++i)
        {
            nodes[i].edges[0] = {FirstNeighbor(i), 1};
            nodes[i].edges[1] = {SecondNeighbor(i), 1};
        }
        constexpr internal_node_t startNode = 1;
        nodes[startNode].distance = 0;
        internal_node_t node = nodes[startNode].edges[0].destinatioNode;
        nodes[node].distance = nodes[startNode].edges[0].distance;
        for(internal_node_t prev = startNode; node != startNode;)
        {
            const auto nextEdge = (prev != nodes[node].edges[0].destinatioNode) ? 0 : 1;
            const auto nextNode = nodes[node].edges[nextEdge].destinatioNode;
            nodes[nextNode].distance = nodes[node].distance + nodes[node].edges[nextEdge].distance;
            prev = node;
            node = nextNode;
        }

        // Odpowiedz na pytania
        for(uint_fast32_t i = 1; i <= numberOfQueries; ++i)
        {
            const auto a = QueryFrom(i), b = QueryTo(i);
            if(a == b)
            {
                out << "0\n";
            }
            else
            {
                const auto internalA = a;
                const auto internalB = b;
                internal_distance_t distToA = nodes[internalA].distance;
                internal_distance_t distToB = nodes[internalB].distance;
                if(distToA > distToB)
                {
                    swap(distToA, distToB);
                }
                // distToA<distToB
                const auto dist = distToB - distToA;
                out << std::min(dist, numberOfStudents - dist) << '\n';
            }
        }

        delete [] nodes;
    }
    else
    {
        std::vector<studentId_t> selectedStudents;
        selectedStudents.reserve(numberOfQueries + numberOfAdditionalStudents);
        std::unordered_map<studentId_t, internal_node_t> studentToInternalId;
        for(uint_fast32_t i = 1; i <= numberOfQueries; ++i)
        {
            const auto a = QueryFrom(i), b = QueryTo(i);
            if(a == b)
            {
                continue;    // Odpowiedź = 0
            }
            if(studentToInternalId.find(a) == studentToInternalId.end())
            {
                selectedStudents.push_back(a);
                studentToInternalId[a] = selectedStudents.size() - 1;
            }
            if(studentToInternalId.find(b) == studentToInternalId.end())
            {
                selectedStudents.push_back(b);
                studentToInternalId[b] = selectedStudents.size() - 1;
            }
        }
        // Generator powinien zachowywać się identycznie na wszystkich węzłach
        std::mt19937 engine(404);
        std::uniform_int_distribution<studentId_t> nodesDistribution(1, numberOfStudents);
        for(uint_fast32_t i = 0; i < numberOfAdditionalStudents; ++i)
        {
            const studentId_t newNode = nodesDistribution(engine);
            if(studentToInternalId.find(newNode) == studentToInternalId.end())
            {
                selectedStudents.push_back(newNode);
                studentToInternalId[newNode] = selectedStudents.size() - 1;
            }
        }
        // Wygenerowano identyfikatory studentów

        const auto numberOfNodes = NumberOfNodes();

        for(auto i = myNodeId; i < selectedStudents.size(); i += numberOfNodes)
        {
            const studentId_t startId = selectedStudents[i];
            PutInt(0, startId);

            studentId_t node = FirstNeighbor(startId);
            student_distance_t dist = 1;
            for(studentId_t prevNode = startId; studentToInternalId.find(node) == studentToInternalId.end(); ++dist)
            {
                studentId_t nextNode = (prevNode != FirstNeighbor(node)) ? FirstNeighbor(node) : SecondNeighbor(node);
                prevNode = node;
                node = nextNode;
            }
            PutInt(0, node);
            PutInt(0, dist);

            node = SecondNeighbor(startId);
            dist = 1;
            for(studentId_t prevNode = startId; studentToInternalId.find(node) == studentToInternalId.end(); ++dist)
            {
                studentId_t nextNode = (prevNode != FirstNeighbor(node)) ? FirstNeighbor(node) : SecondNeighbor(node);
                prevNode = node;
                node = nextNode;
            }
            PutInt(0, node);
            PutInt(0, dist);
        }
        PutInt(0, endOfStudents);
        Send(0);

        if(myNodeId == 0)
        {
            // Przygotuj strukture do odpowiedzi na zapytania na podstawie wiadomości
            Node *nodes = new Node[selectedStudents.size()];
            for(uint_fast32_t i = 0; i < numberOfNodes; ++i)
            {
                const auto node = Receive(-1);
                for(;;)
                {
                    const studentId_t studentA = GetInt(node);
                    if(studentA == endOfStudents)
                    {
                        break;
                    }
                    const internal_node_t nodeA = studentToInternalId[studentA];
                    studentId_t anotherStudent = GetInt(node);
                    student_distance_t distance = GetInt(node);
                    nodes[nodeA].edges[0] = {studentToInternalId[anotherStudent], distance};
                    anotherStudent = GetInt(node);
                    distance = GetInt(node);
                    nodes[nodeA].edges[1] = {studentToInternalId[anotherStudent], distance};
                }
            }

            // Policz odległości (jak sumy prefiksowe)
            constexpr internal_node_t start = 0;
            nodes[start].distance = 0;
            internal_node_t node = nodes[start].edges[0].destinatioNode;
            nodes[node].distance = nodes[start].edges[0].distance;
            for(internal_node_t prev = start; node != start;)
            {
                const auto nextEdge = (prev != nodes[node].edges[0].destinatioNode) ? 0 : 1;
                const auto nextNode = nodes[node].edges[nextEdge].destinatioNode;
                nodes[nextNode].distance = nodes[node].distance + nodes[node].edges[nextEdge].distance;
                prev = node;
                node = nextNode;
            }

            // Odpowiedz na pytania
            for(uint_fast32_t i = 1; i <= numberOfQueries; ++i)
            {
                const auto a = QueryFrom(i), b = QueryTo(i);
                if(a == b)
                {
                    out << "0\n";
                }
                else
                {
                    const auto internalA = studentToInternalId[a];
                    const auto internalB = studentToInternalId[b];
                    internal_distance_t distToA = nodes[internalA].distance;
                    internal_distance_t distToB = nodes[internalB].distance;
                    if(distToA > distToB)
                    {
                        swap(distToA, distToB);
                    }
                    // distToA<distToB
                    const auto dist = distToB - distToA;
                    out << std::min(dist, numberOfStudents - dist) << '\n';
                }
            }
            delete [] nodes;
        }
    }
}

} // namespace

int main(int argc, char **argv)
{
    std::ios_base::sync_with_stdio(false);
#ifdef DEBUG
    init(argc, argv);
#else
    (void)argc;
    (void)argv;
#endif
    solution();
    return 0;
}