Problem p03746 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

typedef long long LL;
typedef pair<int, int> P2;
typedef pair<pair<int, int>, int> P3;
typedef pair<pair<int, int>, pair<int, int>> P4;
#define Fst first
#define Snd second
#define PB(a) push_back(a)
#define MP(a, b) make_pair((a), (b))
#define M3P(a, b, c) make_pair(make_pair((a), (b)), (c))
#define M4P(a, b, c, d) make_pair(make_pair((a), (b)), make_pair((c), (d)))
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define repm(i, a, b) for (int i = (int)(a); i > (int)(b); --i)

int N, M;
vector<int> V[100010];
stack<int> S;
queue<int> Q;
bool p[100010];

int main() {
  scanf("%d%d", &N, &M);
  repp(i, 0, M) {
    int a, b;
    scanf("%d%d", &a, &b);
    V[a].PB(b);
    V[b].PB(a);
  }
  int z = 1;
  while (V[z].size() == 0)
    ++z;
  int x = z;
  p[z] = 1;
  int ans = 1;
  while (1) {
    bool b = 1;
    for (auto u : V[x]) {
      if (p[u])
        continue;
      S.push(u);
      p[u] = 1;
      x = u;
      b = 0;
      ++ans;
      break;
    }
    if (b)
      break;
  }
  x = z;
  while (1) {
    bool b = 1;
    for (auto u : V[x]) {
      if (p[u])
        continue;
      Q.push(u);
      x = u;
      b = 0;
      ++ans;
      break;
    }
    if (b)
      break;
  }
  printf("%d\n", ans);
  while (!S.empty()) {
    printf("%d ", S.top());
    S.pop();
  }
  printf("%d", z);
  while (!Q.empty()) {
    printf(" %d", Q.front());
    Q.pop();
  }
  printf("\n");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

typedef long long LL;
typedef pair<int, int> P2;
typedef pair<pair<int, int>, int> P3;
typedef pair<pair<int, int>, pair<int, int>> P4;
#define Fst first
#define Snd second
#define PB(a) push_back(a)
#define MP(a, b) make_pair((a), (b))
#define M3P(a, b, c) make_pair(make_pair((a), (b)), (c))
#define M4P(a, b, c, d) make_pair(make_pair((a), (b)), make_pair((c), (d)))
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define repm(i, a, b) for (int i = (int)(a); i > (int)(b); --i)

int N, M;
vector<int> V[100010];
stack<int> S;
queue<int> Q;
bool p[100010];

int main() {
  scanf("%d%d", &N, &M);
  repp(i, 0, M) {
    int a, b;
    scanf("%d%d", &a, &b);
    V[a].PB(b);
    V[b].PB(a);
  }
  int z = 1;
  while (V[z].size() == 0)
    ++z;
  int x = z;
  p[z] = 1;
  int ans = 1;
  while (1) {
    bool b = 1;
    for (auto u : V[x]) {
      if (p[u])
        continue;
      S.push(u);
      p[u] = 1;
      x = u;
      b = 0;
      ++ans;
      break;
    }
    if (b)
      break;
  }
  x = z;
  while (1) {
    bool b = 1;
    for (auto u : V[x]) {
      if (p[u])
        continue;
      Q.push(u);
      p[u] = 1;
      x = u;
      b = 0;
      ++ans;
      break;
    }
    if (b)
      break;
  }
  printf("%d\n", ans);
  while (!S.empty()) {
    printf("%d ", S.top());
    S.pop();
  }
  printf("%d", z);
  while (!Q.empty()) {
    printf(" %d", Q.front());
    Q.pop();
  }
  printf("\n");
  return 0;
}