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
#include <bits/stdc++.h>
#include "message.h"
#include "sabotaz.h"
using namespace std;

#define fru(j,n) for(int j=0; j<(n); ++j)
#define tr(it,v) for(auto it=(v).begin(); it!=(v).end(); ++it)
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define ALL(G) (G).begin(),(G).end()

//#define DEBUG
#ifdef DEBUG
	#define DEB printf
#else
	#define DEB(...)
#endif


typedef long long ll;
typedef double D;
typedef pair<int,int> pii;
typedef vector<int> vi;

const int inft = 1000000009;
const int MAXN = 1000006,BLOK=30000;

int myid,n,m,nodes;
//kod (namespace LOW) z biblioteczki druzyny UWr1
namespace LOW {
	int n,tin[MAXN],F[MAXN],dfsTime,nr; //IN: n
	vi bridges,G;
	vector<pii> E,adj[MAXN];
	void add_edge (int a, int b) {//IN: Do dodawania krawedzi
		adj[a].pb(pii(b,nr));
		adj[b].pb(pii(a,nr));
		E.pb(pii(a,b));
		nr++;
	}

	int bccDFS (int u,pii skad=pii(-1,-1)) {
		int lo = tin[u] = ++dfsTime;
		tr(it,adj[u]) {
			if(*it==skad)continue;
			if (!tin[it->x]) {
				int on = bccDFS(it->x,pii(u,it->y));
				lo = min(on, lo);
				if(on>tin[u]){bridges.pb(it->y);}
			} else lo = min(lo, tin[it->x]);
		}
		return lo;
	}
	int Find(int x){
		if(F[x]==x)return x;
		return F[x]=Find(F[x]);
	}
	void Union(int x,int y){
		x=Find(x);y=Find(y);
		F[y]=x;
	}
	void computeBCC(){ //to trzeba odpalic po dodaniu wszystkich krawedzi
		fill(tin, tin+n+1, 0);
		dfsTime = 1;
		fru(i,n) if(!tin[i]) bccDFS(i); 
		//get components
		sort(ALL(bridges));
		int iter=0;
		fru(i,n)F[i]=i;
		fru(i,nr){
			if(iter<bridges.size() && bridges[iter]==i){iter++;continue;}
			if(Find(E[i].x)!=Find(E[i].y)){G.pb(i);Union(E[i].x,E[i].y);}
		}
	}//UWAGA::trzeba pamietac o krawedziach wielokrotnych, ktore nie sa mostami	
}	

void GetMyData(){
	//init bridge structure
	for(int i=myid-1;i<m;i+=nodes){
		int a,b;
		a=BridgeEndA(i);
		b=BridgeEndB(i);
		LOW::add_edge(a,b);
		DEB("(%d,%d)\n",a,b);
	}
	DEB("inst %d m=(%d) koniec GetMyData() %d\n",myid,m,LOW::E.size());
}
void BridgeFind(){
	//execute bridge structure
	//create two vectors bridges, components
	LOW::computeBCC();
	DEB("inst %d koniec BridgeFind() %d %d\n",myid,LOW::bridges.size(),LOW::G.size());
}
void GetMyGraph(){
	//create new graph based on G && bridges
	vector<pii> E2;
	swap(E2,LOW::E);
	LOW::E.clear();
	fru(i,n)LOW::adj[i].clear();
	LOW::nr=0;
	tr(it,LOW::G)fru(j,2)LOW::add_edge(E2[*it].x,E2[*it].y);
	tr(it,LOW::bridges)LOW::add_edge(E2[*it].x,E2[*it].y);
	DEB("inst %d koniec GetMyGraph() BR%d G %d\n",myid,LOW::bridges.size(),LOW::G.size());
	LOW::bridges.clear();
	LOW::G.clear();
}

void GetChildData(int child){
	child--;
	//augment bridge structure with child data
	int ile=0,nr=0;
	while(ile<2){
		if(nr%BLOK==0){Receive(child);}
		nr++;
		int a=GetInt(child),b=GetInt(child);
		if(pii(a,b)==pii(-1,-1)){ile++;continue;}
		if(ile==0)LOW::add_edge(a,b);
		else fru(j,2) LOW::add_edge(a,b);
		DEB("get (%d,%d) nr%d\n",a,b,LOW::nr);
	}
	DEB("inst %d koniec GetChildData() %d\n",myid,child+1);
}
void SendData(int parent){
	parent--;
	//send bridge structure to parent
	vector<pii> M;
	tr(it,LOW::bridges)M.pb(LOW::E[*it]);
	M.pb(pii(-1,-1));
	tr(it,LOW::G)M.pb(LOW::E[*it]);
	M.pb(pii(-1,-1));
	fru(i,M.size()){
		if(i%BLOK==0 && i){Send(parent);}
		DEB("put %d, %d\n",M[i].x,M[i].y);
		PutInt(parent,M[i].x);
		PutInt(parent,M[i].y);
	}
	Send(parent);
	DEB("inst %d koniec SendData() %d, NR%d \n",myid,parent+1,M.size());
}

int Answer(){
	//get number of bridges
	DEB("inst %d Answer()\n",myid);
	return LOW::bridges.size();
}
int main() {
	myid=MyNodeId();myid++;
	nodes=NumberOfNodes();nodes;
	n=NumberOfIsles();
	m=NumberOfBridges();
	LOW::nr=0;
	LOW::n=n;

	//wczytaj swoja czesc
	GetMyData();
	//odpal szukanie mostow
	BridgeFind();
	// jesli masz dzieci czekaj na dane
	bool newdata=false;
	for(int child=2*myid;child<min(2*myid+2,nodes+1);child++){
		if(!newdata)GetMyGraph();
		GetChildData(child);
		newdata=true;
	}
	if(newdata)BridgeFind();
	if(myid>1){
		//send to parent
		SendData(myid/2);
	}
	else{
		printf("%d\n",Answer());
	}
	return 0;
}