Problem p03414 (C++)

Buggy submission (status: Runtime Error):
/*input
5 9
3 2
3 1
4 1
4 2
3 5
5 3
3 4
1 2
2 5

2 2
1 2
2 1

3 3
1 2
1 3
2 3
*/
#include <bits/stdc++.h>
using namespace std;
#define sp ' '
#define endl '\n'
#define fi first
#define se second
#define mp make_pair
#define N 1005
#define bit(x, y) ((x >> y) & 1LL)
#define loop(i, l, r) for (int i = (signed)(l); i <= (signed)(r); i++)

#define what_is(x) cerr << #x << " is " << x << endl;
#define error(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << '(' << a.first << ", " << a.second << ')';
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  for (unsigned int i = 0; i < a.size(); i++)
    os << a[i] << (i < a.size() - 1 ? ", " : "");
  os << ']';
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &a) {
  os << '{';
  for (typename set<T>::iterator it = a.begin(); it != a.end(); it++) {
    typename set<T>::iterator jt = it;
    os << *it << (++jt != a.end() ? ", " : "");
  }
  os << '}';
  return os;
}
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &a) {
  os << "{\n";
  for (typename map<T1, T2>::iterator it = a.begin(); it != a.end(); it++) {
    typename map<T1, T2>::iterator jt = it;
    os << "  " << it->first << ": " << it->second
       << (++jt != a.end() ? ",\n" : "\n");
  }
  os << '}';
  return os;
}

int n, m;
vector<vector<pair<int, int>>> a(N);
int cnt[N][N];
int mark[N][N];
bool ansOrder[200005];

void dfs(int u, int p, const int root, const int source) {
  if (u == root)
    return;
  if (cnt[root][u] >= 2)
    return;
  if (cnt[root][u] == 1 && mark[root][u] == source)
    return;
  cnt[root][u]++;
  mark[root][u] = source;
  for (auto it : a[u]) {
    int v = it.fi;
    if (v == p)
      continue;
    dfs(v, u, root, source);
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  loop(i, 1, m) {
    int u, v;
    cin >> u >> v;
    a[u].push_back(mp(v, i));
  }
  loop(u, 1, m) {
    for (auto it : a[u]) {
      int v = it.fi;
      dfs(v, u, u, v);
    }
  }
  loop(u, 1, n) {
    for (auto it : a[u]) {
      int v = it.fi;
      bool diff = false;
      if (cnt[u][v] >= 2) {
        if (cnt[v][u] == 0)
          diff = true;
      } else if (cnt[u][v] == 1) {
        if (cnt[v][u] >= 1)
          diff = true;
      }
      ansOrder[it.se] = diff;
    }
  }
  loop(i, 1, m) {
    if (ansOrder[i] == false)
      cout << "same" << endl;
    else
      cout << "diff" << endl;
  }
}

Fixed submission:
/*input
5 9
3 2
3 1
4 1
4 2
3 5
5 3
3 4
1 2
2 5

2 2
1 2
2 1

3 3
1 2
1 3
2 3
*/
#include <bits/stdc++.h>
using namespace std;
#define sp ' '
#define endl '\n'
#define fi first
#define se second
#define mp make_pair
#define N 1005
#define bit(x, y) ((x >> y) & 1LL)
#define loop(i, l, r) for (int i = (signed)(l); i <= (signed)(r); i++)

#define what_is(x) cerr << #x << " is " << x << endl;
#define error(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << '(' << a.first << ", " << a.second << ')';
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  for (unsigned int i = 0; i < a.size(); i++)
    os << a[i] << (i < a.size() - 1 ? ", " : "");
  os << ']';
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &a) {
  os << '{';
  for (typename set<T>::iterator it = a.begin(); it != a.end(); it++) {
    typename set<T>::iterator jt = it;
    os << *it << (++jt != a.end() ? ", " : "");
  }
  os << '}';
  return os;
}
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &a) {
  os << "{\n";
  for (typename map<T1, T2>::iterator it = a.begin(); it != a.end(); it++) {
    typename map<T1, T2>::iterator jt = it;
    os << "  " << it->first << ": " << it->second
       << (++jt != a.end() ? ",\n" : "\n");
  }
  os << '}';
  return os;
}

int n, m;
vector<vector<pair<int, int>>> a(N);
int cnt[N][N];
int mark[N][N];
bool ansOrder[200005];

void dfs(int u, int p, const int root, const int source) {
  if (u == root)
    return;
  if (cnt[root][u] >= 2)
    return;
  if (cnt[root][u] == 1 && mark[root][u] == source)
    return;
  cnt[root][u]++;
  mark[root][u] = source;
  for (auto it : a[u]) {
    int v = it.fi;
    if (v == p)
      continue;
    dfs(v, u, root, source);
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  loop(i, 1, m) {
    int u, v;
    cin >> u >> v;
    a[u].push_back(mp(v, i));
  }
  loop(u, 1, n) {
    for (auto it : a[u]) {
      int v = it.fi;
      dfs(v, u, u, v);
    }
  }
  loop(u, 1, n) {
    for (auto it : a[u]) {
      int v = it.fi;
      bool diff = false;
      if (cnt[u][v] >= 2) {
        if (cnt[v][u] == 0)
          diff = true;
      } else if (cnt[u][v] == 1) {
        if (cnt[v][u] >= 1)
          diff = true;
      }
      ansOrder[it.se] = diff;
    }
  }
  loop(i, 1, m) {
    if (ansOrder[i] == false)
      cout << "same" << endl;
    else
      cout << "diff" << endl;
  }
}