Problem p03091 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define MOD (1000000007l)
#define rep(i, n) for (long i = 0; i < n; i++)

using namespace std;

int main(void) {
  long N, M;
  cin >> N >> M;
  unordered_map<long, unordered_set<long>> connect;
  rep(i, M) {
    long a, b;
    cin >> a >> b;
    connect[a].insert(b);
    connect[b].insert(a);
  }
  long odd_nodes = 0;
  long four_nodes = 0;
  long six_nodes = 0;
  vector<long> four_node_indexes;
  for (auto it : connect) {
    long size = it.second.size();
    if (size % 2 == 1)
      odd_nodes++;
    if (size == 4) {
      four_nodes++;
      four_node_indexes.push_back(it.first);
    }
    if (size % 2 == 0 and size >= 6)
      six_nodes++;
  }
  if (odd_nodes != 0) {
    cout << "No" << endl;
    return 0;
  }
  if (six_nodes != 0) {
    cout << "Yes" << endl;
    return 0;
  }
  if (four_nodes < 2) {
    cout << "No" << endl;
    return 0;
  }
  if (four_nodes > 2) {
    cout << "Yes" << endl;
    return 0;
  }
  for (auto it : connect[four_node_indexes[0]]) {
    long current = it;
    unordered_set<long> walked;
    walked.insert(four_node_indexes[0]);
    while (true) {
    }
    if (current == four_node_indexes[1]) {
      break;
    }
    unordered_set<long> neibor = connect[current];
    auto it2 = neibor.begin();
    long foo = *it2;
    it2++;
    long bar = *it2;
    if (walked.count(foo) != 0 and walked.count(bar) != 0) {
      cout << "Yes" << endl;
      return 0;
      long next = walked.count(foo) == 0 ? foo : bar;
      walked.insert(current);
      current = next;
    }
  }
  cout << "No" << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define MOD (1000000007l)
#define rep(i, n) for (long i = 0; i < n; i++)

using namespace std;

int main(void) {
  long N, M;
  cin >> N >> M;
  unordered_map<long, unordered_set<long>> connect;
  rep(i, M) {
    long a, b;
    cin >> a >> b;
    connect[a].insert(b);
    connect[b].insert(a);
  }
  long odd_nodes = 0;
  long four_nodes = 0;
  long six_nodes = 0;
  vector<long> four_node_indexes;
  for (auto it : connect) {
    long size = it.second.size();
    if (size % 2 == 1)
      odd_nodes++;
    if (size == 4) {
      four_nodes++;
      four_node_indexes.push_back(it.first);
    }
    if (size % 2 == 0 and size >= 6)
      six_nodes++;
  }
  if (odd_nodes != 0) {
    cout << "No" << endl;
    return 0;
  }
  if (six_nodes != 0) {
    cout << "Yes" << endl;
    return 0;
  }
  if (four_nodes < 2) {
    cout << "No" << endl;
    return 0;
  }
  if (four_nodes > 2) {
    cout << "Yes" << endl;
    return 0;
  }
  for (auto it : connect[four_node_indexes[0]]) {
    long current = it;
    unordered_set<long> walked;
    walked.insert(four_node_indexes[0]);
    while (true) {
      if (current == four_node_indexes[1]) {
        break;
      }
      unordered_set<long> neibor = connect[current];
      auto it2 = neibor.begin();
      long foo = *it2;
      it2++;
      long bar = *it2;
      if (walked.count(foo) != 0 and walked.count(bar) != 0) {
        cout << "Yes" << endl;
        return 0;
      }
      long next = walked.count(foo) == 0 ? foo : bar;
      walked.insert(current);
      current = next;
    }
  }
  cout << "No" << endl;
  return 0;
}