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

class Deep{
public:
    int i;
    int d;
    Deep(int a, int b){
        i = a;
        d = b;
    }
};

int main(){
    if(MyNodeId() == 0){
        set <int> s;
        for(int i=1 ; i<=NumberOfQueries() ; i++){
            s.insert(QueryFrom(i));
            s.insert(QueryTo(i));
        }
        map <int,int> m;
        int start = *(s.begin());
        m[start] = 0;
        Deep parent(start , 0);
        Deep son(FirstNeighbor(start) , 1);

        while(son.i != start){
            if(s.find(son.i) != s.end())
                m[son.i] = son.d;

            if(FirstNeighbor(son.i) != parent.i){
                parent = son;
                son.i = FirstNeighbor(son.i);
                son.d++;
            }
            else{
                parent = son;
                son.i = SecondNeighbor(son.i);
                son.d++;
            }
        }

        int x,y,dx,dy,odp;

        for(int i=1 ; i<=NumberOfQueries() ; i++){
            x = QueryFrom(i);
            y = QueryTo(i);
            dx = m.find(x)->second;
            dy = m.find(y)->second;
            odp = abs(dx-dy);
            cout << min( odp , NumberOfStudents() - odp ) << endl;
        }
    }
	return 0;
}