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
#include <algorithm>
#include <vector>
#include <cstdio>
// #include "sab-test/sabotaz.h"
#include "sabotaz.h"
#include "message.h"
using namespace std;

const int MAX = 200010;
const int LIMIT = 50000;

int S[MAX];
int findset(int x) { return S[x] == x ? x : S[x] = findset(S[x]); }
bool link(int a, int b) {
  a = findset(a);
  b = findset(b);
  if (a == b) return false;
  S[a] = b;
  return true;
}

int nodes, id;
int n, m;
vector<int> T, tmp, B;
vector<pair<int, int>> G[MAX];
int pre[MAX], low[MAX];
int tpre = 1;

void SendV(int target, const vector<int> &v) {
  PutInt(target, v.size());
  Send(target);
  for (int rep=0;rep<(v.size()+LIMIT-1)/LIMIT;rep++)
  {
    for (int i=rep*LIMIT;i<min(rep*LIMIT+LIMIT, (int)v.size());i++)
      PutInt(target, v[i]);
    Send(target);
  }
}

void ReceiveV(int source, vector<int> &v) {
  int size = GetInt(source);
  v.resize(size);
  for (int rep=0;rep<(size+LIMIT-1)/LIMIT;rep++)
  {
    Receive(source);
    for (int i=rep*LIMIT;i<min(rep*LIMIT+LIMIT, size);i++)
      v[i] = GetInt(source);
  }
}

void spanning_tree() {
  for (int i=id;i<m;i+=nodes)
  {
    int a = BridgeEndA(i);
    int b = BridgeEndB(i);
    G[a].push_back({b, i});
    G[b].push_back({a, i});
    if (link(a, b)) T.push_back(i);
  }

  int cnt = (2*id+1 < nodes) + (2*id+2 < nodes);
  for (int c=0;c<cnt;c++)
  {
    int s = Receive(2*id+1+c);
    ReceiveV(s, tmp);
    for (int ind : tmp)
    {
      int a = BridgeEndA(ind);
      int b = BridgeEndB(ind);
      if (link(a, b)) T.push_back(ind);
    }
  }
  if (id > 0) {
    int p = (id+1)/2 - 1;
    SendV(p, T);
    Receive(p);
    ReceiveV(p, T);
  }
  if (2*id+1 < nodes) SendV(2*id+1, T);
  if (2*id+2 < nodes) SendV(2*id+2, T);
}

void dfs(int u, int pind) {
  pre[u] = low[u] = tpre++;
  for (auto edge : G[u])
    if (edge.second != pind) {
      if (!pre[edge.first]) {
        dfs(edge.first, edge.second);
        low[u] = min(low[u], low[edge.first]);
      } else {
        low[u] = min(low[u], pre[edge.first]);
      }
    }
  if (pind != -1 && pre[u] == low[u]) {
    B.push_back(pind);
  }
}

void bridges() {
  for (int ind : T)
  {
    if (ind % nodes != id) {
      int a = BridgeEndA(ind);
      int b = BridgeEndB(ind);
      G[a].push_back({b, ind});
      G[b].push_back({a, ind});
    }
  }
  for (int i=0;i<n;i++)
    if (!pre[i])
      dfs(i, -1);
  sort(B.begin(), B.end());
}

void solve() {
  int cnt = (2*id+1 < nodes) + (2*id+2 < nodes);
  for (int c=0;c<cnt;c++)
  {
    int s = Receive(2*id+1+c);
    ReceiveV(s, tmp);
    T.resize(B.size());
    auto tend = set_intersection(B.begin(), B.end(), tmp.begin(), tmp.end(), T.begin());
    T.erase(tend, T.end());
    T.swap(B);
  }
  if (id > 0) {
    int p = (id+1)/2 - 1;
    SendV(p, B);
  } else {
    printf("%d\n", (int)B.size());
  }
}

int main() {
  nodes = NumberOfNodes();
  id = MyNodeId();
  n = NumberOfIsles();
  m = NumberOfBridges();
  for (int i=0;i<n;i++) S[i] = i;
  spanning_tree();
  bridges();
  solve();
  return 0;
}