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

#include <iostream>
#include <queue>
#include <set>
#include <vector>

#include <algorithm>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

/*


int ans[100010] = {0};
vector<pair<int, int>> path;
vector<pair<int, int>> tmp;

vector<int> func(vector<int> next) {

        vector<int> ret;
        for (int from : next) {

                for (auto p : path) {
                        if (p.first == from && ans[p.second] == 0)
                        {

                                ans[p.second] = p.first;
                                ret.push_back(p.second);
                        }
                        if (p.second == from && ans[p.first] == 0)
                        {
                                ans[p.first] = p.second;
                                ret.push_back(p.first);
                        }
                        else {
                                tmp.push_back(p);
                        }
                }
                swap(path, tmp);
                tmp.clear();
        }
        return ret;
}

bool check(int N) {

        for (int i = 2; i < N + 1; i++)
        {
                if (ans[i] == 0)
                        return false;
                return true;
        }
}

int main() {

        int N, M;
        vector<pair<int, int>> tmp;

        cin >> N >> M;

        rep(i, M) {
                int a, b;
                cin >> a >> b;
                path.push_back({ a, b });

        }


        vector<int> next = { 1 };
        while (!path.empty())
        {

                next = func(next);

                if (next.empty())break;

        }
        if (check(N)) {

                cout << "Yes" << endl;
                for (int i = 2; i < N + 1; i++)
                {
                        cout << ans[i] << endl;
                }

        }
        else
        {
                cout << "No";
        }


        return 0;
}

*/
/*
vector<int> to[10010];
int main() {
        int N, M;
        cin >> N >> M;

        rep(i, M) {
                int a, b;
                cin >> a >> b;
                to[a].push_back(b);
                to[b].push_back(a);
        }

        queue<int> q;
        q.push(1);

        vector<int> dist(N+1, -1);
        vector<int> pre(N+1, -1);

        dist[0] = 0;

        while (!q.empty())
        {
                int f = q.front(); q.pop();
                for (auto u : to[f])
                {
                        //cout << u;
                        if (dist[u] != -1)continue;
                        dist[u] = dist[f] + 1;
                        pre[u] = f;

                        q.push(u);
                }
        }

        rep(i, N) {
                if (i == 0)continue;
                cout << pre[i] << endl;
        }

        cin >> N;

        return 0;
}

*/

vector<int> path[100010];
int main() {
  int N, M;
  cin >> N >> M;

  rep(i, M) {
    int a, b;
    cin >> a >> b;

    path[a].push_back(b);
    path[b].push_back(a);
  }

  queue<int> from;
  vector<int> done(N + 1, 0);
  done[1] = 1;
  vector<int> ans(N + 1, 0);
  from.push(1);

  while (!from.empty()) {
    int f = from.front();
    from.pop();
    for (auto p : path[f]) {
      if (done[p] != 0)
        continue;
      done[p] = 1;
      ans[p] = f;
      from.push(p);
    }
  }

  cout << "Yes" << endl;
  rep(i, N + 1) {
    if (i == 0)
      continue;
    if (i == 1)
      continue;
    cout << ans[i] << endl;
  }
  // cin >> N;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <vector>

#include <algorithm>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

int ans[100010] = {0};
vector<pair<int, int>> path;
vector<pair<int, int>> tmp;

vector<int> func(vector<int> next) {

  vector<int> ret;
  for (int from : next) {

    for (auto p : path) {
      if (p.first == from && ans[p.second] == 0) {

        ans[p.second] = p.first;
        ret.push_back(p.second);
      }
      if (p.second == from && ans[p.first] == 0) {
        ans[p.first] = p.second;
        ret.push_back(p.first);
      } else {
        tmp.push_back(p);
      }
    }
    swap(path, tmp);
    tmp.clear();
  }
  return ret;
}

bool check(int N) {

  for (int i = 2; i < N + 1; i++) {
    if (ans[i] == 0)
      return false;
    return true;
  }
}

int main() {

  int N, M;
  vector<pair<int, int>> tmp;

  cin >> N >> M;

  rep(i, M) {
    int a, b;
    cin >> a >> b;
    path.push_back({a, b});
  }

  vector<int> next = {1};
  while (!path.empty()) {

    next = func(next);

    if (next.empty())
      break;
  }
  if (check(N)) {

    cout << "Yes" << endl;
    for (int i = 2; i < N + 1; i++) {
      cout << ans[i] << endl;
    }

  } else {
    cout << "No";
  }

  return 0;
}