Problem p03691 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const int MAX_N = 405;

vector<pair<int, int>> adj[MAX_N];

int vis[MAX_N];
int fuck[MAX_N];
vector<pair<int, pair<int, int>>> constr[MAX_N];

void add_constr(int u, int t, int src) {
  vis[u] = 1;
  for (pair<int, int> nxt : adj[u]) {
    if (nxt.first >= t) {
      break;
    }

    if (vis[nxt.first]) {
      fuck[src] = 1;
      break;
    }

    constr[src].push_back(make_pair(nxt.first, make_pair(u, nxt.second)));
    add_constr(nxt.second, nxt.first, src);
  }
}

vector<pair<int, pair<int, int>>>
merge(const vector<pair<int, pair<int, int>>> &u,
      const vector<pair<int, pair<int, int>>> &v) {
  vector<pair<int, pair<int, int>>> ans;
  int uptr = 0, vptr = 0;
  while (uptr < (int)u.size() && vptr < (int)v.size()) {
    if (u[uptr] < v[vptr]) {
      ans.push_back(u[uptr]);
      uptr++;
    } else {
      ans.push_back(v[vptr]);
      vptr++;
    }
  }

  while (uptr < (int)u.size()) {
    ans.push_back(u[uptr]);
    uptr++;
  }

  while (vptr < (int)v.size()) {
    ans.push_back(v[vptr]);
    vptr++;
  }

  return ans;
}

int indeg[MAX_N];
bool fits(int u, int v, int n) {
  for (int i = 1; i <= n; i++) {
    indeg[i] = 0;
  }

  vector<pair<int, pair<int, int>>> merged = merge(constr[u], constr[v]);
  pair<int, pair<int, int>> last = {-1, {-1, -1}};
  for (auto pr : merged) {
    if (pr.first == last.first) {
      if (pr.second != last.second) {
        return false;
      }
    } else {
      indeg[pr.second.second]++;
    }
    last = pr;
  }

  for (int i = 1; i <= n; i++) {
    if (indeg[i] > 1)
      return false;
  }

  if (indeg[u] > 0)
    return false;
  if (indeg[v] > 0)
    return false;
  return true;
}

int main() {
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;

    adj[u].push_back(make_pair(i, v));
    adj[v].push_back(make_pair(i, u));
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      vis[j] = 0;
    }
    add_constr(i, m, i);
    sort(constr[i].begin(), constr[i].end());
  }

  int ans = 0;
  for (int u = 1; u <= n; u++) {
    if (!fuck[u]) {
      for (int v = u + 1; v <= n; v++) {
        if (!fuck[v]) {
          if (fits(u, v, n)) {
            ans++;
          }
        }
      }
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const int MAX_N = 405;

vector<pair<int, int>> adj[MAX_N];

int vis[MAX_N];
int fuck[MAX_N];
vector<pair<int, pair<int, int>>> constr[MAX_N];

void add_constr(int u, int t, int src) {
  vis[u] = 1;
  for (pair<int, int> nxt : adj[u]) {
    if (nxt.first >= t) {
      break;
    }

    if (vis[nxt.second]) {
      fuck[src] = 1;
      break;
    }

    constr[src].push_back(make_pair(nxt.first, make_pair(u, nxt.second)));
    add_constr(nxt.second, nxt.first, src);
  }
}

vector<pair<int, pair<int, int>>>
merge(const vector<pair<int, pair<int, int>>> &u,
      const vector<pair<int, pair<int, int>>> &v) {
  vector<pair<int, pair<int, int>>> ans;
  int uptr = 0, vptr = 0;
  while (uptr < (int)u.size() && vptr < (int)v.size()) {
    if (u[uptr] < v[vptr]) {
      ans.push_back(u[uptr]);
      uptr++;
    } else {
      ans.push_back(v[vptr]);
      vptr++;
    }
  }

  while (uptr < (int)u.size()) {
    ans.push_back(u[uptr]);
    uptr++;
  }

  while (vptr < (int)v.size()) {
    ans.push_back(v[vptr]);
    vptr++;
  }

  return ans;
}

int indeg[MAX_N];
bool fits(int u, int v, int n) {
  for (int i = 1; i <= n; i++) {
    indeg[i] = 0;
  }

  vector<pair<int, pair<int, int>>> merged = merge(constr[u], constr[v]);
  pair<int, pair<int, int>> last = {-1, {-1, -1}};
  for (auto pr : merged) {
    if (pr.first == last.first) {
      if (pr.second != last.second) {
        return false;
      }
    } else {
      indeg[pr.second.second]++;
    }
    last = pr;
  }

  for (int i = 1; i <= n; i++) {
    if (indeg[i] > 1)
      return false;
  }

  if (indeg[u] > 0)
    return false;
  if (indeg[v] > 0)
    return false;
  return true;
}

int main() {
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;

    adj[u].push_back(make_pair(i, v));
    adj[v].push_back(make_pair(i, u));
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      vis[j] = 0;
    }
    add_constr(i, m, i);
    sort(constr[i].begin(), constr[i].end());
  }

  int ans = 0;
  for (int u = 1; u <= n; u++) {
    if (!fuck[u]) {
      for (int v = u + 1; v <= n; v++) {
        if (!fuck[v]) {
          if (fits(u, v, n)) {
            ans++;
          }
        }
      }
    }
  }

  cout << ans << endl;
}