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
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for (int i = (a); i <= (b); ++i)
#define REPD(i,a,b) for (int i = (a); i >= (b); --i)
#define FORI(i,n) REP(i,1,n)
#define FOR(i,n) REP(i,0,int(n)-1)
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define ll long long
#define SZ(x) int((x).size())
#define DBG(v) cerr << #v << " = " << (v) << endl;
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define fi first
#define se second

#include "message.h"
#include "skup.h"

const int max_ints = 15000;
const int N = 110;

int my_id, nodes;

void send_to(int who, vector<int> v) {
	int num = SZ(v);
	PutInt(who, num);
	int added = 1;
	FOR(i,num) {
		PutInt(who, v[i]);
		added++;
		if (added % max_ints == 0) {
			Send(who);
		}
	}
	if (added % max_ints != 0) {
		Send(who);
	}
}

void send0(int who, int scroot) {
	PutInt(who,0);
	PutInt(who, scroot);
	Send(who);
}

vector<int> get_from(int who, int cnt = 1) {
	Receive(who);
	int num = GetInt(who);
	if (num == 0) num = cnt;
	//printf("get from %d (%d numbers)\n", who, num); fflush(stdout);
	vi ret(num);
	int got = 1;
	FOR(i,num) {
		ret[i] = GetInt(who);
		got++;
		if (got % max_ints == 0 && i+1<num) {
			Receive(who);
		}
	}
	return ret;
}

int grsize;
int have[N];
int graph[N][N];
int bits[N*N+N+30];

vi compress() {
	FOR(i,nodes) bits[i] = have[i];
	FOR(i,nodes) FOR(j,nodes) bits[nodes + i*nodes + j] = graph[i][j];
	int q = grsize;
	vi ret(q/30);
	FOR(i,q/30) {
		FOR(j,30) ret[i] = ret[i]*2+bits[i*30+j];
	}
	return ret;
}

void decompress(vi gr) {
	int q = grsize;
	FOR(i,q/30) {
		FOR(j,30) {
			bits[i*30+29-j] = gr[i]%2;
			gr[i] /= 2;
		}
	}
	FOR(i,nodes) have[i] |= bits[i];
	FOR(i,nodes) FOR(j,nodes) graph[i][j] |= bits[nodes + i*nodes + j];
}


int vis[N];
bool wrong;

void dfs(int u, int col=1) {
	if (u<my_id) wrong=true;
	vis[u] = col;
	FOR(i,nodes) if (graph[u][i] && !vis[i]) {
		dfs(i, col+1);
	}
}

void dfsi(int u, int col=1) {
	if (u<my_id) wrong=true;
	vis[u] = col;
	FOR(i,nodes) if (graph[i][u] && !vis[i]) {
		dfs(i, col+1);
	}
}

vector<int> com;
int can[N];

int main() {
	my_id = MyNodeId();
	nodes = NumberOfNodes();
	grsize = nodes*nodes+nodes;
	while (grsize%30!=0) grsize++;
	int m = NumberOfCompanies();
	com.resize(m);
	FOR(i,m) com[i] = GetShareCost(i);
	
	FOR(i,nodes) if (i!=my_id) {
		send_to(i, {1});
	}
	FOR(i,nodes) if (i!=my_id) {
		can[i] = get_from(i)[0];
	}
	
	have[my_id] = 1;
	FOR(i,nodes) graph[i][my_id] = can[i];
	
	FOR(ii,nodes) {
		vi graph_compressed = compress();
		FOR(i,nodes) if (i!=my_id) {
			send_to(i, graph_compressed);
		}
		FOR(i,nodes) if (i!=my_id) {
			vi tmp = get_from(i, grsize/30);
			if (can[i]) {
				decompress(tmp);
			}
		}
	}
	
	dfs(my_id);
	
	bool root = 0;
	int nh = 0;
	FOR(i,nodes) nh += have[i];
	//printf("Node %d: knows %d\n", my_id, nh); fflush(stdout);
	
	if (nh==nodes && !wrong) {
		root = 1;
	}
	if (root) {
		//printf("I am root\n");
		fflush(stdout);
	}
	
	int upstream = -1, scroot = -1;
	if (!root) {
		upstream = Receive(-1);
		int tmp = GetInt(upstream);
		scroot = GetInt(upstream);
	} else {
		scroot = my_id;
	}
	//printf("upstream is %d, scroot is %d\n", upstream, scroot); fflush(stdout);
	
	if (!vis[upstream]) scroot = my_id;
	FOR(i,nodes) vis[i] = false;
	dfsi(scroot);
	//FOR(i,nodes) printf("%d ", vis[i]); printf("\n"); fflush(stdout);
	
	vector<int> child;
	FOR(i,nodes) if (graph[i][my_id] && vis[i]==vis[my_id]+1) child.pb(i);
	FOR(i,SZ(child)) {
		//printf("poking %d\n", child[i]); fflush(stdout);
		send0(child[i], scroot);
	}
	FOR(i,SZ(child)) {
		vi v = get_from(child[i], true);
		FOR(j,SZ(v)) com.pb(v[j]);
	}
	if (upstream != -1) send_to(upstream, com);
	if (root) {
		ll res=0;
		sort(com.begin(), com.end());
		int k = SZ(com);
		//printf("k=%d\n", k);
		//FOR(i,k) printf("%d ", com[i]);
		//printf("\n");
		FOR(i,k) res += 1LL * com[i] * (k-i);
		printf("%lld\n", res);
	}
	return 0;
}