Problem p00596 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

const int V = 7;
typedef vector<int> node;

bool judge(vector<node> &G) {
  bool exist[V] = {};
  REP(i, V) exist[i] = (G[i].size() > 0);
  int s = -1;
  REP(i, V) if (exist[i]) s = i;
  assert(s != -1);
  queue<int> que;
  bool used[V] = {};
  que.push(s);
  used[s] = true;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    REP(i, G[u].size()) {
      int v = G[u][i];
      if (used[v])
        continue;
      used[v] = true;
      que.push(v);
    }
  }
  REP(i, V) if (exist[i] && !used[i]) return false;
  int odd = 0;
  REP(i, V) if (G[i].size() % 2 == 1) odd++;
  return (odd == 2 || odd == 0);
}

int main() {
  int N;
  while (cin >> N && N) {
    vector<node> G(6);
    REP(i, N) {
      string s;
      cin >> s;
      int u = s[0] - '0';
      int v = s[1] - '0';
      G[u].push_back(v);
      G[v].push_back(u);
    }
    if (judge(G))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

const int V = 7;
typedef vector<int> node;

bool judge(vector<node> &G) {
  bool exist[V] = {};
  REP(i, V) exist[i] = (G[i].size() > 0);
  int s = -1;
  REP(i, V) if (exist[i]) s = i;
  assert(s != -1);
  queue<int> que;
  bool used[V] = {};
  que.push(s);
  used[s] = true;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    REP(i, G[u].size()) {
      int v = G[u][i];
      if (used[v])
        continue;
      used[v] = true;
      que.push(v);
    }
  }
  REP(i, V) if (exist[i] && !used[i]) return false;
  int odd = 0;
  REP(i, V) if (G[i].size() % 2 == 1) odd++;
  return (odd == 2 || odd == 0);
}

int main() {
  int N;
  while (cin >> N && N) {
    vector<node> G(7);
    REP(i, N) {
      string s;
      cin >> s;
      int u = s[0] - '0';
      int v = s[1] - '0';
      G[u].push_back(v);
      G[v].push_back(u);
    }
    if (judge(G))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}