Problem p03414 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
const int N = 1005;
map<pair<int, int>, int> Map;
int Ans[N], ne[N], tot, m, n, x, y, f[N][N], Min[N], Max[N], flag[N], fi[N],
    zz[N];
vector<int> G[N];
void jb(int x, int y) {
  ne[++tot] = fi[x];
  fi[x] = tot;
  zz[tot] = y;
}
void dfs(int x) {
  flag[x] = 1;
  for (int i = 0; i < (int)G[x].size(); i++)
    if (!flag[G[x][i]])
      dfs(G[x][i]);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    G[x].push_back(y);
    Map[mp(x, y)] = i;
  }
  for (int i = 1; i <= n; i++) {
    memset(flag, 0, sizeof flag);
    dfs(i);
    for (int j = 1; j <= n; j++)
      f[i][j] = flag[j];
  }
  for (int i = 1; i <= n; i++) {
    memset(Min, -1, sizeof Min);
    memset(Max, -1, sizeof Max);
    memset(flag, 0, sizeof flag);
    flag[i] = 1;
    for (int j = 0; j < G[i].size(); j++)
      if (!flag[G[i][j]]) {
        dfs(G[i][j]);
        for (int k = 1; k <= n; k++)
          if (Min[k] == -1 && flag[k])
            Min[k] = j;
      }
    memset(flag, 0, sizeof flag);
    flag[i] = 1;
    for (int j = G[i].size() - 1; j >= 0; j--)
      if (!flag[G[i][j]]) {
        dfs(G[i][j]);
        for (int k = 1; k <= n; k++)
          if (Max[k] == -1 && flag[k])
            Max[k] = j;
      }
    for (int j = 0; j < G[i].size(); j++)
      if ((Max[G[i][j]] != j || Min[G[i][j]] != j) ^ f[G[i][j]][i])
        Ans[Map[mp(i, G[i][j])]] = 1;
  }
  for (int i = 1; i <= m; i++)
    if (!Ans[i])
      puts("same");
    else
      puts("diff");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
const int N = 1005;
map<pair<int, int>, int> Map;
int Ans[200005], ne[N], tot, m, n, x, y, f[N][N], Min[N], Max[N], flag[N],
    fi[N], zz[N];
vector<int> G[N];
void jb(int x, int y) {
  ne[++tot] = fi[x];
  fi[x] = tot;
  zz[tot] = y;
}
void dfs(int x) {
  flag[x] = 1;
  for (int i = 0; i < (int)G[x].size(); i++)
    if (!flag[G[x][i]])
      dfs(G[x][i]);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    G[x].push_back(y);
    Map[mp(x, y)] = i;
  }
  for (int i = 1; i <= n; i++) {
    memset(flag, 0, sizeof flag);
    dfs(i);
    for (int j = 1; j <= n; j++)
      f[i][j] = flag[j];
  }
  for (int i = 1; i <= n; i++) {
    memset(Min, -1, sizeof Min);
    memset(Max, -1, sizeof Max);
    memset(flag, 0, sizeof flag);
    flag[i] = 1;
    for (int j = 0; j < G[i].size(); j++)
      if (!flag[G[i][j]]) {
        dfs(G[i][j]);
        for (int k = 1; k <= n; k++)
          if (Min[k] == -1 && flag[k])
            Min[k] = j;
      }
    memset(flag, 0, sizeof flag);
    flag[i] = 1;
    for (int j = G[i].size() - 1; j >= 0; j--)
      if (!flag[G[i][j]]) {
        dfs(G[i][j]);
        for (int k = 1; k <= n; k++)
          if (Max[k] == -1 && flag[k])
            Max[k] = j;
      }
    for (int j = 0; j < G[i].size(); j++)
      if ((Max[G[i][j]] != j || Min[G[i][j]] != j) ^ f[G[i][j]][i])
        Ans[Map[mp(i, G[i][j])]] = 1;
  }
  for (int i = 1; i <= m; i++)
    if (!Ans[i])
      puts("same");
    else
      puts("diff");
  return 0;
}