Problem p01421 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
int flow[310], path[310], map[310][310], maps[310][310], arsum[100010];
int start, end, n, m, sum;
int que[2][310], t[2], h[2];
int BFS(void) {
  int node;
  t[0] = h[0] = 0;
  que[0][t[0]++] = start;
  int i = 1, j;
  memset(path, -1, sizeof(path));
  flow[start] = 110;
  while (i) {
    // puts("2");
    h[i & 1] = t[i & 1] = 0;
    while (h[(i - 1) & 1] < t[(i - 1) & 1]) {
      node = que[(i - 1) & 1][h[(i - 1) & 1]++];
      for (j = 1; j <= n; j++)
        if (j != start && map[node][j] > 0 && path[j] == -1) {
          // puts("3");
          if (flow[node] < map[node][j])
            flow[j] = flow[node];
          else
            flow[j] = map[node][j];
          path[j] = node;
          que[i & 1][t[i & 1]++] = j;
        }
    }
    if (h[i & 1] == t[i & 1] || path[end] != -1)
      break;
    i++;
  }
  // printf("%d\\n",path[end]);
  if (path[end] == -1)
    return -1;
  return flow[end];
}
int EK(void) {
  int temp, ans = 0, pre, d;
  while ((temp = BFS()) != -1) {
    // puts("1");
    ans += temp;
    d = end;
    pre = path[d];
    if (maps[pre][d])
      arsum[sum++] = maps[pre][d];
    while (d != start) {
      map[pre][d] -= temp;
      map[d][pre] += temp;
      d = pre, pre = path[d];
      if (maps[pre][d])
        arsum[sum++] = maps[pre][d];
    }
  }
  return ans;
}
int main() {
  int i, a, b;

  while (scanf("%d%d", &n, &m) != EOF) {
    memset(map, 0, sizeof(map));
    memset(maps, 0, sizeof(maps));
    for (i = 1; i <= m; i++) {
      scanf("%d%d", &a, &b);
      maps[b][a] = i;
      map[a][b] = 1;
      map[b][a] = 1;
    }
    scanf("%d%d", &start, &end);
    sum = 0;
    printf("%d\\n", EK());
    sort(arsum, arsum + sum);
    printf("%d\\n", sum);
    for (i = 0; i < sum; i++)
      if (i == 0)
        printf("%d\\n", arsum[i]);
      else if (arsum[i] != arsum[i - 1])
        printf("%d\\n", arsum[i]);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <ctype.h>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

#define maxn 309
#define inf 1000000
//------------------------Dinic模板
struct edge {
  int v, Next;
  int val;
} net[100010];

int level[maxn], Qu[maxn], out[maxn], Next[maxn];
class Dinic {
public:
  int end;
  Dinic() {
    end = 0;
    memset(Next, -1, sizeof(Next));
  }
  inline void insert(int x, int y, int c) {
    net[end].v = y, net[end].val = c, net[end].Next = Next[x], Next[x] = end++;
    net[end].v = x, net[end].val = c, net[end].Next = Next[y], Next[y] = end++;
  }
  bool BFS(int S, int E) {
    memset(level, -1, sizeof(level));
    int low = 0, high = 1;
    Qu[0] = S, level[S] = 0;
    for (; low < high;) {
      int x = Qu[low];
      for (int i = Next[x]; i != -1; i = net[i].Next) {
        if (net[i].val == 0)
          continue;
        int y = net[i].v;
        if (level[y] == -1) {
          level[y] = level[x] + 1;
          Qu[high++] = y;
        }
      }
      low++;
    }
    return level[E] != -1;
  }
  int MaxFlow(int S, int E) {
    int maxflow = 0;
    for (; BFS(S, E);) {
      memcpy(out, Next, sizeof(out));
      int now = -1;
      for (;;) {
        if (now < 0) {
          int cur = out[S];
          for (; cur != -1; cur = net[cur].Next)
            if (net[cur].val && out[net[cur].v] != -1 && level[net[cur].v] == 1)
              break;
          if (cur >= 0)
            Qu[++now] = cur, out[S] = net[cur].Next;
          else
            break;
        }
        int u = net[Qu[now]].v;
        if (u == E) {
          int flow = inf;
          int index = -1;
          for (int i = 0; i <= now; i++) {
            if (flow > net[Qu[i]].val)
              flow = net[Qu[i]].val, index = i;
          }
          maxflow += flow;
          for (int i = 0; i <= now; i++)
            net[Qu[i]].val -= flow, net[Qu[i] ^ 1].val += flow;
          for (int i = 0; i <= now; i++) {
            if (net[Qu[i]].val == 0) {
              now = index - 1;
              break;
            }
          }
        } else {
          int cur = out[u];
          for (; cur != -1; cur = net[cur].Next)
            if (net[cur].val && out[net[cur].v] != -1 &&
                level[u] + 1 == level[net[cur].v])
              break;
          if (cur != -1)
            Qu[++now] = cur, out[u] = net[cur].Next;
          else
            out[u] = -1, now--;
        }
      }
    }
    return maxflow;
  }
};
int Map[309][309];
int main() {

  int n, m;
  int u, v, s, t;

  while (scanf("%d%d", &n, &m) != EOF) {
    memset(Map, 0, sizeof(Map));
    int cnt = 1;
    Dinic ss;

    for (int i = 0; i < m; i++) {
      scanf("%d%d", &u, &v);
      Map[u][v] = cnt++;
      ss.insert(u, v, 1);
      // ss.insert(v,u,1);
    }
    cin >> s >> t;
    cout << ss.MaxFlow(s, t) << endl;
    int ans[309];
    cnt = 0;
    for (int i = 0; i < ss.end; i++) {
      if (net[i].val > 0 && i % 2 == 0)
        ans[cnt++] = i / 2 + 1;
    }
    cout << cnt << endl;
    for (int i = 0; i < cnt; i++)
      printf("%d\\n", ans[i]);
  }

  return 0;
}