Problem p02678 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define DEBUG fprintf(stderr, "Passing [%s] line %d\\n", __FUNCTION__, __LINE__)
#define File(x)                                                                \\
  freopen(x ".in", "r", stdin);                                                \\
  freopen(x ".out", "w", stdout)

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;

inline int gi() {
  int f = 1, x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return f * x;
}

inline LL gl() {
  LL f = 1, x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return f * x;
}

const int INF = 0x3f3f3f3f, N = 200003, M = N << 1;

int h, n, m;
int fa[N];
int tot, head[N], ver[M], nxt[M];
bool vis[N];
int ans[N], bu[N];

inline void add(int u, int v) {
  ver[++tot] = v, nxt[tot] = head[u], head[u] = tot;
}

int main() {
  // File("");
  n = gi(), m = gi();
  for (int i = 1; i <= m; i += 1) {
    int u = gi(), v = gi();
    add(u, v), add(v, u);
  }
  queue<int> q;
  vis[1] = true;
  q.push(1);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; i; i = nxt[i]) {
      int v = ver[i];
      if (vis[v])
        continue;
      ans[v] = u;
      vis[v] = true;
      q.push(v);
    }
  }
  for (int i = 2; i <= n; i += 1) {
    if (!vis[i]) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  for (int i = 2; i <= n; i += 1)
    printf("%d\\n", ans[i]);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define DEBUG fprintf(stderr, "Passing [%s] line %d\\n", __FUNCTION__, __LINE__)
#define File(x)                                                                \\
  freopen(x ".in", "r", stdin);                                                \\
  freopen(x ".out", "w", stdout)

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;

inline int gi() {
  int f = 1, x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return f * x;
}

inline LL gl() {
  LL f = 1, x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return f * x;
}

const int INF = 0x3f3f3f3f, N = 200003, M = N << 1;

int h, n, m;
int fa[N];
int tot, head[N], ver[M], nxt[M];
bool vis[N];
int ans[N], bu[N];

inline void add(int u, int v) {
  ver[++tot] = v, nxt[tot] = head[u], head[u] = tot;
}

void dfs(int u, int lst, int bs) {
  ans[u] = lst, bu[u] = bs;
  for (int i = head[u]; i; i = nxt[i]) {
    int v = ver[i];
    if (bu[v] <= bs)
      continue;
    dfs(v, u, bs + 1);
  }
}

int main() {
  // File("");
  n = gi(), m = gi();
  for (int i = 1; i <= m; i += 1) {
    int u = gi(), v = gi();
    add(u, v), add(v, u);
  }
  memset(bu, 0x3f, sizeof bu);
  dfs(1, 1, 0);
  for (int i = 2; i <= n; i += 1) {
    if (ans[i] == 0) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  for (int i = 2; i <= n; i += 1)
    printf("%d\\n", ans[i]);
  return 0;
}