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
#include "kollib.h"
#include "message.h"
#include <iostream>
using namespace std;

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <exception>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <regex>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <tuple>
using namespace std;

#define fore(it, x) for (auto &it: x)
#define fo(x, a, b) for (auto x = a; x <= b; x++)
#define rep(x, n) for (auto x = 0; x < n; x++)
#define vi vector<int>

#define ll long long

const int P = (int)1e9 + 7;
int N;
int nodes;
int id;
int queries;

set<ll> data;

typedef array<int, 4> ret;

int next(int p, int &prev) {
    if (FirstNeighbor(p) != prev) {
        prev = p;
        return FirstNeighbor(p);
    }
    else {
        prev = p;
        return SecondNeighbor(p);
    }
}

#define db(x) cerr << #x << " " << x << endl;

int result = (int)1e9;

ret go(int p, bool eq, int b) {
    if (eq && data.count(p)) return ret({{p, 0, p, 0}});
    int prevle = p; int prevri = p;
    int le = FirstNeighbor(p);
    int ri = SecondNeighbor(p);
    if (le == b) result = 0;
    int s0 = 1; int s1 = 1;

    while (data.count(le) == 0) {
        le = next(le, prevle);
        s0++;
        if (le == b) result = min(result, s0);
    }
    while (data.count(ri) == 0) {
        ri = next(ri, prevri);
        s1++;
        if (ri == b) result = min(result, s1);
    }

    return ret({{le, s0, ri, s1}});
}

map <int, map<int,int>> edge;

map <int, int> costs;

void dfs(int i, int curcost) {
    if (costs.count(i) == 0 || costs[i] > curcost)
        costs[i] = curcost;
    else 
        return;
    fore (it, edge[i])
        dfs(it.first, curcost + it.second);
}

void doit() {
    srand(0);
    for (int j = 1; j <= min(200, N / 3); j++) {
        ll val = rand() % N + 1;
        if (data.count(val)) { j--; continue; }
        data.insert(val);
    }
    if (data.size() == 0)
        data.insert(1);
    int k = 0;
    int cnt = 0;
    fore (it, data) {
        if ((k++) % nodes == id) {
            cnt++;
            auto ans = go(it, false, -2);
            rep (j, nodes) {
                PutInt(j, it);
                rep (k, 4)
                    PutInt(j, ans[k]);
                Send(j);
            }
        }
    }
    fore (___it, data) {
        int from = Receive(-1);
        int kto = GetInt(from);
        ret ans;
        rep (j, 4) ans[j] = GetInt(from);
        edge[kto][ans[0]] = ans[1];
        edge[kto][ans[2]] = ans[3];
    }
    for (int i = queries - id; i >= 1; i -= nodes) {
        int a, b;
        a = QueryFrom(i);
        b = QueryTo(i);
        result = (int)1e9;
        auto ans = go(a, true, b);
        auto other = go(b, true, -2);
        dfs(ans[0], ans[1]);
        dfs(ans[2], ans[3]);

        result = min(result, costs[other[0]] + other[1]);
        result = min(result, costs[other[2]] + other[3]);
        costs.clear(); 

        PutLL(0, i);
        PutLL(0, result);
        Send(0);
    }
}

int main() {
    id = MyNodeId();
    nodes = NumberOfNodes();
    N = NumberOfStudents();
    queries = NumberOfQueries();

    doit();

    /*
    for (int i = queries - id; i >= 1; i -= nodes) {
        int a, b;
        a = QueryFrom(i);
        b = QueryTo(i);
        int le = a, ri = a;
        int prevle = -1, prevri = -1;
        int steps = 0;
        if (a != b) {
            steps = 1;
            prevle = a;
            prevri = b;
            le = FirstNeighbor(a);
            ri = SecondNeighbor(a);
            while (le !=b && ri != b) {
                int fi;
                if (prevle == (fi=FirstNeighbor(le))) {
                    prevle = le;
                    le = SecondNeighbor(le);
                }
                else {
                    prevle = le;
                    le = fi;
                }

                if (prevri == -1) prevri = le;

                if (prevri == (fi = FirstNeighbor(ri))) {
                    prevri = ri;
                    ri = SecondNeighbor(ri);
                }
                else {
                    prevri = ri;
                    ri = fi;
                }
                steps++;
            }
        }
        PutLL(0, i);
        PutLL(0, steps);
        Send(0);
    }
    */

    if (id == 0) {
        vi ans(queries + 1);
        rep (k, queries) {
            int cn = Receive(-1);
            int i = GetLL(cn);
            int steps = GetLL(cn);
            ans[i] = steps;
        }
        fo (i, 1, queries) {
            cout << ans[i] << endl;
        }
    }
    return 0;
}