Problem p03414 (C++)

Buggy submission (status: Runtime Error):
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>

using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;

typedef long long ll;
typedef unsigned int ul;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef map<int, vector<int>> mvii;
typedef map<int, int> mii;
typedef queue<int> qi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;

// Order Statistic Tree

/* Special functions:

                find_by_order(k) --> returns iterator to the kth largest element
   counting from 0 order_of_key(val) --> returns the number of items in a set
   that are strictly smaller than our item
*/

// typedef tree<
// int,
// null_type,
// less<int>,
// rb_tree_tag,
// tree_order_statistics_node_update>
// ordered_set;

#define MP make_pair
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
#define ALL(a) a.begin(), a.end()
#define PI acos(-1)
#define ms(x, y) memset(x, y, sizeof(x))
#define inf 1e9
#define INF 1e16
#define pb push_back
#define MAX 1005
#define debug(a, b) cout << a << ": " << b << endl
#define Debug cout << "Reached here" << endl
#define prnt(a) cout << a << "\n"
#define mod 1000000007LL
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORr(i, a, b) for (int i = (a); i >= (b); i--)
#define itrALL(c, itr)                                                         \
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)
#define lc ((node) << 1)
#define rc ((node) << 1 | 1)
#define VecPrnt(v)                                                             \
  FOR(J, 0, v.size()) cout << v[J] << " ";                                     \
  cout << endl
#define endl "\n"
#define PrintPair(x) cout << x.first << " " << x.second << endl
#define EPS 1e-9
#define ArrPrint(a, st, en)                                                    \
  for (int J = st; J <= en; J++)                                               \
    cout << a[J] << " ";                                                       \
  cout << endl;

/* Direction Array */

// int fx[]={1,-1,0,0};
// int fy[]={0,0,1,-1};
// int fx[]={0,0,1,-1,-1,1,-1,1};
// int fy[]={-1,1,0,0,1,1,-1,-1};

template <class T> inline T bigmod(T p, T e, T M) {
  ll ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1)
      ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}

template <class T> inline T gcd(T a, T b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); }
template <class T> inline T lcm(T a, T b) {
  a = abs(a);
  b = abs(b);
  return (a / gcd(a, b)) * b;
}
template <class T, class X> inline bool getbit(T a, X i) {
  T t = 1;
  return ((a & (t << i)) > 0);
}
template <class T, class X> inline T setbit(T a, X i) {
  T t = 1;
  return (a | (t << i));
}
template <class T, class X> inline T resetbit(T a, X i) {
  T t = 1;
  return (a & (~(t << i)));
}

inline ll getnum() {
  char c = getchar();
  ll num, sign = 1;
  for (; c < '0' || c > '9'; c = getchar())
    if (c == '-')
      sign = -1;
  for (num = 0; c >= '0' && c <= '9';) {
    c -= '0';
    num = num * 10 + c;
    c = getchar();
  }
  return num * sign;
}

inline ll power(ll a, ll b) {
  ll multiply = 1;
  FOR(i, 0, b) { multiply *= a; }
  return multiply;
}

/****** END OF HEADER ******/

int n, m;
vi graph[MAX], ids[MAX];
bool visited[MAX][MAX][2], reach[MAX][MAX];
int way[MAX][MAX][2];
string ans[2 * MAX];

void dfs(int u, int beg, int id, bool fl) {
  if (visited[beg][u][fl])
    return;

  // prnt(u);

  way[beg][u][fl] = id;
  visited[beg][u][fl] = true;

  for (auto v : graph[u]) {
    if (v == beg)
      continue;

    dfs(v, beg, id, fl);
  }
}

void dfs(int u, int beg) {
  if (reach[beg][u])
    return;
  reach[beg][u] = true;

  for (auto v : graph[u]) {
    dfs(v, beg);
  }
}

void solve() {
  FOR(i, 1, n + 1) { dfs(i, i); }

  // Debug;

  FOR(i, 1, n + 1) {
    // prnt(graph[i][0]);
    FOR(j, 0, graph[i].size()) {
      int v = graph[i][j];
      int id = ids[i][j];

      dfs(v, i, id, 0);
    }

    FORr(j, (int)graph[i].size() - 1, 0) {
      int v = graph[i][j];
      int id = ids[i][j];

      dfs(v, i, id, 1);
    }

    // cout<<"starting from vertex "<<i<<endl;

    // FOR(j,0,graph[i].size())
    // {
    // 	int v=graph[i][j];
    // 	cout<<"to go to vertex "<<v<<" using edge "<<way[i][v][0]<<"
    // "<<way[i][v][1]<<endl; 	cout<<"reaching from "<<v<<" to "<<i<<" is
    // "<<reach[v][i]<<endl;
    // }

    FOR(j, 0, graph[i].size()) {
      int v = graph[i][j];
      int id = ids[i][j];

      // debug("v",v);

      if (way[i][v][0] == way[i][v][1]) {
        if (!reach[v][i])
          ans[id] = "same";
        else
          ans[id] = "diff";
      } else {
        if (reach[v][i])
          ans[id] = "same";
        else
          ans[id] = "diff";
      }
    }
  }

  FOR(i, 0, m) printf("%s\n", ans[i].c_str());
}

int main() {
  // ios_base::sync_with_stdio(0);
  // cin.tie(NULL); cout.tie(NULL);
  // freopen("in.txt","r",stdin);

  int test, cases = 1;

  scanf("%d%d", &n, &m);

  int u, v;

  FOR(i, 0, m) {
    scanf("%d%d", &u, &v);
    graph[u].pb(v);
    ids[u].pb(i);
  }

  solve();

  return 0;
}


Fixed submission:
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>

using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;

typedef long long ll;
typedef unsigned int ul;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef map<int, vector<int>> mvii;
typedef map<int, int> mii;
typedef queue<int> qi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;

// Order Statistic Tree

/* Special functions:

                find_by_order(k) --> returns iterator to the kth largest element
   counting from 0 order_of_key(val) --> returns the number of items in a set
   that are strictly smaller than our item
*/

// typedef tree<
// int,
// null_type,
// less<int>,
// rb_tree_tag,
// tree_order_statistics_node_update>
// ordered_set;

#define MP make_pair
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
#define ALL(a) a.begin(), a.end()
#define PI acos(-1)
#define ms(x, y) memset(x, y, sizeof(x))
#define inf 1e9
#define INF 1e16
#define pb push_back
#define MAX 1005
#define debug(a, b) cout << a << ": " << b << endl
#define Debug cout << "Reached here" << endl
#define prnt(a) cout << a << "\n"
#define mod 1000000007LL
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORr(i, a, b) for (int i = (a); i >= (b); i--)
#define itrALL(c, itr)                                                         \
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)
#define lc ((node) << 1)
#define rc ((node) << 1 | 1)
#define VecPrnt(v)                                                             \
  FOR(J, 0, v.size()) cout << v[J] << " ";                                     \
  cout << endl
#define endl "\n"
#define PrintPair(x) cout << x.first << " " << x.second << endl
#define EPS 1e-9
#define ArrPrint(a, st, en)                                                    \
  for (int J = st; J <= en; J++)                                               \
    cout << a[J] << " ";                                                       \
  cout << endl;

/* Direction Array */

// int fx[]={1,-1,0,0};
// int fy[]={0,0,1,-1};
// int fx[]={0,0,1,-1,-1,1,-1,1};
// int fy[]={-1,1,0,0,1,1,-1,-1};

template <class T> inline T bigmod(T p, T e, T M) {
  ll ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1)
      ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}

template <class T> inline T gcd(T a, T b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); }
template <class T> inline T lcm(T a, T b) {
  a = abs(a);
  b = abs(b);
  return (a / gcd(a, b)) * b;
}
template <class T, class X> inline bool getbit(T a, X i) {
  T t = 1;
  return ((a & (t << i)) > 0);
}
template <class T, class X> inline T setbit(T a, X i) {
  T t = 1;
  return (a | (t << i));
}
template <class T, class X> inline T resetbit(T a, X i) {
  T t = 1;
  return (a & (~(t << i)));
}

inline ll getnum() {
  char c = getchar();
  ll num, sign = 1;
  for (; c < '0' || c > '9'; c = getchar())
    if (c == '-')
      sign = -1;
  for (num = 0; c >= '0' && c <= '9';) {
    c -= '0';
    num = num * 10 + c;
    c = getchar();
  }
  return num * sign;
}

inline ll power(ll a, ll b) {
  ll multiply = 1;
  FOR(i, 0, b) { multiply *= a; }
  return multiply;
}

/****** END OF HEADER ******/

int n, m;
vi graph[MAX], ids[MAX];
bool visited[MAX][MAX][2], reach[MAX][MAX];
int way[MAX][MAX][2];
string ans[200005];

void dfs(int u, int beg, int id, bool fl) {
  if (visited[beg][u][fl])
    return;

  // prnt(u);

  way[beg][u][fl] = id;
  visited[beg][u][fl] = true;

  for (auto v : graph[u]) {
    if (v == beg)
      continue;

    dfs(v, beg, id, fl);
  }
}

void dfs(int u, int beg) {
  if (reach[beg][u])
    return;
  reach[beg][u] = true;

  for (auto v : graph[u]) {
    dfs(v, beg);
  }
}

void solve() {
  FOR(i, 1, n + 1) { dfs(i, i); }

  // Debug;

  FOR(i, 1, n + 1) {
    // prnt(graph[i][0]);
    FOR(j, 0, graph[i].size()) {
      int v = graph[i][j];
      int id = ids[i][j];

      dfs(v, i, id, 0);
    }

    FORr(j, (int)graph[i].size() - 1, 0) {
      int v = graph[i][j];
      int id = ids[i][j];

      dfs(v, i, id, 1);
    }

    // cout<<"starting from vertex "<<i<<endl;

    // FOR(j,0,graph[i].size())
    // {
    // 	int v=graph[i][j];
    // 	cout<<"to go to vertex "<<v<<" using edge "<<way[i][v][0]<<"
    // "<<way[i][v][1]<<endl; 	cout<<"reaching from "<<v<<" to "<<i<<" is
    // "<<reach[v][i]<<endl;
    // }

    FOR(j, 0, graph[i].size()) {
      int v = graph[i][j];
      int id = ids[i][j];

      // debug("v",v);

      if (way[i][v][0] == way[i][v][1]) {
        if (!reach[v][i])
          ans[id] = "same";
        else
          ans[id] = "diff";
      } else {
        if (reach[v][i])
          ans[id] = "same";
        else
          ans[id] = "diff";
      }
    }
  }

  FOR(i, 0, m) printf("%s\n", ans[i].c_str());
}

int main() {
  // ios_base::sync_with_stdio(0);
  // cin.tie(NULL); cout.tie(NULL);
  // freopen("in.txt","r",stdin);

  int test, cases = 1;

  scanf("%d%d", &n, &m);

  int u, v;

  FOR(i, 0, m) {
    scanf("%d%d", &u, &v);
    graph[u].pb(v);
    ids[u].pb(i);
  }

  solve();

  return 0;
}