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
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <map>
#include <cstdio>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <cstring>
#define REP(i,x,v)for(int i=x;i<=v;i++)
#define REPD(i,x,v)for(int i=x;i>=v;i--)
#define FOR(i,v)for(int i=0;i<v;i++)
#define FORE(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REMIN(x,y) (x)=min((x),(y))
#define REMAX(x,y) (x)=max((x),(y))
#define pb push_back
#define sz size()
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define IN(x,y) ((y).find((x))!=(y).end())
#define un(v) v.erase(unique(ALL(v)),v.end())
#define LOLDBG
#ifdef LOLDBG
#define DBG(vari) cerr<<#vari<<" = "<<vari<<endl;
#define DBG2(v1,v2) cerr<<(v1)<<" - "<<(v2)<<endl;
#else
#define DBG(vari)
#define DBG2(v1,v2)
#endif
#define CZ(x) scanf("%d",&(x));
#define CZ2(x,y) scanf("%d%d",&(x),&(y));
#define CZ3(x,y,z) scanf("%d%d%d",&(x),&(y),&(z));
#define ALL(x) (x).begin(),(x).end()
#define tests int dsdsf;cin>>dsdsf;while(dsdsf--)
#define testss int dsdsf;CZ(dsdsf);while(dsdsf--)
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
template<typename T,typename TT> ostream &operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";}
template<typename T> ostream &operator<<(ostream &s,vector<T> t){s<<"{";FOR(i,t.size())s<<t[i]<<(i==t.size()-1?"":",");return s<<"}"<<endl; }
typedef unsigned int uint;

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


#define MT_N 624
#define MT_M 397
#define MT_MSB 0x80000000U
#define MT_LS31B 0x7FFFFFFFU
#define MT_A 2567483615U


class MersenneTwister // pseudo-random number generator
{
uint twistory[MT_N];
int pos;
public:
MersenneTwister(uint seed=0)
{
twistory[0]=seed;
for (int i=1;i<MT_N;i++) twistory[i]=1812433253U*(twistory[i-1]^(twistory[i-1]>>30))+i;
pos=0;
}
void generate(void)
{
uint tmp; int i;
for (i=0;i<MT_N-MT_M;i++)
{
tmp=(twistory[i]&MT_MSB)+(twistory[i+1]&MT_LS31B);
twistory[i]=twistory[i+MT_M]^(tmp>>1)^(MT_A&-(tmp&1));
}
for (;i<MT_N-1;i++)
{
tmp=(twistory[i]&MT_MSB)+(twistory[i+1]&MT_LS31B);
twistory[i]=twistory[i+MT_M-MT_N]^(tmp>>1)^(MT_A&-(tmp&1));
}
tmp=(twistory[i]&MT_MSB)+(twistory[0]&MT_LS31B);
twistory[i]=twistory[MT_M-1]^(tmp>>1)^(MT_A&-(tmp&1));
}
uint rand_unsigned()
{
if (pos==0) generate();
uint ans=twistory[pos++];
pos&=-(pos!=624); // if (pos == 624) pos = 0;
ans^=ans>>11;
ans^=(ans<<7)&2636928640U;
ans^=(ans<<15)&4022730752U;
ans^=ans>>18;
return ans;
}
int rand_signed()
{
return rand_unsigned()>>1;
}
int next_int(int n)
{
ll w=0;
FOR(i,15) w=(16*w+rand_unsigned()%16)%n;
return w%n;
}


};

MersenneTwister rnd(1028);
#define D ((1<<15)+231)
set<int> qnodes;
set<int> rnodes;
int hasz[D];
int rn[222];
ll n;
int q,m,myid;

#define H(x) ((((unsigned(x))*172243251))%D)

bool isqnode(int w)
{
	if (!hasz[H(w)]) return 0;
    return IN(w,qnodes);
}
bool isrnode(int w)
{
	if (!hasz[H(w)]) return 0;
    return IN(w,rnodes);
}

vector<pair<pii,int> > wys;

void go(int akt,int pre)
{
	int prev_nice=pre;
	int odl=0;

    while(1)
	{
		odl++;
        if (isqnode(akt))
		{
            wys.pb(mp(mp(prev_nice,akt),odl));
            odl=0;
            prev_nice=akt;
		}
		if (isrnode(akt))
		{
			wys.pb(mp(mp(prev_nice,akt),odl));
            break;
		}
		int a=FirstNeighbor(akt);
		if (a==pre)
		{
			pre=akt;
            akt=SecondNeighbor(akt);
		}
		else
		{
            pre=akt;
            akt=a;
		}
	}

	//if (myid==1) DBG(ciag);

}

int main()
{
    n=NumberOfStudents();
    q=NumberOfQueries();
    myid=MyNodeId();
    m=NumberOfNodes();
    FOR(i,m) rn[i]=rnd.next_int(n)+1;
    FOR(i,m) hasz[H(rn[i])]=1;
    FOR(i,m) rnodes.insert(rn[i]);
    REP(i,1,q)
    {
        int a=QueryFrom(i);
        int b=QueryTo(i);
        qnodes.insert(a);
        qnodes.insert(b);
        hasz[H(a)]=1;
        hasz[H(b)]=1;
    }
    int moj=rn[myid];
    int ri=FirstNeighbor(moj);
    int le=SecondNeighbor(moj);
    wys.clear();
    go(le,moj);
    go(ri,moj);
	PutInt(0,wys.sz);
    FOR(i,wys.sz)
    {
        PutInt(0,wys[i].fi.fi);
        PutInt(0,wys[i].fi.se);
        PutInt(0,wys[i].se);
    }
	Send(0);
    if (myid) return 0;
    vi fajne;
    for(set<int>::iterator it=rnodes.begin();it!=rnodes.end();it++)fajne.pb(*it);
    for(set<int>::iterator it=qnodes.begin();it!=qnodes.end();it++)fajne.pb(*it);
    sort(ALL(fajne));un(fajne);
    map<int,int> mem;
    FOR(i,fajne.sz) mem[fajne[i]]=i;
    int F=fajne.sz;
    vector<vi> d(F,vi(F,n+1));
    FOR(i,F)d[i][i]=0;
    FOR(i,m)
    {
			int od=Receive(-1);
			int ile=GetInt(od);
			//DBG(ile);
            FOR(j,ile)
            {
                int a=GetInt(od);
                int b=GetInt(od);
                int dyst=GetInt(od);
                //DBG(mp(mp(a,b),dyst));
                a=mem[a];
                b=mem[b];

                REMIN(d[a][b],dyst);
                REMIN(d[b][a],dyst);
            }
    }
    FOR(k,F)FOR(i,F)FOR(j,F)REMIN(d[i][j],d[i][k]+d[k][j]);
	REP(i,1,q)
    {
        int a=QueryFrom(i);
        int b=QueryTo(i);
        printf("%d\n",d[mem[a]][mem[b]]);
    }
    //DBG(vi(rn,rn+m));


    return 0;
}