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

#include <algorithm>
#include <cassert>
#include <iostream>
#include <utility>
#include <vector>
using std::pair;
using std::vector;

typedef long long LL;
typedef unsigned char uchar;

int numNodes;
int myNode;
int numVertices;
int numAllEdges;

constexpr int maxMessageSize = 8 << 10;
constexpr int maxLocalEdges = 2 << 20;

struct Vertex {
  int degree;
  int neighborsPtr;
  int depth;
  int low;
  int parent;
  int nextChild;
  bool seenParent;
  Vertex() : degree{0}, neighborsPtr{-1}, depth{-1}, low{-1}, parent{-1}, nextChild{0}, seenParent{false} {}
}; // 28 b

vector<Vertex> vertices; // 7 MB
// max edges:
// At most = 2*1.5*numVertices = 600K when merging remote,
// At most maxLocalEdges + 1.5 * numVertices = 2.3M when merging locally.
vector<pair<int,int>> edges; // 19 MB
vector<int> neighborStorage; // 19 MB
// used during compression
int numBridgesFound;
vector<uchar> compressed; // 2 MB
vector<int> vertexStack; // 1 MB

constexpr int parentBit = 1<<20;
constexpr int noParent = 0xffffff;

inline int SplitAllEdges(int node) {
  return LL(numAllEdges) * node / numNodes;
}

void ReserveMemory() {
  vertices.reserve(numVertices);
  int maxEdges = std::max(3 * numVertices,
      std::min(numAllEdges / numNodes + 1, maxLocalEdges + 3 * numVertices/2));
  edges.reserve(maxEdges);
  neighborStorage.reserve(2 * maxEdges);
  compressed.reserve(6 * numVertices);
  vertexStack.reserve(numVertices);
}

inline void ClearGraph() {
  vertices.clear();
  vertices.resize(numVertices);
  edges.clear();
}

inline void AddEdge(int a, int b) {
  if (a != b) {
    edges.push_back({a,b});
    ++vertices[a].degree;
    ++vertices[b].degree;
  }
}

void BuildNeighbors() {
  neighborStorage.resize(2u * edges.size());
  int nextNeighborsPtr = 0;
  for (Vertex &v : vertices) {
    nextNeighborsPtr += v.degree;
    v.neighborsPtr = nextNeighborsPtr;
  }
  assert(nextNeighborsPtr == int(neighborStorage.size()));
  for (const auto &e : edges) {
    Vertex &a = vertices[e.first];
    Vertex &b = vertices[e.second];
    neighborStorage[--a.neighborsPtr] = e.second;
    neighborStorage[--b.neighborsPtr] = e.first;
  }
}

inline int ParseInt24(vector<uchar>::const_iterator &it) {
  unsigned res = unsigned(*it++) << 16;
  res += unsigned(*it++) << 8;
  res += unsigned(*it++);
  return res;
}

inline void CompressInt24(unsigned x) {
  compressed.push_back(uchar(x >> 16));
  compressed.push_back(uchar(x >> 8));
  compressed.push_back(uchar(x));
}

void Decompress() {
  vector<uchar>::const_iterator it = compressed.begin();
  while (it != compressed.end()) {
    int first = ParseInt24(it);
    int x = first;
    int y;
    for(;;) {
      y = ParseInt24(it);
      if (y & parentBit) break;
      AddEdge(x, y);
      x = y;
    }
    if (x != first) AddEdge(x, first);
    if (y != noParent) AddEdge(first, y & ~parentBit);
  }
}

void BiconnectedCompress() {
  compressed.clear();
  vertexStack.clear();
  numBridgesFound = 0;

  for (int rootIdx=0; rootIdx < numVertices; ++rootIdx) {
    Vertex &root = vertices[rootIdx];
    if (root.depth != -1) continue;
    root.depth = 0;
    root.low = 0;
    vertexStack.push_back(rootIdx);
    int vIdx = rootIdx;

    while (vIdx != -1) {
      Vertex &v = vertices[vIdx];
      if (v.nextChild < v.degree) {
        int chIdx = neighborStorage[v.neighborsPtr + v.nextChild];
        if (chIdx == v.parent && !v.seenParent) {
          ++v.nextChild;
          v.seenParent = true;
          continue;
        }
        Vertex &ch = vertices[chIdx];
        if (ch.depth == -1) {
          // unvisited child
          ch.depth = v.depth + 1;
          ch.low = ch.depth;
          ch.parent = vIdx;
          vertexStack.push_back(chIdx);
          vIdx = chIdx;
        } else {
          // back edge
          v.low = std::min(v.low, ch.depth);
          ++v.nextChild;
        }
      } else {
        // no more children
        if (v.parent != -1) {
          Vertex &p = vertices[v.parent];
          p.low = std::min(p.low, v.low);
          p.nextChild++;
        }
        if (v.low >= v.depth) {
          for(;;) {
            int x = vertexStack.back();
            CompressInt24(x);
            vertexStack.pop_back();
            if (x == vIdx) break;
          }
          if (v.parent != -1) {
            ++numBridgesFound;
            CompressInt24(v.parent | parentBit);
          } else {
            CompressInt24(noParent);
          }
        }
        vIdx = v.parent;
      }
    }
  }
}

void SendCompressed(int node) {
  PutInt(node, compressed.size());
  int sz = 4;
  for (char c : compressed) {
    if(sz == maxMessageSize) {
      Send(node);
      sz=0;
    }
    PutChar(node, c);
    ++sz;
  }
  Send(node);
}

void ReceiveCompressed(int node) {
  Receive(node);
  compressed.resize(GetInt(node));
  int sz = 4;
  for (uchar &c : compressed) {
    if(sz == maxMessageSize) {
      Receive(node);
      sz=0;
    }
    c = GetChar(node);
    ++sz;
  }
}

void LocalProcessing() {
  int alpha = SplitAllEdges(myNode);
  int beta = SplitAllEdges(myNode+1);
  while (alpha < beta) {
    int gamma = std::min(alpha + maxLocalEdges, beta);

    ClearGraph();
    Decompress();

    for (int i=alpha; i < gamma; ++i) {
      int a = BridgeEndA(i);
      int b = BridgeEndB(i);
      AddEdge(a,b);
    }

    BuildNeighbors();
    BiconnectedCompress();
    alpha = gamma;
  }
}

int main() {
  numNodes = NumberOfNodes();
  myNode = MyNodeId();
  numVertices = NumberOfIsles();
  numAllEdges = NumberOfBridges();
  numNodes = std::min(numNodes, numAllEdges / (3 * numVertices) + 1);
  if (myNode >= numNodes) { return 0; }
  ReserveMemory();

  LocalProcessing();
  for(int dist=1; ; dist<<=1) {
    if (myNode & dist) {
      SendCompressed(myNode - dist);
      // We are done, exit.
      break;
    } else if (myNode + dist < numNodes) {
      // Incoming! Merge.
      ClearGraph();
      Decompress();
      ReceiveCompressed(myNode + dist);
      Decompress();
      BuildNeighbors();
      BiconnectedCompress();
    } else if (myNode > 0) {
      // Nothing to merge, sleep.
    } else {
      // We have the result!
      std::cout << numBridgesFound << '\n';
      break;
    }
  }
}