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
#include"kollib.h"
#include"message.h"
#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>

enum
{
	MAX = 210,
};

struct node
{
	int left, right;
	int dis_left, dis_right;
	node() : left(0), right(0), dis_left(0), dis_right(0) {}
};

std::map<int,node> graph;
std::set<int> rands;

void add_edge(int a, int b, int v)
{
	if(graph[a].left && graph[a].left != b)
	{
		graph[a].right = b;
		graph[a].dis_right = v;
	}
	else
	{
		graph[a].left = b;
		graph[a].dis_left = v;
	}
}

int min(int a, int b) { return (a<b)?a:b; }

int main()
{
	int instance_id = MyNodeId(), instance_num = NumberOfNodes();
	for(int i = 0; i < NumberOfQueries(); i++)
	{
		graph[QueryFrom(i+1)] = node();
		graph[QueryTo(i+1)] = node();
	}
	srand(1324);
	int beginning = -1;
	for(int i = 0; i < instance_num-1; i++)
	{
		int r = random();
		r = (r<0)?-r:r;
		int x = r%NumberOfStudents()+1;
		graph[x] = node();
		rands.insert(x);
		if(instance_id == i+1) {beginning = x;}
	}

	if(instance_id == 0)
	{
		int working = (instance_num-1)*2;
		while(working)
		{
			int source = Receive(-1);
			int from = GetInt(source);
			int to = GetInt(source);
			int val = GetInt(source);
			add_edge(from,to,val);
			add_edge(to,from,val);
			if(rands.find(to) != rands.end()) working--;
		}
		
		for(int i = 0; i < NumberOfQueries(); i++)
		{
			int beg = QueryFrom(i+1);
			int cur = beg;
			int dis = 0;
			int tar = QueryTo(i+1);
			while(cur != tar)
			{
				if(beg == graph[cur].left)
				{
					dis += graph[cur].dis_right;
					beg = cur;
					cur = graph[cur].right;
				}
				else
				{
					dis += graph[cur].dis_left;
					beg = cur;
					cur = graph[cur].left;
				}
			}
			printf("%d\n", min(dis,NumberOfStudents()-dis));
		}
	}
	else
	{
		int t_beg = 0, t_end = 2;
		int from[2]; from[0] = from[1] = beginning;
		int cur[2]; cur[0] = cur[1] = beginning;
		int dis[2]; dis[0] = dis[1] = 0;
		int prev[2];
		prev[0] = FirstNeighbor(beginning);
		prev[1] = SecondNeighbor(beginning);
		while(t_beg != t_end)
			for(int i = t_beg; i < t_end; i++)
			{	
				int f = FirstNeighbor(cur[i]);
				if(f == prev[i])
				{
					prev[i] = cur[i];
					cur[i] = SecondNeighbor(cur[i]);
				}
				else
				{
					prev[i] = cur[i];
					cur[i] = f;
				}
				dis[i]++;
				if(graph.find(cur[i])!=graph.end())
				{
					PutInt(0,from[i]);
					PutInt(0,cur[i]);
					PutInt(0,dis[i]);
					Send(0);
					from[i] = cur[i];
					dis[i] = 0;
					if(rands.find(cur[i])!=rands.end())
					{
						if(i == 0) 
							t_beg++;
						else
							t_end--;
					}
				}
			}
	}

	return 0;
}