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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
//I could have taken more class-based approach...

#include "message.h"
#include "sabotaz.h"

#include <iostream>
#include <sstream>
#include <cstdio>
#include <vector>

// #define DEBUG

using namespace std;

const int INF = 1000*1000*1000;
const int MAX_N = 200005;
const int MAX_INSTANCES=105;
const int MAX_MSG = 50001;
const int END_MSG = -1000000000;

enum JOB {
	JOB_FIRST_PHASE_WORKER,
	JOB_FIRST_PHASE_SUPERVISOR,
	JOB_MASTER,
	JOB_SECOND_PHASE_WORKER,
	JOB_SECOND_PHASE_SUPERVISOR,
	JOB_NONE
};

//Compute at startup 'constants':
int INSTANCES;
int MY_ID;
int N, M;
int WORKERS;
JOB MY_JOB;
int MY_MASTER;

//Used by workers only:
int INTERVAL_LEN;
int MY_START, MY_END;

//Used by supervisors only:
vector<int> MY_SLAVES;

//Used by master only:
vector<int> FIRST_PHASE_SUPERVISORS;
vector<int> SECOND_PHASE_SUPERVISORS;

int startM(int workerId) {
	return workerId*INTERVAL_LEN;
}
int endM(int workerId) {
	return (workerId == WORKERS*WORKERS-1 ? M-1 : (workerId+1)*INTERVAL_LEN-1);
}


void init() {
	INSTANCES = NumberOfNodes();
	MY_ID = MyNodeId();
	N = NumberOfIsles();
	M = NumberOfBridges();
	
	WORKERS = 6;
	while (2*(WORKERS+WORKERS*WORKERS)+1 > INSTANCES)
		--WORKERS;
	
	INTERVAL_LEN = M/(WORKERS*WORKERS);
	
	/*Workers -> used instances:
	 * 6 -> 83
	 * 5 -> 61
	 * 4 -> 41
	 * 3 -> 25
	 * 2 -> 13
	 * 1 -> 5
	 * This load management probably can be improved...
	 */
	
	int tmpId;
	
	tmpId = MY_ID;
	if (tmpId < WORKERS*WORKERS) {
		MY_JOB = JOB_FIRST_PHASE_WORKER;
		MY_MASTER = WORKERS*WORKERS + (tmpId/WORKERS);
		MY_START = startM(tmpId);
		MY_END = endM(tmpId);
		return;
	}
	
	tmpId -= WORKERS*WORKERS;
	if (tmpId < WORKERS) {
		MY_JOB = JOB_FIRST_PHASE_SUPERVISOR;
		MY_MASTER = WORKERS*WORKERS+WORKERS;
		for (int i=0; i<WORKERS; ++i) {
			MY_SLAVES.push_back(tmpId*WORKERS+i);
		}
		return;
	}
	
	tmpId -= WORKERS;
	if (tmpId == 0) {
		MY_JOB = JOB_MASTER;
		for (int i=0; i<WORKERS; ++i) {
			FIRST_PHASE_SUPERVISORS.push_back(WORKERS*WORKERS+i);
			SECOND_PHASE_SUPERVISORS.push_back(WORKERS*WORKERS+WORKERS+1+i);
		}
		return;
	}
	
	tmpId -= 1;
	if (tmpId < WORKERS) {
		MY_JOB = JOB_SECOND_PHASE_SUPERVISOR;
		MY_MASTER = WORKERS*WORKERS+WORKERS;
		for (int i=0; i<WORKERS; ++i) {
			MY_SLAVES.push_back(WORKERS*WORKERS+WORKERS+1+WORKERS+WORKERS*tmpId+i);
		}
		return;
	}
	
	tmpId -= WORKERS;
	if (tmpId < WORKERS*WORKERS) {
		MY_JOB = JOB_SECOND_PHASE_WORKER;
		MY_MASTER = WORKERS*WORKERS+WORKERS+1+(tmpId/WORKERS);
		MY_START = startM(tmpId);
		MY_END = endM(tmpId);
		return;
	}
	
	MY_JOB=JOB_NONE;
	return;
}

void debugIntroduceYourself() {
	stringstream ss;
	
	ss << "Instance " << MY_ID << " -> MY_JOB=";
	
	if (MY_JOB == JOB_FIRST_PHASE_WORKER) {
		ss << "FIRST_PHASE_WORKER   ;   MY_MASTER = " << MY_MASTER << "   ;   MY_START = " << MY_START << "   ;   MY_END = " << MY_END << "\n";
	} else if (MY_JOB == JOB_FIRST_PHASE_SUPERVISOR) {
		ss << "FIRST_PHASE_SUPERVISOR   ;   MY_MASTER = " << MY_MASTER << "   ;   MY_SLAVES = ";
		for (vector<int>::iterator vit=MY_SLAVES.begin(); vit!=MY_SLAVES.end(); ++vit)
			ss << (*vit) << ", ";
		ss << "\n";
	} else if (MY_JOB == JOB_MASTER) {
		ss << "MASTER   ;   FIRST_PHASE_SUPERVISORS = ";
		for (vector<int>::iterator vit=FIRST_PHASE_SUPERVISORS.begin(); vit!=FIRST_PHASE_SUPERVISORS.end(); ++vit)
			ss << (*vit) << ", ";
		ss << "   ;   SECOND_PHASE_SUPERVISORS = ";
		for (vector<int>::iterator vit=SECOND_PHASE_SUPERVISORS.begin(); vit!=SECOND_PHASE_SUPERVISORS.end(); ++vit)
			ss << (*vit) << ", ";
		ss << "\n";
	} else if (MY_JOB == JOB_SECOND_PHASE_SUPERVISOR) {
		ss << "SECOND_PHASE_SUPERVISOR   ;   MY_MASTER = " << MY_MASTER << "   ;   MY_SLAVES = ";
		for (vector<int>::iterator vit=MY_SLAVES.begin(); vit!=MY_SLAVES.end(); ++vit)
			ss << (*vit) << ", ";
		ss << "\n";
	} else if (MY_JOB == JOB_SECOND_PHASE_WORKER) {
		ss << "SECOND_PHASE_WORKER   ;   MY_MASTER = " << MY_MASTER << "   ;   MY_START = " << MY_START << "   ;   MY_END = " << MY_END << "\n";
	} else if (MY_JOB == JOB_NONE) {
		ss << "NONE\n";
	}
	
	cerr << ss.str();
}


//#########################################################################
//Comms:
//#########################################################################


void sendVect(const vector<int>& res, int whom) {
	
	#ifdef DEBUG
		fprintf(stderr, "Instance %d sending res of size %d to %d\n", MY_ID, (int)res.size(), whom);
	#endif
	
	//PutInt(whom, res.size());
	for (int i=0; i<res.size(); ++i) {
		PutInt(whom, res[i]);
		
		if ((i+1)%MAX_MSG==0)
			Send(whom);
	}
	
	PutInt(whom, END_MSG);
	Send(whom);
}

void receiveVect(vector<int>& res, int whom) {
	#ifdef DEBUG
		fprintf(stderr, "Instance %d receiving res from %d\n", MY_ID, whom);
	#endif
	res.clear();
	
	int received=0;
	int val;
	
	while(true) {
		if (received%MAX_MSG==0)
			Receive(whom);
		
		val = GetInt(whom);
		++received;
		
		if (val == END_MSG)
			break;
		
		res.push_back(val);
	}
}



//#########################################################################
//FIRST PHASE - finding spanning tree
//#########################################################################

int find(vector<int>& p, int v) {
	return (p[v] == v ? v : p[v] = find(p, p[v]));
}

bool unify(vector<int>& p, int v1, int v2) {
	int p1 = find(p, v1);
	int p2 = find(p, v2);
	if (p1 == p2)
		return false;
	
	p[p1] = p2;
	return true;
}

void initFindUnify(vector<int>& p) {
	p.clear();
	for (int i=0; i<N; ++i)
		p.push_back(i);
}



void mainFirstPhaseWorker() {
	vector<int> res;
	
	vector<int> p;
	initFindUnify(p);
	
	int a, b;
	
	for (int i=MY_START; i<=MY_END; ++i) {
// 		fprintf(stderr, "Instance %d asking for bridge\n", MY_ID);
		a = BridgeEndA(i);
		b = BridgeEndB(i);
		
// 		fprintf(stderr, "Instance %d got bridge: %d %d\n", MY_ID, a, b);
		if (unify(p, a, b)) {
// 			fprintf(stderr, "Instance %d got unify\n", MY_ID);
			res.push_back(i);	//This edge is part of the spanning tree
// 			fprintf(stderr, "Instance %d pushed\n", MY_ID);
		}
	}
	
	
		
	sendVect(res, MY_MASTER);
}

void receiveFindUnifyData(vector<int>& res, vector<int>& slavesVect) {
	vector<int> p;
	initFindUnify(p);
	
	for (vector<int>::iterator slave=slavesVect.begin(); slave!=slavesVect.end(); ++slave) {
		#ifdef DEBUG
			fprintf(stderr, "Instance %d receiving res from slave %d\n", MY_ID, *slave);
		#endif
		
		vector<int> tmp;
		int a, b;
		
		receiveVect(tmp, *slave);
		
		for (vector<int>::iterator vit=tmp.begin(); vit!=tmp.end(); ++vit) {
			a = BridgeEndA(*vit);
			b = BridgeEndB(*vit);
			
			if (unify(p, a, b)) {
				res.push_back(*vit);
			}
		}
	}
}

void mainFirstPhaseSupervisor() {
	vector<int> res;
	
	receiveFindUnifyData(res, MY_SLAVES);
	
	#ifdef DEBUG
		fprintf(stderr, "Instance %d sending res to %d\n", MY_ID, MY_MASTER);
	#endif
	
	sendVect(res, MY_MASTER);
}




//#########################################################################
//MASTER:
//#########################################################################

//Local declaration caused a segfault
vector<int> edges[MAX_N];
vector<int> edgesIds[MAX_N];
// int edgeCount[MAX_N]={};
// bool inQueue[MAX_N]={};
// vector<int> processingQueue;
vector<int> order;
vector<int> size;
vector<int> nodeParent;
vector<int> edgeIdToParent;

//Generated and gathered from workers:
vector<int> low, high;

// struct dfsFrame {
// 	
// };

// void dfsIterative(int node, int parent, int ordNum) {
// 	
// }

void dfs(int node, int parent, int ordNum) {
	nodeParent[node]=parent;
	order[node]=ordNum;
	
	for (int i=0; i<edges[node].size(); ++i) {
		int child = edges[node][i];
		
		if (child == parent)
			continue;
		
		edgeIdToParent[child] = edgesIds[node][i];
		dfs(child, node, ordNum+1);
		size[node] += size[child];
		ordNum += size[child];
	}
	
	++size[node];
}

void genTreeData(vector<int>& spanningForestEdges) {
	int a, b;
	for (vector<int>::iterator vit=spanningForestEdges.begin(); vit!=spanningForestEdges.end(); ++vit) {
		a = BridgeEndA(*vit);
		b = BridgeEndB(*vit);
		
		edges[a].push_back(b);
		edgesIds[a].push_back(*vit);
		edges[b].push_back(a);
		edgesIds[b].push_back(*vit);
	}
	
// 	fprintf(stderr, "Instance %d, checkpoint 1\n", MY_ID);
	
	for (int i=0; i<N; ++i) {
		nodeParent.push_back(-1);
		edgeIdToParent.push_back(-1);
		order.push_back(INF);
		size.push_back(0);
		
		low.push_back(INF);
		high.push_back(-1);
	}
	
// 	fprintf(stderr, "Instance %d, checkpoint 2\n", MY_ID);
	
	for (int i=0; i<N; ++i) {
		if (order[i] == INF)
			dfs(i, -1, 1);
		
		low[i] = high[i] = order[i];
	}
// 	fprintf(stderr, "Instance %d, checkpoint 3\n", MY_ID);
}

void dfsLowHigh(int node, int parent) {
	for (int i=0; i<edges[node].size(); ++i) {
		int child = edges[node][i];
		
		if (child == parent)
			continue;
		
		dfsLowHigh(child, node);
		
		high[node] = max(high[node], high[child]);
		low[node] = min(low[node], low[child]);
	}
}


void gatherLowHigh(vector<int>& SLAVES) {
	vector<int> tmpV;
	
	for (vector<int>::iterator slave=SLAVES.begin(); slave!=SLAVES.end(); ++slave) {
		receiveVect(tmpV, *slave);
		
		for (int i=0; i<N; ++i) {
			if (low[i] > tmpV[i])
				low[i] = tmpV[i];
		}
		
		
		receiveVect(tmpV, *slave);
		
		for (int i=0; i<N; ++i) {
			if (high[i] < tmpV[i])
				high[i] = tmpV[i];
		}
	}
}


void mainMaster() {
	vector<int> spanningForestEdges;
	
	receiveFindUnifyData(spanningForestEdges, FIRST_PHASE_SUPERVISORS);
	
	#ifdef DEBUG
		fprintf(stderr, "Spanning forest:\n");
		for (int i=0; i<spanningForestEdges.size(); ++i) {
			fprintf(stderr, "   %d %d\n", (int)BridgeEndA(spanningForestEdges[i]), (int)BridgeEndB(spanningForestEdges[i]));
		}
	#endif
	
	for (vector<int>::iterator slave=SECOND_PHASE_SUPERVISORS.begin(); slave!=SECOND_PHASE_SUPERVISORS.end(); ++slave) {
		sendVect(spanningForestEdges, *slave);
	}
	
	#ifdef DEBUG
		fprintf(stderr, "Master generating tree data...\n");
	#endif
		
	genTreeData(spanningForestEdges);
	
	#ifdef DEBUG
		fprintf(stderr, "Master generated tree data...\n");
	#endif
	
	#ifdef DEBUG
		fprintf(stderr, "Order:\n");
		for (int i=0; i<N; ++i) {
			fprintf(stderr, "   node %d -> %d\n", i, order[i]);
		}
		
		fprintf(stderr, "Edge to parent:\n");
		for (int i=0; i<N; ++i) {
			if (nodeParent[i]==-1)
				fprintf(stderr, "   node %d -> root\n", i);
			else
				fprintf(stderr, "   node %d -> %d %d\n", i, (int)BridgeEndA(edgeIdToParent[i]), (int)BridgeEndB(edgeIdToParent[i]));
		}
		
		fprintf(stderr, "Size:\n");
		for (int i=0; i<N; ++i) {
			fprintf(stderr, "   node %d -> size: %d\n", i, size[i]);
		}
		
	#endif

	
	//Final processing:
	
	gatherLowHigh(SECOND_PHASE_SUPERVISORS);
	
	#ifdef DEBUG
		fprintf(stderr, "HighLow1:\n");
		for (int i=0; i<N; ++i) {
			fprintf(stderr, "   node %d -> high=%d  low=%d\n", i, high[i], low[i]);
		}
	#endif
	
	for (int i=0; i<N; ++i) {
		if (edgeIdToParent[i] == -1)
			dfsLowHigh(i, -1);
	}
	
	#ifdef DEBUG
		fprintf(stderr, "HighLow2:\n");
		for (int i=0; i<N; ++i) {
			fprintf(stderr, "   node %d -> high=%d  low=%d\n", i, high[i], low[i]);
		}
	#endif
	
	
	int res=0;
	
	for (int i=0; i<N; ++i) {
		if (edgeIdToParent[i] == -1)
			continue;
		
		if (low[i] == order[i] && high[i] < order[i]+size[i])
			++res;
	}
	
	printf("%d\n", res);
	
}




//#########################################################################
//SECOND PHASE - finding bridges
//#########################################################################


// void initLowHigh() {
// 	for (int i=0; i<N; ++i) {
// 		low.push_back(INF);
// 		high.push_back(-2);
// 	}
// }


void mainSecondPhaseSupervisor() {
	vector<int> spanningForestEdges;
	
	receiveVect(spanningForestEdges, MY_MASTER);
	
	for (vector<int>::iterator slave=MY_SLAVES.begin(); slave!=MY_SLAVES.end(); ++slave) {
		sendVect(spanningForestEdges, *slave);
	}
	
	genTreeData(spanningForestEdges);	//Probably no need for this. Just init high & low
	
	//Gather:
	gatherLowHigh(MY_SLAVES);
	
	sendVect(low, MY_MASTER);
	sendVect(high, MY_MASTER);
}


void mainSecondPhaseWorker() {
	vector<int> spanningForestEdges;
	
// 	fprintf(stderr, "Instance %d will receive spanningForestEdges\n", MY_ID);
	receiveVect(spanningForestEdges, MY_MASTER);
// 	fprintf(stderr, "Instance %d received spanningForestEdges\n", MY_ID);
	
	genTreeData(spanningForestEdges);
	
	
// 	fprintf(stderr, "Instance %d generated tree data\n", MY_ID);
	
	
	int a, b, aOrd, bOrd;
	for (int i=MY_START; i<=MY_END; ++i) {
		a = BridgeEndA(i);
		b = BridgeEndB(i);
		
		aOrd = order[a];
		bOrd = order[b];
		
		if (edgeIdToParent[a] != i) {
			if (low[a] > bOrd) {
				low[a] = bOrd;
				#ifdef DEBUG
					fprintf(stderr, "Improving %d node low to %d\n", a, low[a]);
				#endif
			}
			if (high[a] < bOrd) {
				high[a] = bOrd;
				#ifdef DEBUG
					fprintf(stderr, "Improving %d node high to %d\n", a, high[a]);
				#endif
			}	
		}
		
		if (edgeIdToParent[b] != i) {
			if (low[b] > aOrd) {
				low[b] = aOrd;
				#ifdef DEBUG
					fprintf(stderr, "Improving %d node low to %d\n", b, low[b]);
				#endif
			}
			if (high[b] < aOrd) {
				high[b] = aOrd;
				#ifdef DEBUG
					fprintf(stderr, "Improving %d node high to %d\n", b, high[b]);
				#endif
			}
		}
	}
	
	sendVect(low, MY_MASTER);
	sendVect(high, MY_MASTER);
}



//#########################################################################
//MAIN:
//#########################################################################
int main() {
	init();
	
	#ifdef DEBUG
		debugIntroduceYourself();
	#endif
	
	switch(MY_JOB) {
		case JOB_FIRST_PHASE_WORKER:
			mainFirstPhaseWorker();
			break;
		case JOB_FIRST_PHASE_SUPERVISOR:
			mainFirstPhaseSupervisor();
			break;
		case JOB_MASTER:
			mainMaster();
			break;
		case JOB_SECOND_PHASE_SUPERVISOR:
			mainSecondPhaseSupervisor();
			break;
		case JOB_SECOND_PHASE_WORKER:
			mainSecondPhaseWorker();
			break;
		case JOB_NONE:
			break;
	}
	
	return 0;
}