Problem p03414 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <vector>
#define N 1005
#define M 200005
using namespace std;
int read() {
  char c = getchar();
  while (c < '0' || c > '9') {
    continue;
  }
  int num = c ^ 48;
  c = getchar();
  while (c >= '0' && c <= '9') {
    num = num * 10 + (c ^ 48);
    c = getchar();
  }
  return num;
}
vector<int> edge[N];
int p1[N][N];
void dfs1(int now, int p) {
  for (vector<int>::iterator it = edge[now].begin(); it != edge[now].end();
       ++it) {
    int v = *it;
    if (p1[p][v]) {
      continue;
    }
    p1[p][v] = p1[p][now];
    dfs1(v, p);
  }
}
int p2[N][N];
void dfs2(int now, int p) {
  for (vector<int>::iterator it = edge[now].begin(); it != edge[now].end();
       ++it) {
    int v = *it;
    if (p2[p][v]) {
      continue;
    }
    p2[p][v] = p2[p][now];
    dfs2(v, p);
  }
}
int u[M], v[M];
int main() {
  int n, m;
  n = read();
  m = read();
  for (int i = 0; i < m; ++i) {
    u[i] = read();
    v[i] = read();
    edge[u[i]].push_back(v[i]);
  }
  for (int i = 1; i <= n; ++i) {
    p1[i][i] = i;
    for (vector<int>::iterator it = edge[i].begin(); it != edge[i].end();
         ++it) {
      int v = *it;
      if (p1[i][v]) {
        continue;
      }
      p1[i][v] = v;
      dfs1(v, i);
    }
    p2[i][i] = i;
    for (vector<int>::iterator it = --edge[i].end();; --it) {
      int v = *it;
      if (p2[i][v]) {
        if (it == edge[i].begin()) {
          break;
        }
        continue;
      }
      p2[i][v] = v;
      dfs2(v, i);
      if (it == edge[i].begin()) {
        break;
      }
    }
  }
  for (int i = 0; i < m; ++i) {
    int flag = 0;
    if (p1[v[i]][u[i]]) {
      flag ^= 1;
    }
    if (p1[u[i]][v[i]] != p2[u[i]][v[i]]) {
      flag ^= 1;
    }
    if (flag) {
      printf("diff\n");
    } else {
      printf("same\n");
    }
  }
}

Fixed submission:
#include <cstdio>
#include <vector>
#define N 1005
#define M 200005
using namespace std;
int read() {
  char c = getchar();
  while (c < '0' || c > '9') {
    continue;
  }
  int num = c ^ 48;
  c = getchar();
  while (c >= '0' && c <= '9') {
    num = num * 10 + (c ^ 48);
    c = getchar();
  }
  return num;
}
vector<int> edge[N];
int p1[N][N];
void dfs1(int now, int p) {
  for (vector<int>::iterator it = edge[now].begin(); it != edge[now].end();
       ++it) {
    int v = *it;
    if (p1[p][v]) {
      continue;
    }
    p1[p][v] = p1[p][now];
    dfs1(v, p);
  }
}
int p2[N][N];
void dfs2(int now, int p) {
  for (vector<int>::iterator it = edge[now].begin(); it != edge[now].end();
       ++it) {
    int v = *it;
    if (p2[p][v]) {
      continue;
    }
    p2[p][v] = p2[p][now];
    dfs2(v, p);
  }
}
int u[M], v[M];
int main() {
  int n, m;
  n = read();
  m = read();
  for (int i = 0; i < m; ++i) {
    u[i] = read();
    v[i] = read();
    edge[u[i]].push_back(v[i]);
  }
  for (int i = 1; i <= n; ++i) {
    p1[i][i] = i;
    for (vector<int>::iterator it = edge[i].begin(); it != edge[i].end();
         ++it) {
      int v = *it;
      if (p1[i][v]) {
        continue;
      }
      p1[i][v] = v;
      dfs1(v, i);
    }
    p2[i][i] = i;
    if (edge[i].begin() == edge[i].end()) {
      continue;
    }
    for (vector<int>::iterator it = --edge[i].end();; --it) {
      int v = *it;
      if (p2[i][v]) {
        if (it == edge[i].begin()) {
          break;
        }
        continue;
      }
      p2[i][v] = v;
      dfs2(v, i);
      if (it == edge[i].begin()) {
        break;
      }
    }
  }
  for (int i = 0; i < m; ++i) {
    int flag = 0;
    if (p1[v[i]][u[i]]) {
      flag ^= 1;
    }
    if (p1[u[i]][v[i]] != p2[u[i]][v[i]]) {
      flag ^= 1;
    }
    if (flag) {
      printf("diff\n");
    } else {
      printf("same\n");
    }
  }
}