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
#include "message.h"
#include "kollib.h"
#include <iostream>
#include <cstdio>
#include <ctime>
#include <set>
#include <vector>
#include <algorithm>
#define LL long long
#define ff first
#define ss second
#define PB push_back
#define MP make_pair
#define DEBUG(x) cerr<<#x<<" "<<(x)<<endl;
using namespace std;
const int Q = 1005;
int losy[Q], lsize;
int zapytania[Q], zsize;
pair<int, int>odpowiedzi[Q];
int osize;
int size;
int dist[Q];
int ans[Q];
bool odw[Q];
vector<pair<int, int> >V[Q];
int binsearch(int w)
{
  int poc = 1;
  int kon = zsize;
  while(poc <= kon)
  {
    int sr = (poc + kon)/2;
    if(zapytania[sr] == w)
      return sr;
    if(zapytania[sr] > w)
      kon = sr-1;
    else
      poc = sr+1;
  }
  return -1;
}
bool niema(int w)
{
  for(int i=1; i<=zsize; i++)
    if(zapytania[i] == w)
      return 0;
  return 1;
}
int szukaj(int w)
{
  int poc = 0;
  int kon = lsize-1;
  int ret = -1;
  while(poc <= kon)
  {
    int sr = (poc+kon)/2;
    if(losy[sr] == w)
      ret = sr;
    if(losy[sr] >= w)
      kon = sr-1;
    else
      poc = sr+1;
  }
  return ret;
}
void symulacja(int w, int przodek)
{
    for(int i = 1;; i++)
  {
      int x = binsearch(w);
      if(x != -1)
      	odpowiedzi[++osize] = MP(x, i);      
      x = szukaj(w);
      if(w != losy[MyNodeId()] && x != -1)
      {
	odpowiedzi[++osize] = MP(x, i);
	break;
      }      
      if(przodek == FirstNeighbor(w))
      {
	przodek = w;
	w = SecondNeighbor(w);
      }
      else
      {
	przodek = w;
	w = FirstNeighbor(w);
      }      
  }
  PutInt(0, osize);
  for(int i=1; i<=osize; i++)
  {
    PutInt(0, odpowiedzi[i].ff);
    PutInt(0, odpowiedzi[i].ss);
  }
  Send(0);
}
void DFS(int w, int przodek)
{
  odw[w] = 1;
  for(int i=w; i<2*NumberOfNodes(); i+=NumberOfNodes())
  {
    if((!odw[V[i].back().ff] || (V[i].back().ff == 0 && przodek != 0 )))
    {
      for(int j=0; j<V[i].size()-1; j++)
      {
	int u = V[i][j].ff;
	ans[u] = V[i][j].ss+dist[w]-1;
      }
      dist[V[i].back().ff] = dist[w] + V[i].back().ss - 1;
      if(V[i].back().ff != 0)
      DFS(V[i].back().ff, w);
      return;
    }
  }
}

int main()
{
  srand(time(NULL));
  int ID = MyNodeId();
  int N = NumberOfNodes();
  int n = NumberOfStudents();
  int m = NumberOfQueries();
  
  if(ID == 0)
  {    
    for(int i=1; i<=N; i++)    
      losy[lsize++] = rand()%n+1;    
    sort(losy, losy+lsize);
    for(int i=1; i<N; i++)
    {
      for(int j=0; j<lsize; j++)      
	PutInt(i, losy[j]);      
      Send(i);
    }
  }  
  if(ID > 0)
  {
    Receive(0);
    for(int i=1; i<=N; i++)    
      losy[lsize++] = GetInt(0);    
  }  
  for(int i=1; i<=m; i++)
  {
    if(niema(QueryFrom(i)))    
      zapytania[++zsize] = QueryFrom(i);    
    if(niema(QueryTo(i)))    
      zapytania[++zsize] = QueryTo(i);    
  }
  sort(zapytania+1, zapytania+1+zsize);
  int w = losy[ID];
  int przodek = FirstNeighbor(w);
  symulacja(w, przodek);
  osize = 0;
  przodek = SecondNeighbor(w);
  symulacja(w, przodek);
  
  if(ID == 0)
  {
    for(int i=0; i<N; i++)
    {      
      Receive(i);
      int ile = GetInt(i);
      while(ile--)
      {
	int a = GetInt(i);	
	int b = GetInt(i);	
	V[i].PB(MP(a, b));
      }
      Receive(i);
      ile = GetInt(i);
      while(ile--)
      {
	int a = GetInt(i);	
	int b = GetInt(i);	
	V[i+N].PB(MP(a, b));
      }      
    }
    DFS(0, -1);
    for(int i=1; i<=m; i++)
    {
      int O = abs(ans[binsearch(QueryFrom(i))] - ans[binsearch(QueryTo(i))]);
      printf("%d\n", min(O, n-O));      
    }
  }
  return 0;
}