Problem p03091 (C++)

Buggy submission (status: Runtime Error):

#include "bits/stdc++.h"
using namespace std;
#pragma warning(disable : 4996)

int dfs(const vector<vector<int>> &edges, int now, int from, vector<int> ss) {
  if (now == ss[0])
    return 0;
  else if (now == ss[1])
    return 1;
  for (auto e : edges[now]) {
    if (e != from)
      return dfs(edges, e, now, ss);
  }
  assert(false);
}
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> edges(N);
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }
  bool ok = true;
  for (int i = 0; i < N; ++i) {
    if (edges[i].size() == 0)
      ok = false;
    else if (edges[i].size() % 2 == 1)
      ok = false;
  }
  if (ok) {
    bool aok = false;
    int num = 0;
    for (int i = 0; i < N; ++i) {
      if (edges[i].size() >= 6)
        aok = true;
      else if (edges[i].size() >= 4)
        num++;
    }
    if (aok || num >= 3)
      aok = true;
    else if (num == 2) {
      vector<int> ss;
      for (int i = 0; i < N; ++i) {
        if (edges[i].size() == 4) {
          ss.push_back(i);
        }
      }

      int to_same_num = 0;
      for (int a = 0; a < 2; ++a) {
        int now = ss[a];
        for (auto e : edges[now]) {
          int to = dfs(edges, e, now, ss);
          if (a == to)
            to_same_num++;
        }
      }
      if (to_same_num == 4)
        aok = true;
      else {
        assert(to_same_num == 0);
        aok = false;
      }
    } else {
      assert(false);
    }
    if (aok) {
      ok = true;
    } else {
      ok = false;
    }
  }
  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}


Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#pragma warning(disable : 4996)

int dfs(const vector<vector<int>> &edges, int now, int from, vector<int> ss) {
  if (now == ss[0])
    return 0;
  else if (now == ss[1])
    return 1;
  for (auto e : edges[now]) {
    if (e != from)
      return dfs(edges, e, now, ss);
  }
  assert(false);
}
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> edges(N);
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }
  bool ok = true;
  for (int i = 0; i < N; ++i) {
    if (edges[i].size() == 0)
      ok = false;
    else if (edges[i].size() % 2 == 1)
      ok = false;
  }
  if (ok) {
    bool aok = false;
    int num = 0;
    for (int i = 0; i < N; ++i) {
      if (edges[i].size() >= 6)
        aok = true;
      else if (edges[i].size() >= 4)
        num++;
    }
    if (aok || num >= 3)
      aok = true;
    else if (num == 2) {
      vector<int> ss;
      for (int i = 0; i < N; ++i) {
        if (edges[i].size() == 4) {
          ss.push_back(i);
        }
      }

      int to_same_num = 0;
      for (int a = 0; a < 2; ++a) {
        int now = ss[a];
        for (auto e : edges[now]) {
          int to = dfs(edges, e, now, ss);
          if (a == to)
            to_same_num++;
        }
      }
      if (to_same_num == 4)
        aok = true;
      else {
        assert(to_same_num == 0);
        aok = false;
      }
    } else {
      // assert(false);
    }
    if (aok) {
      ok = true;
    } else {
      ok = false;
    }
  }
  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}