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
#include<bits/stdc++.h>
#define PII pair<int,int>
#define f first
#define s second
#define VI vector<int>
#define LL long long
#define MP make_pair
#define LD long double
#define PB push_back
#define ALL(V) V.begin(),V.end()
#define abs(x) max((x),-(x))
#define PDD pair<LD,LD> 
#define VPII vector< PII > 
#define siz(V) ((int)V.size())
#define FOR(x, b, e)  for(int x=b;x<=(e);x++)
#define FORD(x, b, e) for(int x=b;x>=(e);x--)
#define REP(x, n)     for(int x=0;x<(n);x++)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
using namespace std;
#include "sabotaz.h"
#include "message.h"
int NODES,ID,N,M,n,m;
const int MXN=2e5+5;

namespace mostuj
	{
	VI V[MXN];
	int low[MXN];
	int czas[MXN];
	char stan[MXN];//0-nieruszony, 1-nastosie, 2-zdjęcty
	int scc[MXN];
	int TIME=1;
	int SPOJNE=0;
	VPII mosty;
	VI S;
	void dfs(int v,int pop)
		{
		czas[v]=low[v]=TIME++;
		stan[v]=1;
		S.PB(v);
		bool j=0;
		REP(i,siz(V[v]))
		  {
		  if(V[v][i]==pop&&!j){j=1;continue;}
		  if(stan[V[v][i]]==0)
		    {
		    dfs(V[v][i],v);
		    low[v]=min(low[v],low[V[v][i]]);
		    }
		  if(stan[V[v][i]]==1)
		    {
		    low[v]=min(low[v],czas[V[v][i]]);
		    }
		  }
		
		if(low[v]==czas[v])
		  {
		  if(pop)mosty.PB(MP(v,pop));
		  SPOJNE++;
		  while(S.back()!=v)
		    {
		    scc[S.back()]=SPOJNE;
		    stan[S.back()]=2;
		    S.pop_back();
		    }
		  scc[v]=SPOJNE;
		  S.pop_back();
		  stan[v]=2;
		  }
		}
	void calc()
		{
		FOR(i,1,n)
			if(stan[i]==0)
				dfs(i,0);
		}
	void clear()
		{
		FOR(i,1,n)
			{
			V[i].clear();
			low[i]=czas[i]=scc[i]=0;
			stan[i]=0;
			}
		TIME=1;
		SPOJNE=0;
		mosty.clear();
		S.clear();
		}
	void addedge(int a,int b)
		{
		if(a==b)return;
		V[a].PB(b);
		V[b].PB(a);
		}
	VI tovector()
		{
		VI ret;
		FOR(i,1,n)ret.PB(scc[i]);
		ret.PB(mosty.size());
		REP(i,mosty.size())
			{
			ret.PB(mosty[i].f);
			ret.PB(mosty[i].s);
			}
		return ret;
		}
	int pom[MXN];
	void vectortoedges(VI V)
		{
		REP(i,n)
			{
			int x=V[i];
			if(pom[x]!=0)addedge(pom[x],i+1);
			pom[x]=i+1;
			}
		REP(i,n)
			{
			int x=V[i];
			if(pom[x])
				{
				addedge(pom[x],i+1);
				pom[x]=0;
				}
			}
		int m=V[n];
		int w=n+1;
		REP(i,m)
			{
			addedge(V[w],V[w+1]);
			w+=2;			
			}
		}
	}
int MXMESSAGE=60010;//intów111 ma byćkilka zapasu bo dupa
void send(int target,VI &V)
	{
	//cerr<<"send ID:"<<ID<<" tr: "<<target<<" siz: "<<V.size()<<endl;
	PutInt(target,V.size());
	REP(i,V.size())
		{
		if(i%MXMESSAGE==MXMESSAGE-1)
			Send(target);
		PutInt(target,V[i]);
		}
	Send(target);
	}
VI get(int source)
	{
//	cerr<<"get ID: "<<ID<<" from: "<<source<<endl;
	VI r;
	Receive(source);
	int s=GetInt(source);
	REP(i,s)
		{
		if(i%MXMESSAGE==MXMESSAGE-1)
			Receive(source);
		r.PB(GetInt(source));
		}
	return r;
	}
int main() 
{
NODES=NumberOfNodes();
ID=MyNodeId();
n=N=NumberOfIsles();
m=M=NumberOfBridges();


int SIZ=M/NODES+(M%NODES>0);
int poc=ID*SIZ;
mini(poc,M);
int kon=min(M-1,poc+SIZ-1);
FOR(i,poc,kon)
	{
	int a=BridgeEndA(i)+1;
	int b=BridgeEndB(i)+1;
	mostuj::addedge(a,b);
	}
mostuj::calc();

VI V=mostuj::tovector();


if(ID!=0 && (ID+1)*2>NODES)//wysyłam odrazu
	{
	send((ID+1)/2-1,V);
	return 0;
	}
	
mostuj::clear();
mostuj::vectortoedges(V);
	
if(NODES>1)mostuj::vectortoedges(get((ID+1)*2-1));
if((ID+1)*2+1<=NODES)
	{
	mostuj::vectortoedges(get((ID+1)*2));
	}
mostuj::calc();
if(ID>0)
	{
	V=mostuj::tovector();
	send((ID+1)/2-1,V);
	}
else
	{
//	cerr<<N<<" "<<M<<endl;
	printf("%d\n",(int)mostuj::mosty.size());
	if(0)REP(i,mostuj::mosty.size())
		{
		cerr<<mostuj::mosty[i].f<<" "<<mostuj::mosty[i].s<<endl;
		}
	}

}