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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const ll mod = 998244353;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    data_struct;

vector<ll> gr[100010];
ll dist[100010], par[100010];
bool vis[100010];

void dfs(ll u, ll p) {
  queue<pair<ll, ll>> q;
  q.push({u, p});
  vis[1] = true;
  while (!q.empty()) {
    pair<ll, ll> pr = q.front();
    q.pop();
    par[pr.first] = pr.second;
    for (auto it : gr[pr.first]) {
      if (!vis[it]) {
        vis[it] = true;
        q.push({it, pr.first});
      }
    }
  }
}

// void dijkstra( ll n, ll src)
// {
// 	dist[src] = 0;
// 	priority_queue < pair<ll,ll> , vector<pair<ll,ll>> ,
// greater<pair<ll,ll>> > pq; 	pq.push({0, src}); 	while(!pq.empty())
// 	{
// 		pair<ll,ll> p= pq.top();
// 		ll x=p.second;
// 		pq.pop();
// 		for(pair<ll,ll> y: gr[x])
// 		{
// 			ll wt=y.second;
// 			ll v=y.first;
// 			if(dist[v] > dist[x]+wt)
// 			{
// 				dist[v] = dist[x]+wt;
// 				par[v]=x;
// 				pq.push({dist[v],v});
// 			}
// 		}
// 	}
// }

int main() {
  ll n, m;
  cin >> n >> m;
  ll x, y;
  while (m--) {
    cin >> x >> y;
    gr[x].push_back(y);
    gr[y].push_back(x);
  }
  ll i;
  for (i = 1; i <= n; i++)
    dist[i] = -1;
  dist[1] = 0;
  dfs(1, -1);
  // dijkstra(n,1);
  for (i = 2; i <= n; i++)
    if (dist[i] == INT_MAX) {
      cout << "No";
      return 0;
    }
  // for(i=1;i<=n;i++)
  // 	cout<<dist[i]<<" ";
  // cout<<endl;
  cout << "Yes\\n";
  for (i = 1; i <= n; i++)
    for (i = 2; i <= n; i++)
      cout << par[i] << "\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const ll mod = 998244353;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    data_struct;

vector<ll> gr[100010];
ll dist[100010], par[100010];
bool vis[100010];

void dfs(ll u, ll p) {
  vis[u] = true;
  for (ll it : gr[u]) {
    if (vis[it]) {
      if (dist[it] > dist[u] + 1) {
        dist[it] = dist[u] + 1;
        par[it] = u;
        dfs(it, u);
      }
    } else {
      dist[it] = dist[u] + 1;
      par[it] = u;
      dfs(it, u);
    }
  }
}

int main() {
  ll n, m;
  cin >> n >> m;
  ll x, y;
  while (m--) {
    cin >> x >> y;
    gr[x].push_back(y);
    gr[y].push_back(x);
  }
  ll i;
  for (i = 1; i <= n; i++)
    dist[i] = -1;
  dist[1] = 0;
  dfs(1, -1);
  for (i = 2; i <= n; i++)
    if (dist[i] == -1) {
      cout << "No";
      return 0;
    }
  // for(i=1;i<=n;i++)
  // 	cout<<dist[i]<<" ";
  // cout<<endl;
  cout << "Yes\\n";
  for (i = 2; i <= n; i++)
    cout << par[i] << "\\n";
}