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
// Author: Adam Krasuski

#include <cstdio>
#include "kollib.h"
#include "message.h"
#include <cstdlib>
#include <set>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdarg>

using namespace std;

/*
local\compile.bat main.cpp
local\run.bat 10 main.cpp.exe i.in

*/

struct seen_node{
    int number;
    int place;
};

int cmp(seen_node a,seen_node b){
    return a.place<b.place;
}

struct edge{
    int from;
    int to;
    int length;
};

int abs(int a){
    if(a>0){return a;}
    return -a;
}

#define DEBUG 0
void debug_printf(const char* pat,...){
    if(DEBUG){
        va_list args;
        va_start(args,pat);
        vprintf(pat,args);
        va_end(args);
    }
}

int main(){
    int N=NumberOfNodes();
    int id=MyNodeId();
    set<int>starting_nodes;
    set<int>query_nodes;
    int m=NumberOfQueries();
    for(int i=0;i<m;i++){
        query_nodes.insert(QueryFrom(i+1));
        query_nodes.insert(QueryTo(i+1));
    }

    if(id==0){
        int n=NumberOfStudents();
        N=min(N,n);//so that there is not an excess of computers
        srand(1337);//LEET
        int i=0;
        while(i<N){
            int x=rand()%n;
            x++;
            if(starting_nodes.count(x)==0){
                i++;
                starting_nodes.insert(x);
            }
        }
        for(int i=1;i<N;i++){
            for(set<int>::iterator it=starting_nodes.begin();it!=starting_nodes.end();it++){
                PutInt(i,*it);
            }
            Send(i);
        }
    }
    else{
        int n=NumberOfStudents();
        N=min(N,n);//so that there is not an excess of computers
        if(id>=N){return 0;}//there is an excess of computers
        Receive(0);
        for(int i=0;i<N;i++){
            starting_nodes.insert(GetInt(0));
        }
    }

    if(id==0&&DEBUG){
        debug_printf("Query nodes:\n");
        for(set<int>::iterator it=query_nodes.begin();it!=query_nodes.end();it++){
            debug_printf("%d ",*it);
        }
        debug_printf("\nChosen nodes:\n");
        for(set<int>::iterator it=starting_nodes.begin();it!=starting_nodes.end();it++){
            debug_printf("%d ",*it);
        }
    }

    int index=0;
    int my_starting_node;
    vector<seen_node>nodes_seen;//important nodes only

    int i=0;
    for(set<int>::iterator it=starting_nodes.begin();it!=starting_nodes.end();it++){
        if(i==id){
            my_starting_node=*it;
        }
        i++;
    }
    seen_node sn1={my_starting_node,0};
    nodes_seen.push_back(sn1);
    int current_node=my_starting_node;
    int previous_node=my_starting_node;
    while(1){
        int next;
        if(index==0){
            next=SecondNeighbor(my_starting_node);
        }
        else{
            if(SecondNeighbor(current_node)==previous_node){
                next=FirstNeighbor(current_node);
            }
            else{
                next=SecondNeighbor(current_node);
            }
        }

        index++;
        previous_node=current_node;
        current_node=next;

        if(id==0){
            debug_printf("At index %d, the node is %d.\n",index,current_node);
        }

        if(starting_nodes.count(current_node)>0){
            //push and break
            seen_node sn={current_node,index};
            nodes_seen.push_back(sn);
            break;
        }
        else if(query_nodes.count(current_node)>0){
            //push
            seen_node sn={current_node,index};
            nodes_seen.push_back(sn);
        }
    }

    current_node=my_starting_node;
    previous_node=my_starting_node;
    index=0;
    while(1){//this time, going the other direction
        int next;
        if(index==0){
            next=FirstNeighbor(my_starting_node);
        }
        else{
            if(SecondNeighbor(current_node)==previous_node){
                next=FirstNeighbor(current_node);
            }
            else{
                next=SecondNeighbor(current_node);
            }
        }
        index--;
        previous_node=current_node;
        current_node=next;

        if(id==0){
            debug_printf("At index %d, the node is %d.\n",index,current_node);
        }

        if(starting_nodes.count(current_node)>0){
            //push and break
            seen_node sn={current_node,index};
            nodes_seen.push_back(sn);
            break;
        }
        else if(query_nodes.count(current_node)>0){
            //push
            seen_node sn={current_node,index};
            nodes_seen.push_back(sn);
        }
    }

    sort(nodes_seen.begin(),nodes_seen.end(),cmp);
    vector<int>distances;
    for(int i=0;i<nodes_seen.size()-1;i++){
        distances.push_back(nodes_seen[i+1].place-nodes_seen[i].place);
    }
    PutInt(0,nodes_seen.size());
    PutInt(0,nodes_seen[0].number);
    for(int i=0;i<nodes_seen.size()-1;i++){
        PutInt(0,distances[i]);
        PutInt(0,nodes_seen[i+1].number);
    }
    Send(0);

    if(id==0){
        set<int>all_nodes=starting_nodes;
        all_nodes.insert(query_nodes.begin(),query_nodes.end());
        vector<int>to_long;
        map<int,int>to_short;
        for(set<int>::iterator it=all_nodes.begin();it!=all_nodes.end();it++){
            to_long.push_back(*it);
            to_short[*it]=to_long.size()-1;
            debug_printf("Node %d is shorthand for %d.\n",to_long.size()-1,*it);
        }
        //now all important nodes are translated into shorthand info, e.g. 0->a, 1->b, 2->c; where a,b,c are actual nodes long numbers
        int noin=to_long.size();//Number Of Important Nodes
        vector<edge>edges[noin];

        for(int i=0;i<N;i++){
            debug_printf("\nReceiving from node %d:\n",i);
            Receive(i);
            int k=GetInt(i);
            debug_printf("Nodes visited: %d\n",k);
            int a=GetInt(i);
            debug_printf("Visited nodes:\n%d",a);
            for(int j=0;j<k-1;j++){
                int d=GetInt(i);
                edge ed;
                ed.from=to_short[a];
                ed.length=d;
                a=GetInt(i);
                ed.to=to_short[a];
                if(edges[ed.from].size()==0|| (edges[ed.from].size()==1&&edges[ed.from][0].to!=ed.to) ){
                    edges[ed.from].push_back(ed);
                    int temp=ed.from;
                    ed.from=ed.to;
                    ed.to=temp;
                    edges[ed.from].push_back(ed);
                }
                debug_printf("-(d:%d)-%d",d,a);
            }
            debug_printf("\n");
        }
        debug_printf("Edges in the graph (shorthand notation):\n");
        for(int i=0;i<noin;i++){
            debug_printf("\nFrom node %d, there are following edges: ",i);
            for(int j=0;j<edges[i].size();j++){
                debug_printf("%d->%d:%d ",edges[i][j].from,edges[i][j].to,edges[i][j].length);
            }
        }
        debug_printf("\n");
        int positions[noin];
        positions[0]=0;//node of shorthand index 0 is at place 0

        int prev=edges[0][0].to;
        int cur=0;
        do{
            int next;
            int dist;
            if(edges[cur][0].to==prev){
                next=edges[cur][1].to;
                dist=edges[cur][1].length;
            }
            else{
                next=edges[cur][0].to;
                dist=edges[cur][0].length;
            }
            positions[next]=positions[cur]+dist;
            prev=cur;
            cur=next;

        }while(cur!=0);
        positions[0]=0;//node of shorthand index 0 is at place 0
        debug_printf("Positions of nodes:\n");
        for(int i=0;i<noin;i++){
            debug_printf("Node %d is at position %d.\n",i,positions[i]);
        }

        debug_printf("Answering queries:\n");
        for(int i=1;i<=m;i++){
            int j,k;
            j=QueryFrom(i);
            k=QueryTo(i);
            j=to_short[j];
            k=to_short[k];
            j=positions[j];
            k=positions[k];
            int dist=min( abs(k-j), NumberOfStudents()-abs(k-j) );
            debug_printf("For query %d, from %d to %d, the answer is %d.\n",i,QueryFrom(i),QueryTo(i),dist);
            printf("%d\n",dist);
        }

    }







}