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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define eps 1e-9
#define ALL(x) x.begin(), x.end()
#define INS(x) inserter(x, x.begin())
#define rep(i, j, k) for (int i = j; i <= k; i++)
#define MAXN 10005
#define MAXM 40005
#define INF 0x3fffffff
#define PB push_back
#define MP make_pair
#define X first
#define Y second
#define lc (k << 1)
#define rc ((k << 1) | 1)
#define mii ((l + r) >> 1)
#define fk puts("fuck!")
#define clr(x, y) memset(x, y, sizeof(x))
using namespace std;
typedef long long LL;
int i, j, k, n, m, x, y, T, mx, mi, cas, num, len;
bool flag;

const int inf = 0x3f3f3f3f;
struct edgenode {
  int from, to, next, flag;
  int cap;
} edge[MAXM];
int Edge, head[MAXN], ps[MAXN], dep[MAXN];

void add_edge(int x, int y, int c, int flag) {
  edge[Edge].from = x;
  edge[Edge].to = y;
  edge[Edge].cap = c;
  edge[Edge].flag = flag;
  edge[Edge].next = head[x];
  head[x] = Edge++;

  edge[Edge].from = y;
  edge[Edge].to = x;
  edge[Edge].cap = 0;
  edge[Edge].flag = flag;
  edge[Edge].next = head[y];
  head[y] = Edge++;
}

int dinic(int n, int s, int t) {
  int tr, flow = 0;
  int i, j, k, l, r, top;
  while (1) {
    memset(dep, -1, (n + 1) * sizeof(int));
    for (l = dep[ps[0] = s] = 0, r = 1; l != r;) // BFS部分，将?定?分?
    {
      for (i = ps[l++], j = head[i]; j != -1; j = edge[j].next) {
        if (edge[j].cap && -1 == dep[k = edge[j].to]) {
          dep[k] = dep[i] + 1;
          ps[r++] = k;
          if (k == t) {
            l = r;
            break;
          }
        }
      }
    }
    if (dep[t] == -1)
      break;

    for (i = s, top = 0;;) // DFS部分
    {
      if (i == t) // 当前点就是?点?
      {
        for (k = 0, tr = inf; k < top; ++k)
          if (edge[ps[k]].cap < tr)
            tr = edge[ps[l = k]].cap;

        for (k = 0; k < top; ++k)
          edge[ps[k]].cap -= tr, edge[ps[k] ^ 1].cap += tr;

        flow += tr;
        i = edge[ps[top = l]].from;
      }

      for (j = head[i]; j != -1; j = edge[j].next) // 找当前点所指向的点
        if (edge[j].cap && dep[i] + 1 == dep[edge[j].to])
          break;

      if (j != -1) {
        ps[top++] = j; // 当前点有所指向的点，把?个点加入?中
        i = edge[j].to;
      } else {
        if (!top)
          break; // 当前点没有指向的点，回溯
        dep[i] = -1;
        i = edge[ps[--top]].from;
      }
    }
  }
  return flow;
}

vector<int> ans;
int main() {
  memset(head, -1, sizeof(head));
  Edge = 0;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    add_edge(x, y, 1, 1);
    add_edge(y, x, 1, 0);
  }
  int S, T;
  scanf("%d%d", &S, &T);
  printf("%d\\n", dinic(n + 3, S, T));

  ans.clear();
  for (i = 0; i < Edge; i += 2) {
    if (edge[i].flag == 0 && edge[i].cap == 0) {
      ans.PB(i / 4 + 1);
    }
  }
  printf("%d\\n", ans.size());
  for (i = 0; i < ans.size(); i++) {
    printf("%d\\n", ans[i]);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int maxn = 800;
const int INF = 199999999;
struct Edge {
  int from, to, cap, flow;
  int flag;
};
struct Dinic {
  int n, m, s, t;
  vector<Edge> edges; // ?数的?倍
  vector<int> G[maxn]; // ?接表，G[i][j]表示?点i的第j条?在e数?中的序号
  bool vis[maxn];      // BFS使用
  int d[maxn];         // 从起点到i的距?
  int cur[maxn];       // 当前弧指?
  void ClearAll(int n) {
    for (int i = 0; i < n; i++)
      G[i].clear();
    edges.clear();
  }
  void ClearFlow() {
    for (int i = 0; i < edges.size(); i++)
      edges[i].flow = 0;
  }

  void AddEdge(int from, int to, int cap, int sign) {
    edges.push_back((Edge){from, to, cap, 0, sign});
    edges.push_back((Edge){to, from, cap, 0, -sign});
    m = edges.size();
    G[from].push_back(m - 2);
    G[to].push_back(m - 1);
  }
  bool BFS() {
    memset(vis, 0, sizeof(vis));
    queue<int> Q;
    Q.push(s);
    vis[s] = 1;
    d[s] = 0;
    while (!Q.empty()) {
      int x = Q.front();
      Q.pop();
      for (int i = 0; i < G[x].size(); i++) {
        Edge &e = edges[G[x][i]];
        if (!vis[e.to] && e.cap > e.flow) {
          vis[e.to] = 1;
          d[e.to] = d[x] + 1;
          Q.push(e.to);
        }
      }
    }
    return vis[t];
  }

  int DFS(int x, int a) {
    if (x == t || a == 0)
      return a;
    int flow = 0, f;
    for (int &i = cur[x]; i < G[x].size(); i++) {
      Edge &e = edges[G[x][i]];
      if (d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
        e.flow += f;
        edges[G[x][i] ^ 1].flow -= f;
        flow += f;
        a -= f;
        if (a == 0)
          break;
      }
    }
    return flow;
  }

  int Maxflow(int s, int t) {
    this->s = s;
    this->t = t;
    int flow = 0;
    while (BFS()) {
      memset(cur, 0, sizeof(cur));
      flow += DFS(s, INF);
    }
    return flow;
  }
  vector<int> Mincut() // call this after maxflow
  {
    vector<int> ans;
    for (int i = 0; i < edges.size(); i++) {
      Edge &e = edges[i];
      if (vis[e.from] && !vis[e.to] && e.cap > 0)
        ans.push_back(i);
    }
    return ans;
  }
  void Reduce() {
    for (int i = 0; i < edges.size(); i++)
      edges[i].cap -= edges[i].flow;
  }
};
Dinic g;

vector<int> q;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  g.n = n;
  g.ClearAll(n + m + 2);
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    g.AddEdge(a, b, 1, i + 1);
  }
  int x, y;
  scanf("%d%d", &x, &y);
  int fin = g.Maxflow(x, y);
  printf("%d\\n", fin);
  for (int i = 0; i < g.edges.size(); i++) {
    // cout<<g.edges[i].flow<<endl;
    if (g.edges[i].flow == 1) {
      if (g.edges[i].flag > 0)
        continue;
      else
        q.push_back(-g.edges[i].flag);
    }
  }
  printf("%d\\n", q.size());
  sort(q.begin(), q.end());
  for (int i = 0; i < q.size(); i++)
    printf("%d\\n", q[i]);
}