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
//#include "sabotaz-test.h"
#include "sabotaz.h"
#include "message.h"

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <cassert>
#include <cstring>
#include <ext/numeric>
using namespace std ;
using namespace __gnu_cxx ;
typedef long long LL ;
typedef pair<int,int> PII ;
typedef vector<int> VI ;
const int INF = 1e9+100 ;
const LL INFLL = (LL)INF * (LL)INF ;
#define REP(i,n) for(i=0;i<(n);++i)
#define ALL(c) c.begin(),c.end()
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define CLEAR(t) memset(t,0,sizeof(t))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second

template<class T1,class T2> ostream& operator<<(ostream &s, const pair<T1,T2> &x) { s<< "(" << x.FI << "," << x.SE << ")"; return s;}
template<class T>           ostream& operator<<(ostream &s, const vector<T>   &t) { FOREACH(it, t) s << *it << " " ; return s ; }
template<class T>           ostream& operator<<(ostream &s, const set<T>      &t) { FOREACH(it, t) s << *it << " " ; return s ; }
template<class T1,class T2> ostream& operator<<(ostream &s, const map<T1, T2> &t) { FOREACH(it, t) s << *it << " " ; return s ; }

///////////////////////////////////////////

const int MAXN = 200100 ;

VI G[MAXN] ;

int d[MAXN] ; int nextNr=1 ; // czasy odwiedzenia
int low[MAXN] ;

vector<PII> bridges ;
vector<PII> rest ;
void addRest(const VI &c) {
	//cout << "c = " << c << endl ;
	if(c.size()<=1) return ;
	for(int i=0 ; i+1<c.size() ; i++)
		rest.PB(MP(c[i],c[i+1])) ;
	rest.PB(MP((int)c.back(), c[0])) ;
}

VI stack ;

void DFS(int u, int p) {
	stack.PB(u) ;
	low[u] = d[u] = nextNr++ ;
	bool canGoToParent=false ; // w razie krawedzi wielokrotnych
	FOREACH(it, G[u]) {
		if(*it==p && !canGoToParent) {
			canGoToParent = true ;
			continue ;
		}
		if(d[*it]==0) { // nieodwiedzone
			DFS(*it, u) ;
			low[u] = min(low[u], low[*it]) ;
			if(low[*it]>d[u]) {
				bridges.PB(MP(u,*it)) ;
				
				VI c ;
				while(stack.back() != *it) {
					c.PB(stack.back()) ;
					stack.pop_back() ;
					
					assert(!stack.empty()) ;
				}
				c.PB(*it) ;
				stack.pop_back() ;
				addRest(c) ;
			}
		}
		else low[u] = min(low[u], d[*it]) ;
	}
}

void calculateBridge(int N) {
	CLEAR(d) ;
	CLEAR(low) ;
	nextNr=1 ;
	stack.clear() ;
	bridges.clear() ;
	rest.clear() ;
	int i ;
	REP(i,N)
		if(!d[i]) {
			if(!G[i].empty()) {
				DFS(i, -1) ;
				addRest(stack) ;
				stack.clear() ;
			}
		}
}

inline void addEdge(int a, int b) {
	G[a].PB(b) ;
	G[b].PB(a) ;
}

inline int ceil(int a, int b) {
	return (a+b-1)/b ;
}

const int LIMIT=32700 ;
void __send(int address, const vector<PII> &message) {
	const int parts = ceil(message.size(),LIMIT) ;
	PutInt(address, parts) ; 	// liczba czesci, idzie pierwsza wiadomoscia
	Send(address) ;
	
	for(int i=0 ; i<message.size() ; i+=LIMIT) {
		int len = min(LIMIT, (int)message.size()-i) ;
		PutInt(address, len) ;
		for(int j=i ; j<i+len ; j++) {
			PutInt(address, message[j].FI) ;
			PutInt(address, message[j].SE) ;
		}
		Send(address) ;
	}
}

void __receive(int address) {
	Receive(address) ;
	int parts = GetInt(address) ;
	while(parts--) {
		Receive(address) ;
		int m = GetInt(address), a, b ;
		while(m--) {
			a = GetInt(address) ;
			b = GetInt(address) ;
			addEdge(a,b) ;
		}
	}
}

int main()
{
	ios_base::sync_with_stdio(0) ;
	//if(MyNodeId()>0) return 0 ;
	
	const LL N = NumberOfIsles() ;
	LL M = NumberOfBridges() ;
	int a, b ;
	LL start = M*    MyNodeId()/NumberOfNodes() ;
	LL   end = M*(MyNodeId()+1)/NumberOfNodes() ;
	for(LL i=start ; i<end ; i++) {
		a = BridgeEndA(i) ;
		b = BridgeEndB(i) ;
		addEdge(a,b) ;
	}
	
	calculateBridge(N) ;
	
	int i, j ;
	VI workers(NumberOfNodes()) ;	// do scalania, debilne, ale nie chce mi sie myslec
	REP(i, workers.size())
		workers[i]=i ;
	
	while(workers.size()>1) {
		REP(i, workers.size()) {
			if(workers[i]==MyNodeId()) {
				if(i&1) { // wysylanie
					rest.insert(rest.end(), ALL(bridges)) ;
					__send(workers[i-1], rest) ;
					
					return 0 ;
				}
				else if(i+1<workers.size()) {
					REP(j, N) {
						(VI()).swap(G[j]) ;
						assert(G[j].empty()) ;
					}
					FOREACH(it, rest)
						addEdge(it->FI, it->SE) ;
					FOREACH(it, bridges)
						addEdge(it->FI, it->SE) ;
					
					__receive(workers[i+1]) ;
					
					calculateBridge(N) ;
				}
			}
		}
		VI tmp ;
		for(i=0 ; i<workers.size() ; i+=2)
			tmp.PB(workers[i]) ;
		workers = tmp ;
	}
	
	assert(MyNodeId()==0) ;
	cout << bridges.size() << endl ;
}