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
#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>

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

// Information about nodes:
// ------------------------
struct {
  int number_of_nodes;
  int my_id;

  void Init() {
    number_of_nodes = NumberOfNodes();
    my_id = MyNodeId();
  }
} NodeInfo;

// My queue:
// ---------
struct {
  int values[200005];  // It should be large enough to store 2 * MAX_NODES.
  int b, e;

  void Init() {
    b = e = 0;
  }

  void Add(int x) {
    values[e++] = x;
  }

  int Pop() {
    return values[b++];
  }

  int Size() {
    return e - b;
  }
} Queue;

// This node's tasks:
// ------------------
struct {
  // 1. Range of edges this node processes.
  int edge_min, edge_max;
  // 2. Sequence of nodes with whom this node merges its results.
  std::vector<int> merges_with;
  // 3. If this node is the last one, sends_to_whom = -1;
  //    otherwise sends_to_whom contains id of the node which will merge
  //    this node's results with its results.
  int sends_to_whom;

  void Init() {
    const int n = NumberOfIsles();
    const int m = NumberOfBridges();
    const int edge_range_width
        = (m + NodeInfo.number_of_nodes - 1) / NodeInfo.number_of_nodes;
    edge_min = std::min(NodeInfo.my_id * edge_range_width, m);
    edge_max = std::min((NodeInfo.my_id + 1) * edge_range_width - 1, m - 1);
    Queue.Init();
    for (int i = 0; i < NodeInfo.number_of_nodes; i++) {
      Queue.Add(i);
    }
    sends_to_whom = -1;
    while (Queue.Size() >= 2) {
      const int node_a = Queue.Pop();
      const int node_b = Queue.Pop();
      if (node_a == NodeInfo.my_id) {
        merges_with.push_back(node_b);
      }
      if (node_b == NodeInfo.my_id) {
        sends_to_whom = node_a;
      }
      Queue.Add(node_a);
    }
  }
} Tasks;

// Find & Union:
// -------------
struct {
  int link[200005];
  std::vector<int> group[200005];

  void Init(int n) {
    for (int i = 1; i <= n; i++) {
      link[i] = i;
    }
  }

  int Find(int w) {
    if (link[w] == w) {
      return w;
    }
    return link[w] = Find(link[w]);
  }

  void Union(int u, int v) {
    link[Find(u)] = Find(v);
  }

  void MakeGroups(int n) {
    for (int i = 1; i <= n; i++) {
      group[i].clear();
    }
    for (int i = 1; i <= n; i++) {
      group[Find(i)].push_back(i);
    }
  }

  const std::vector<int>& GetGroup(int w) {
    return group[w];
  }
} FindUnion;

// Graph and operations on the graph:
// ----------------------------------
struct {
  int n;  // Number of vertices and edges in the whole graph.
  // The graph has only a subset of edges from the original graph.
  std::vector<int> graph[200005];
  int pre_counter;  // Counter for the pre_order numbers.
  int pre_order[200005];
  int low[200005];
  int number_of_bridges;

  void Init() {
    n = NumberOfIsles();
    for (int i = Tasks.edge_min; i <= Tasks.edge_max; i++) {
      const int edge_a = BridgeEndA(i) + 1;  // Here nodes are numbered
      const int edge_b = BridgeEndB(i) + 1;  // from 1 to n (hence +1).
      graph[edge_a].push_back(edge_b);
      graph[edge_b].push_back(edge_a);
    }
  }

  void DfsForLow(int w, int father) {
    pre_order[w] = pre_counter++;
    low[w] = pre_order[w];
    for (int neighbour : graph[w]) {
      if (neighbour == father) {
        // Prevents from going back to father using the same edge,
        // but blocks only one such edge.
        father = -1;
        continue;
      }
      if (pre_order[neighbour]) {
        // This neighbour has been already visited.
        low[w] = std::min(low[w], pre_order[neighbour]);
      } else {
        DfsForLow(neighbour, w);
        low[w] = std::min(low[w], low[neighbour]);
      }
    }
  }

  bool IsABridge(int a, int b) {
    if (pre_order[a] > pre_order[b]) {
      std::swap(a, b);
    }
    // Now a is above b.
    return low[b] > pre_order[a];
  }

  void ComputeLow() {
    for (int i = 1; i <= n; i++) {
      pre_order[i] = 0;
    }
    pre_counter = 1;
    for (int i = 1; i <= n; i++) {
      if (pre_order[i] == 0) {
        DfsForLow(i, -1);
      }
    }
  }

  void Canonize() {
    number_of_bridges = 0;
    ComputeLow();
    FindUnion.Init(n);
    for (int w = 1; w <= n; w++) {
      int graph_size = graph[w].size();
      for (int i = 0; i < graph_size; i++) {
        if (!IsABridge(w, graph[w][i])) {
          FindUnion.Union(w, graph[w][i]);
          std::swap(graph[w][i], graph[w][graph_size - 1]);
          graph_size--;
          i--;
        } else {
          number_of_bridges++;
        }
      }
      graph[w].resize(graph_size);
    }
    number_of_bridges /= 2;  // Each bridge was counted two times.
    FindUnion.MakeGroups(n);
    for (int w = 1; w <= n; w++) {
      const std::vector<int>& group = FindUnion.GetGroup(w);
      if ((int) group.size() > 1) {
        for (int i = 1; i < group.size(); i++) {
          graph[group[i - 1]].push_back(group[i]);
          graph[group[i]].push_back(group[i - 1]);
        }
        graph[group[0]].push_back(group.back());
        graph[group.back()].push_back(group[0]);
      }
    }
  }

  void MergeWith(int with_whom) {
    Receive(with_whom);
    int number_of_edges = GetInt(with_whom);
    while (number_of_edges--) {
      const int edge_a = GetInt(with_whom);
      const int edge_b = GetInt(with_whom);
      graph[edge_a].push_back(edge_b);
      graph[edge_b].push_back(edge_a);
    }
  }

  void SendTo(int to_whom) {
    std::vector<std::pair<int, int>> edges;
    for (int w = 1; w <= n; w++) {
      for (int neighbour : graph[w]) {
        if (neighbour < w) {
          edges.emplace_back(neighbour, w);
        }
      }
    }
    PutInt(to_whom, (int) edges.size());
    for (const auto& edge : edges) {
      PutInt(to_whom, edge.first);
      PutInt(to_whom, edge.second);
    }
    Send(to_whom);
  }
} Graph;

int main() {
  NodeInfo.Init();
  Tasks.Init();
  Graph.Init();
  Graph.Canonize();
  for (int node_id : Tasks.merges_with) {
    Graph.MergeWith(node_id);
    Graph.Canonize();
  }
  if (Tasks.sends_to_whom == -1) {
    printf("%d\n", Graph.number_of_bridges);
  } else {
    Graph.SendTo(Tasks.sends_to_whom);
  }
  return 0;
}