Problem p03680 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

typedef signed long long int slli;
typedef unsigned long long int ulli;
const slli MAX_SLLI = 0x7FFFFFFFFFFFFFFF;
const slli MIN_SLLI = 0x8000000000000000;
const ulli MAX_ULLI = 0xFFFFFFFFFFFFFFFF;
const ulli MIN_ULLI = 0x0000000000000000;

#define N_TIMES(i, n) for (ulli i = 0; i < n; ++i)
#define N_TIMES_REV(i, n) for (slli i = n - 1; i >= 0; --i)

#ifdef __DEBUG__
template <typename T>
std::ostream &operator<<(std::ostream &os, const std::list<T> &list) {
  const std::string delim = ", ";
  typename std::list<T>::const_iterator itr = list.begin();
  while (itr != list.end()) {
    os << *itr;
    ++itr;
    if (itr != list.end())
      os << delim;
  }
  return os;
}

template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
  const std::string delim = ", ";
  for (unsigned n = 0; n < v.size(); ++n) {
    os << v[n];
    if ((n + 1) < v.size())
      os << delim;
  }
  return os;
}
#endif

int search(const std::vector<unsigned> &a) {
  unsigned N = a.size();

  std::vector<bool> f(N, false);
  f[0] = true;

  unsigned pos = 0, count = 0;
  while (pos != 1) {
    unsigned next = a[pos];

    if (f[next])
      return -1;

    ++count;

    pos = next;
  }

  return count;
}

int main() {
  unsigned N;
  std::cin >> N;

  std::vector<unsigned> a(N);
  for (unsigned n = 0; n < N; ++n) {
    std::cin >> a[n];
    --a[n];
  }

  std::cout << search(a) << std::endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

typedef signed long long int slli;
typedef unsigned long long int ulli;
const slli MAX_SLLI = 0x7FFFFFFFFFFFFFFF;
const slli MIN_SLLI = 0x8000000000000000;
const ulli MAX_ULLI = 0xFFFFFFFFFFFFFFFF;
const ulli MIN_ULLI = 0x0000000000000000;

#define N_TIMES(i, n) for (ulli i = 0; i < n; ++i)
#define N_TIMES_REV(i, n) for (slli i = n - 1; i >= 0; --i)

#ifdef __DEBUG__
template <typename T>
std::ostream &operator<<(std::ostream &os, const std::list<T> &list) {
  const std::string delim = ", ";
  typename std::list<T>::const_iterator itr = list.begin();
  while (itr != list.end()) {
    os << *itr;
    ++itr;
    if (itr != list.end())
      os << delim;
  }
  return os;
}

template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
  const std::string delim = ", ";
  for (unsigned n = 0; n < v.size(); ++n) {
    os << v[n];
    if ((n + 1) < v.size())
      os << delim;
  }
  return os;
}
#endif

int search(const std::vector<unsigned> &a) {
  unsigned N = a.size();

  std::vector<bool> f(N, false);
  f[0] = true;

  unsigned pos = 0, count = 0;
  while (pos != 1) {
    unsigned next = a[pos];

    if (f[next])
      return -1;

    f[next] = true;

    ++count;

    pos = next;
  }

  return count;
}

int main() {
  unsigned N;
  std::cin >> N;

  std::vector<unsigned> a(N);
  for (unsigned n = 0; n < N; ++n) {
    std::cin >> a[n];
    --a[n];
  }

  std::cout << search(a) << std::endl;

  return 0;
}