Problem p03414 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define rpe(i, r, l) for (int i = (r); i >= (l); --i)
#define rpp(i, x, e, head) for (int i = head[x]; ~i; i = e[i].next)
#define dyes cerr << "yes" << endl
#define dbg(x) cerr << #x << "=" << x << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define pts puts("")
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
inline int read() {
  int f = 1, x = 0;
  char ch;
  do {
    ch = getchar();
    if (ch == '-')
      f = -1LL;
  } while (ch < '0' || ch > '9');
  do {
    x = x * 10 + ch - '0';
    ch = getchar();
  } while (ch >= '0' && ch <= '9');
  return f * x;
}
inline ll readll() {
  ll f = 1, x = 0;
  char ch;
  do {
    ch = getchar();
    if (ch == '-')
      f = -1LL;
  } while (ch < '0' || ch > '9');
  do {
    x = x * 10 + ch - '0';
    ch = getchar();
  } while (ch >= '0' && ch <= '9');
  return f * x;
}
template <class T> inline void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> inline void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
inline void swap(int &a, int &b) {
  int c = a;
  a = b;
  b = c;
}
using namespace std;
#define mst(a, val) memset(a, val, sizeof(a))
#define pii pair<int, int>
#define piii pair<int, pair<int, int>>
#define mp(i, j) make_pair(i, j)
#define fi first
#define sc second
#define inf (0x3f3f3f3f)
#define infl (0x3f3f3f3f3f3f3f3fLL)
#define forvec(i, j)                                                           \
  for (vector<int>::iterator i = j.begin(); i != j.end(); ++i)
#define forvecv(i, j)                                                          \
  for (vector<int>::iterator i = --j.end(); i >= j.begin(); --i)
//=====================head end======================//
const int N = 3e3 + 10;
const int M = 2e5 + 10;
vector<pii> e[N];
inline void add(int u, int v, int id) { e[u].push_back(mp(v, id)); }
int n, m;
int rea[N][N], vis[N];
inline void dfs1(int x, int frm) {
  vis[x] = 1;
  rea[frm][x] = 1;
  for (auto k : e[x]) {
    int v = k.fi;
    if (vis[v])
      continue;
    dfs1(v, frm);
  }
}
int p[N][N], q[N][N], col;
inline void dfs2(int x, int frm) {
  p[frm][x] = col;
  vis[x] = 1;
  for (auto k : e[x]) {
    int v = k.fi;
    if (vis[v])
      continue;
    dfs2(v, frm);
  }
}
inline void dfs3(int x, int frm) {
  q[frm][x] = col;
  vis[x] = 1;
  for (auto k : e[x]) {
    int v = k.fi;
    if (vis[v])
      continue;
    dfs3(v, frm);
  }
}
struct edge {
  int u, v, id;
} E[N];
int main() {
  n = read();
  m = read();
  rep(i, 1, m) {
    int u = read(), v = read();
    add(u, v, i);
    E[i].id = i;
    E[i].u = u;
    E[i].v = v;
  }
  rep(i, 1, n) {
    rep(j, 1, n) vis[j] = 0;
    dfs1(i, i);
  }
  rep(x, 1, n) {
    rep(j, 1, n) vis[j] = 0;
    vis[x] = 1;
    for (auto k : e[x]) {
      col = k.sc;
      int v = k.fi;
      if (vis[v])
        continue;
      dfs2(v, x);
    }
    rep(j, 1, n) vis[j] = 0;
    vis[x] = 1;
    int sz = (int)e[x].size() - 1;
    rpe(j, sz, 0) {
      pii k = e[x][j];
      col = k.sc;
      int v = k.fi;
      if (vis[v])
        continue;
      dfs3(v, x);
    }
  }
  rep(i, 1, m) {
    int u = E[i].u, v = E[i].v;
    int k = rea[v][u];
    k ^= (p[u][v] != i || q[u][v] != i);
    if (k) {
      puts("diff");
    } else
      puts("same");
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define rpe(i, r, l) for (int i = (r); i >= (l); --i)
#define rpp(i, x, e, head) for (int i = head[x]; ~i; i = e[i].next)
#define dyes cerr << "yes" << endl
#define dbg(x) cerr << #x << "=" << x << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define pts puts("")
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
inline int read() {
  int f = 1, x = 0;
  char ch;
  do {
    ch = getchar();
    if (ch == '-')
      f = -1LL;
  } while (ch < '0' || ch > '9');
  do {
    x = x * 10 + ch - '0';
    ch = getchar();
  } while (ch >= '0' && ch <= '9');
  return f * x;
}
inline ll readll() {
  ll f = 1, x = 0;
  char ch;
  do {
    ch = getchar();
    if (ch == '-')
      f = -1LL;
  } while (ch < '0' || ch > '9');
  do {
    x = x * 10 + ch - '0';
    ch = getchar();
  } while (ch >= '0' && ch <= '9');
  return f * x;
}
template <class T> inline void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> inline void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
inline void swap(int &a, int &b) {
  int c = a;
  a = b;
  b = c;
}
using namespace std;
#define mst(a, val) memset(a, val, sizeof(a))
#define pii pair<int, int>
#define piii pair<int, pair<int, int>>
#define mp(i, j) make_pair(i, j)
#define fi first
#define sc second
#define inf (0x3f3f3f3f)
#define infl (0x3f3f3f3f3f3f3f3fLL)
#define forvec(i, j)                                                           \
  for (vector<int>::iterator i = j.begin(); i != j.end(); ++i)
#define forvecv(i, j)                                                          \
  for (vector<int>::iterator i = --j.end(); i >= j.begin(); --i)
//=====================head end======================//
const int N = 3e3 + 10;
const int M = 2e5 + 10;
vector<pii> e[N];
inline void add(int u, int v, int id) { e[u].push_back(mp(v, id)); }
int n, m;
int rea[N][N], vis[N];
inline void dfs1(int x, int frm) {
  vis[x] = 1;
  rea[frm][x] = 1;
  for (auto k : e[x]) {
    int v = k.fi;
    if (vis[v])
      continue;
    dfs1(v, frm);
  }
}
int p[N][N], q[N][N], col;
inline void dfs2(int x, int frm) {
  p[frm][x] = col;
  vis[x] = 1;
  for (auto k : e[x]) {
    int v = k.fi;
    if (vis[v])
      continue;
    dfs2(v, frm);
  }
}
inline void dfs3(int x, int frm) {
  q[frm][x] = col;
  vis[x] = 1;
  for (auto k : e[x]) {
    int v = k.fi;
    if (vis[v])
      continue;
    dfs3(v, frm);
  }
}
struct edge {
  int u, v, id;
} E[M];
int main() {
  n = read();
  m = read();
  rep(i, 1, m) {
    int u = read(), v = read();
    add(u, v, i);
    E[i].id = i;
    E[i].u = u;
    E[i].v = v;
  }
  rep(i, 1, n) {
    rep(j, 1, n) vis[j] = 0;
    dfs1(i, i);
  }
  rep(x, 1, n) {
    rep(j, 1, n) vis[j] = 0;
    vis[x] = 1;
    for (auto k : e[x]) {
      col = k.sc;
      int v = k.fi;
      if (vis[v])
        continue;
      dfs2(v, x);
    }
    rep(j, 1, n) vis[j] = 0;
    vis[x] = 1;
    int sz = (int)e[x].size() - 1;
    rpe(j, sz, 0) {
      pii k = e[x][j];
      col = k.sc;
      int v = k.fi;
      if (vis[v])
        continue;
      dfs3(v, x);
    }
  }
  rep(i, 1, m) {
    int u = E[i].u, v = E[i].v;
    int k = rea[v][u];
    k ^= (p[u][v] != i || q[u][v] != i);
    if (k) {
      puts("diff");
    } else
      puts("same");
  }
  return 0;
}