Problem p03414 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define st first
#define nd second
typedef pair<int, int> pp;
const int mod = 1e9 + 7;
const int N = 1e3 + 3;

vector<pp> V[N], U[N];
vector<int> Y[N];
int D[N], low[N], zaman, H[N], Scc[N], scc, T[N], ans[N], n, m, x, y, i, X;
stack<int> S;

void f(int x) {
  D[x] = low[x] = ++zaman;
  S.push(x);
  H[x] = 1;
  for (auto t : V[x]) {
    int y = t.st;
    if (y == X)
      continue;
    if (!D[y]) {
      f(y);
      low[x] = min(low[x], low[y]);
    } else if (H[y])
      low[x] = min(low[x], D[y]);
  }

  if (low[x] == D[x]) {
    scc++;
    for (int y = 0; y != x;) {
      y = S.top();
      S.pop();
      H[y] = 0;
      Scc[y] = scc;
      Y[scc].pb(y);
    }
  }
}

void g(int x) {
  X = x;
  int i;
  memset(D, zaman = 0, sizeof D);
  memset(Scc, scc = 0, sizeof Scc);
  for (i = 1; i <= n; i++) {
    Y[i].clear();
    U[i].clear();
  }
  for (i = 1; i <= n; i++)
    if (i != X && !D[i])
      f(i);
  if (x == 0) {
    for (i = 1; i <= n; i++)
      T[i] = Scc[i];
    return;
  }

  memset(D, 0, sizeof D);
  for (auto t : V[X]) {
    U[Scc[t.st]].pb(t);
  }
  for (i = scc; i; i--) {
    if (!D[i] && U[i].size() == 0)
      continue;
    if (D[i] || U[i].size() > 1) {
      for (auto t : U[i]) {
        ans[t.nd] = T[X] == T[t.st];
      }
    } else {
      for (auto t : U[i])
        ans[t.nd] = T[X] != T[t.st];
    }

    for (auto x : Y[i]) {
      for (auto y : V[x])
        D[Scc[y.st]] = 1;
    }
  }
}

signed main() {
  cin >> n >> m;
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    V[x].pb(mp(y, i));
  }

  for (i = 0; i <= n; i++)
    g(i);
  for (i = 1; i <= m; i++)
    puts(ans[i] ? "same" : "diff");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define st first
#define nd second
typedef pair<int, int> pp;
const int mod = 1e9 + 7;
const int N = 1e3 + 3;

vector<pp> V[N], U[N];
vector<int> Y[N];
int D[N], low[N], zaman, H[N], Scc[N], scc, T[N], ans[N * 200], n, m, x, y, i,
    X;
stack<int> S;

void f(int x) {
  D[x] = low[x] = ++zaman;
  S.push(x);
  H[x] = 1;
  for (auto t : V[x]) {
    int y = t.st;
    if (y == X)
      continue;
    if (!D[y]) {
      f(y);
      low[x] = min(low[x], low[y]);
    } else if (H[y])
      low[x] = min(low[x], D[y]);
  }

  if (low[x] == D[x]) {
    scc++;
    for (int y = 0; y != x;) {
      y = S.top();
      S.pop();
      H[y] = 0;
      Scc[y] = scc;
      Y[scc].pb(y);
    }
  }
}

void g(int x) {
  X = x;
  int i;
  memset(D, zaman = 0, sizeof D);
  memset(Scc, scc = 0, sizeof Scc);
  for (i = 1; i <= n; i++) {
    Y[i].clear();
    U[i].clear();
  }
  for (i = 1; i <= n; i++)
    if (i != X && !D[i])
      f(i);
  if (x == 0) {
    for (i = 1; i <= n; i++)
      T[i] = Scc[i];
    return;
  }

  memset(D, 0, sizeof D);
  for (auto t : V[X]) {
    U[Scc[t.st]].pb(t);
  }
  for (i = scc; i; i--) {
    if (!D[i] && U[i].size() == 0)
      continue;
    if (D[i] || U[i].size() > 1) {
      for (auto t : U[i]) {
        ans[t.nd] = T[X] == T[t.st];
      }
    } else {
      for (auto t : U[i])
        ans[t.nd] = T[X] != T[t.st];
    }

    for (auto x : Y[i]) {
      for (auto y : V[x])
        D[Scc[y.st]] = 1;
    }
  }
}

signed main() {
  cin >> n >> m;
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    V[x].pb(mp(y, i));
  }

  for (i = 0; i <= n; i++)
    g(i);
  for (i = 1; i <= m; i++)
    puts(ans[i] ? "same" : "diff");
  return 0;
}