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


#include <bits/stdc++.h>
#include <cmath>
using ll = long long;
using namespace std;
vector<int> arr;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define mins(x, y) x = min(x, y)
#define maxs(x, y) x = max(x, y)
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
static const int NIL = -1;
static const int INF = 1000000007;
#define mp make_pair
#define sz(x) int(x.sise())
#define mod 1000000007
#define reps(i, s, n) for (int i = s; i < n; i++)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
deque<int> deq;
#define fi first
#define se second
// #define int long long

// cerrはコメントアウトしなくても通る//ただしTLEを引き起こすことがある

// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
// const ll INF = 1LL << 60;
// typedef pair<ll,ll> P;
typedef pair<int, int> P;
bool chmx(int &a, int b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}

bool chmn(int &a, int b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

vector<int> to[200005];
int visited[100005];
int root[100005];
int dis[100005];
int co = 0;

void dfs(int u) {
  for (int v : to[u]) {
    if (dis[v] >= dis[u] + 1) {
      root[v] = u;
      dis[v] = dis[u] + 1;
      dfs(v);
    }
  }
  if (visited[u] == 0) {
    visited[u] = 1;
    co++;
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  rep(i, n) {
    root[i] = i;
    dis[i] = INF;
  }
  dis[0] = 0;
  qu.push(0);
  while (!qu.empty()) {
    int u = qu.front();
    qu.pop();
    for (int v : to[u]) {
      if (dis[v] > dis[u] + 1) {
        dis[v] = dis[u] + 1;
        root[v] = u;
        qu.push(v);
      }
      if (visited[v] == 0) {
        co++;
        visited[v] = 1;
      }
    }
  }

  if (co != n) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  rep2(i, n - 1) { cout << root[i] + 1 << endl; }
  return 0;
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>
#include <cmath>
using ll = long long;
using namespace std;
vector<int> arr;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define mins(x, y) x = min(x, y)
#define maxs(x, y) x = max(x, y)
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
static const int NIL = -1;
static const int INF = 1000000007;
#define mp make_pair
#define sz(x) int(x.sise())
#define mod 1000000007
#define reps(i, s, n) for (int i = s; i < n; i++)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
deque<int> deq;
#define fi first
#define se second
// #define int long long

// cerrはコメントアウトしなくても通る//ただしTLEを引き起こすことがある

// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
// const ll INF = 1LL << 60;
// typedef pair<ll,ll> P;
typedef pair<int, int> P;
bool chmx(int &a, int b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}

bool chmn(int &a, int b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

vector<int> to[200005];
int visited[100005];
int root[100005];
int dis[100005];
int co = 0;

void dfs(int u) {
  for (int v : to[u]) {
    if (dis[v] >= dis[u] + 1) {
      root[v] = u;
      dis[v] = dis[u] + 1;
      dfs(v);
    }
  }
  if (visited[u] == 0) {
    visited[u] = 1;
    co++;
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  rep(i, n) {
    root[i] = i;
    dis[i] = INF;
  }
  dis[0] = 0;
  st.push(0);
  while (!st.empty()) {
    int u = st.top();
    st.pop();
    for (int v : to[u]) {
      if (dis[v] > dis[u] + 1) {
        dis[v] = dis[u] + 1;
        root[v] = u;
        st.push(v);
      }
      if (visited[v] == 0) {
        co++;
        visited[v] = 1;
      }
    }
  }

  if (co != n) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  rep2(i, n - 1) { cout << root[i] + 1 << endl; }
  return 0;
}