Problem p03091 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long int ll;

struct Edge {
  int to, rev;
  ll cap;
  Edge(int to, ll cap, int rev) : to(to), cap(cap), rev(rev) {}
};
// ここの値に注意！！
const int N = 20500;
const ll INF = 1e9;
vector<Edge> g[N];
int level[N];
int iter[N];
void add_edge(int s, int t, ll c) {
  g[s].push_back(Edge(t, c, g[t].size()));
  g[t].push_back(Edge(s, 0, g[s].size() - 1));
}
void bfs(int s) {
  memset(level, -1, sizeof(level));
  queue<int> q;
  level[s] = 0;
  q.push(s);
  while (q.size()) {
    int v = q.front();
    q.pop();
    for (auto &e : g[v]) {
      if (e.cap > 0 && level[e.to] < 0) {
        level[e.to] = level[v] + 1;
        q.push(e.to);
      }
    }
  }
}
ll dfs(int v, int t, ll f) {
  if (v == t)
    return f;
  for (int &i = iter[v]; i < g[v].size(); i++) {
    Edge &e = g[v][i];
    if (e.cap > 0 && level[v] < level[e.to]) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        g[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}
ll max_flow(int s, int t) {
  ll flow = 0;
  while (1) {
    bfs(s);
    if (level[t] < 0)
      return flow;
    memset(iter, 0, sizeof(iter));
    int f;
    while ((f = dfs(s, t, INF)) > 0) {
      flow += f;
    }
  }
}

vector<int> gg[100100];

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    gg[x].push_back(y);
    gg[y].push_back(x);
    add_edge(x, y, 1);
    add_edge(y, x, 1);
  }
  int six = 0;
  int four = 0;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    if (gg[i].size() % 2) {
      cout << "No" << endl;
      return 0;
    }
    if (gg[i].size() == 6)
      six++;
    if (gg[i].size() == 4) {
      four++;
      v.push_back(i);
    }
  }
  if (six) {
    cout << "Yes" << endl;
    return 0;
  }
  if (four >= 3) {
    cout << "Yes" << endl;
    return 0;
  } else {
    if (four == 2) {
      if (max_flow(v[0], v[1]) == 2) {
        cout << "Yes" << endl;
      } else {
        cout << "No" << endl;
      }
    } else {
      cout << "No" << endl;
      return 0;
    }
  }
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long int ll;

struct Edge {
  int to, rev;
  ll cap;
  Edge(int to, ll cap, int rev) : to(to), cap(cap), rev(rev) {}
};
// ここの値に注意！！
const int N = 205000;
const ll INF = 1e9;
vector<Edge> g[N];
int level[N];
int iter[N];
void add_edge(int s, int t, ll c) {
  g[s].push_back(Edge(t, c, g[t].size()));
  g[t].push_back(Edge(s, 0, g[s].size() - 1));
}
void bfs(int s) {
  memset(level, -1, sizeof(level));
  queue<int> q;
  level[s] = 0;
  q.push(s);
  while (q.size()) {
    int v = q.front();
    q.pop();
    for (auto &e : g[v]) {
      if (e.cap > 0 && level[e.to] < 0) {
        level[e.to] = level[v] + 1;
        q.push(e.to);
      }
    }
  }
}
ll dfs(int v, int t, ll f) {
  if (v == t)
    return f;
  for (int &i = iter[v]; i < g[v].size(); i++) {
    Edge &e = g[v][i];
    if (e.cap > 0 && level[v] < level[e.to]) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        g[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}
ll max_flow(int s, int t) {
  ll flow = 0;
  while (1) {
    bfs(s);
    if (level[t] < 0)
      return flow;
    memset(iter, 0, sizeof(iter));
    int f;
    while ((f = dfs(s, t, INF)) > 0) {
      flow += f;
    }
  }
}

vector<int> gg[100100];

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    gg[x].push_back(y);
    gg[y].push_back(x);
    add_edge(x, y, 1);
    add_edge(y, x, 1);
  }
  int six = 0;
  int four = 0;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    if (gg[i].size() % 2) {
      cout << "No" << endl;
      return 0;
    }
    if (gg[i].size() == 6)
      six++;
    if (gg[i].size() == 4) {
      four++;
      v.push_back(i);
    }
  }
  if (six) {
    cout << "Yes" << endl;
    return 0;
  }
  if (four >= 3) {
    cout << "Yes" << endl;
    return 0;
  } else {
    if (four == 2) {
      if (max_flow(v[0], v[1]) == 2) {
        cout << "Yes" << endl;
      } else {
        cout << "No" << endl;
      }
    } else {
      cout << "No" << endl;
      return 0;
    }
  }
}