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
#include <vector>
#include <iostream>
#include <sstream>
#include <math.h>
#include <sys/time.h>
#include <cstdlib>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <fstream>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include "kollib.h"
#include "message.h"

#define FOR(i,a,b)  for(__typeof(b) i=(a);i<(b);++i)
#define REP(i,a)    FOR(i,0,a)
#define FOREACH(x,c)   for(__typeof(c.begin()) x=c.begin();x != c.end(); x++)
#define ALL(c)      c.begin(),c.end()
#define CLEAR(c)    memset(c,0,sizeof(c))
#define SIZE(c) (int) ((c).size())

#define PB          push_back
#define MP          make_pair
#define X           first
#define Y           second

#define ULL         unsigned long long
#define LL          long long
#define LD          long double
#define II         pair<int, int>
#define DD         pair<double, double>

#define VC	    vector
#define VI          VC<int>
#define VVI         VC<VI>
#define VD          VC<double>
#define VS          VC<string>
#define VII         VC<II>
#define VDD         VC<DD>

#define DB(a)       cerr << #a << ": " << a << endl;

using namespace std;

template<class T> void print(VC < T > v) {cerr << "[";if (SIZE(v) != 0) cerr << v[0]; FOR(i, 1, SIZE(v)) cerr << "," << v[i]; cerr << "]\n";}
template<class T> string i2s(T &x) { ostringstream o; o << x; return o.str(); }
VS split(string &s, char c = ' ') {VS all; int p = 0, np; while (np = s.find(c, p), np >= 0) {if (np != p) all.PB(s.substr(p, np - p)); p = np + 1;} if (p < SIZE(s)) all.PB(s.substr(p)); return all;}

int N, ID;
int n;

void distInit(){
    N = NumberOfNodes();
    ID = MyNodeId();
    n = NumberOfStudents();
}

int W;
VI WPV;
set<int> WP;
set<int> Q;
VI QV;
VII QII;
int q;

void readQueries(){
    q = NumberOfQueries();
    REP(i,q){
        int a = QueryFrom(i+1);
        int b = QueryTo(i+1);
        Q.insert(a); Q.insert(b);
        QII.PB(MP(a,b));
        QV.PB(a);QV.PB(b);
    }
    sort(ALL(QV));
}

void genWaypoints(){
    readQueries();
    W = min(10*N,n/5);
    //WP = unordered_set<int>(3*W);
    WPV.clear(); WPV.reserve(3*W);
    
    srand(666);

    while(SIZE(WP) < W){
        int w = rand()%n+1;
        if (WP.find(w) == WP.end()){
            WPV.PB(w);
            WP.insert(w);
        }
    }
    sort(ALL(WPV));
}

map<int,VII> edges;

void store(const VII &history){
    REP(i,SIZE(history)-1)
        edges[history[i].X].PB(MP(history[i+1].X,history[i+1].Y-history[i].Y));
}

void send(const VII &history){
    if (ID == 0)
        store(history);
    else{
        PutInt(0,SIZE(history));
        REP(i,SIZE(history)){
            PutInt(0,history[i].X);
            PutInt(0,history[i].Y);
        }
        Send(0);
    }    
}

int n1, n2;

void scan(){
    n1 = ((LL)ID*W)/N;
    n2 = ((LL)(ID+1)*W)/N;

    for(int i=n1; i < n2; i++){
        REP(j,2){
            VII history;
            int prev = WPV[i];
            history.PB(MP(prev,0));
            int p = (j?FirstNeighbor(prev):SecondNeighbor(prev));
            int steps = 1;
            //while( WP.find(p) == WP.end()){
            while(!binary_search(ALL(WPV),p)){
            //    if (Q.find(p) != Q.end())
                if (binary_search(ALL(QV),p))
                    history.PB(MP(p,steps));
                int next = FirstNeighbor(p);
                if (next == prev)
                    next = SecondNeighbor(p);
                prev = p;
                p = next;
                steps++;
            } 
            history.PB(MP(p,steps));
            send(history);
        }
    }
}

map<int,int> pos;

void goRound(){
    int i = WPV[0], steps = 0;
    int prev = -1;
    do{
        //cerr << i << " " << steps << endl;
        pos[i] = steps;
        for(auto e : edges[i])
            if (e.X != prev){
                prev = i;
                i = e.X;
                steps += e.Y;
                break;
            }
    } while (i != WPV[0]);
}

void runQueries(){
    for(auto q : QII){
        int a = pos[q.X], b = pos[q.Y];
        int res = abs(a-b);
        if (res > n/2) res = n-res;
        printf("%d\n",res);
    } 
}

void answer(){
    VII history;
    REP(i,2*(W-n2)){
        history.clear();
        int from = Receive(-1);
        int sz = GetInt(from);
        REP(j,sz){
            int a = GetInt(from);
            int b = GetInt(from);
            history.PB(MP(a,b));
        }
        store(history);
    }
    goRound();
    runQueries();
}

void solve(){
    genWaypoints();
    scan();
    if (ID == 0)
        answer();
}

void solveBrute(){
    int prev = -1;
    int p = 1;
    int steps = 0;
    pos[p] = steps;
    REP(i,n-1){
        int next = FirstNeighbor(p);
        if (next == prev)
            next = SecondNeighbor(p);
        prev = p;
        p = next;
        steps++;
        pos[p] = steps;
    } 
    readQueries();
    runQueries();
}

int main(int argc, char *argv[]){
    distInit();
    if (n <= 100 || N == 1){
        if (ID == 0)
            solveBrute();
    }else
        solve();
    return 0;
}