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

#include <bits/stdc++.h>
#define M_PI_2 1.57079632679489661923
#define ll long long int
#define endl '\\n'
using namespace std;

vector<vector<int>> g;
vector<bool> visit;
vector<int> parent;
vector<int> dist;
template <typename T>

class graph {
  unordered_map<T, list<pair<T, int>>> m;

public:
  void addedge(T u, T v, int dist, bool bidir = true) {
    m[u].push_back(make_pair(v, dist));
    if (bidir) {
      m[v].push_back(make_pair(u, dist));
    }
  }
  void printAdj() {
    for (auto i : m) {
      cout << i.first << " -> ";
      for (auto j : i.second) {
        cout << "( " << j.first << "," << j.second << " )"
             << " ";
      }
      cout << endl;
    }
  }

  void dijkstra(T src) {
    unordered_map<T, int> dist;
    vector<int> par(m.size(), -1);
    for (auto j : m) {
      dist[j.first] = INT_MAX;
    }
    dist[src] = 0;
    set<pair<int, T>> s;
    s.insert({0, src});

    while (!s.empty()) {
      auto p = *(s.begin());
      T node = p.second;
      int nodedist = p.first;
      s.erase(s.begin());
      for (auto child : m[node]) {
        if ((nodedist + child.second) < dist[child.first]) {
          auto k = s.find({dist[child.first], child.second});
          if (k != s.end()) {
            s.erase(k);
          }
          s.insert({nodedist + child.second, child.first});
          dist[child.first] = nodedist + child.second;
          par[child.first] = node;
        }
      }
    }
    for (int i = 1; i < m.size(); i++) {
      if (par[i] == -1) {
        cout << "No" << endl;
        return;
      }
    }
    cout << "Yes" << endl;
    for (int i = 1; i < m.size(); i++) {
      cout << par[i] + 1 << endl;
    }
  }
};

void solve() {
  int n, k;
  cin >> n >> k;
  int x, y;
  graph<int> g;
  for (int i = 0; i < k; i++) {
    cin >> x >> y;
    x--;
    y--;
    g.addedge(x, y, 1);
  }

  g.dijkstra(0);
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define PI 3.14159265358979323846
#define endl '\\n'
using namespace std;

const int INF = 1000000000;
vector<vector<pair<int, int>>> adj;
vector<int> d, p;

void dijkstra(int s) {
  int n = adj.size();
  d.assign(n, INF);
  p.assign(n, -1);
  vector<bool> u(n, false);

  d[s] = 0;
  for (int i = 0; i < n; i++) {
    int v = -1;
    for (int j = 0; j < n; j++) {
      if (!u[j] && (v == -1 || d[j] < d[v]))
        v = j;
    }

    if (d[v] == INF)
      break;

    u[v] = true;
    for (auto edge : adj[v]) {
      int to = edge.first;
      int len = edge.second;

      if (d[v] + len < d[to]) {
        d[to] = d[v] + len;
        p[to] = v;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (!u[i]) {
      cout << "No" << endl;
      return;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i < n; i++) {
    cout << p[i] + 1 << endl;
  }
}

void solve() {
  int n, k;
  cin >> n >> k;
  adj = vector<vector<pair<int, int>>>(n);
  p = vector<int>(n);
  d = vector<int>(n);
  int x, y;
  for (int i = 0; i < k; i++) {
    cin >> x >> y;
    x--;
    y--;
    adj[x].push_back({y, 1});
    adj[y].push_back({x, 1});
  }
  dijkstra(0);
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}