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

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

class Edge {
public:
  int to, cost;
  Edge(int to, int cost) : to(to), cost(cost) {}
};

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

int prim(const Graph &g, int s = 0) {
  int n = g.size();
  int total = 0;

  vector<bool> visited(n);
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  // priority_queue<pair<int, int> > q;

  q.push(make_pair(0, s));

  vector<int> res;
  while (not q.empty()) {
    pair<int, int> u = q.top();
    q.pop();
    if (visited[u.second])
      continue;
    total += u.first;
    res.emplace_back(u.first);
    visited[u.second] = true;
    for (auto it : g[u.second]) {
      if (not visited[it.to])
        q.push(make_pair(it.cost, it.to));
    }
  }
  sort(all(res));
  // for(auto i : res){ show(i) }
  return res[res.size() / 2];
}

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    Graph g(n);
    rep(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      a--;
      b--;
      g[a].emplace_back(b, c);
      g[b].emplace_back(a, c);
    }
    cout << prim(g) << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

const int INF = 1e8;

int n;
int g[1005][1005];
bool use_edge[1005][1005];

bool bfs(int n, int edge, int mid) {
  bool used[1005] = {0};
  queue<int> q; //?¨??????????????????\\??????

  used[0] = 1;
  q.push(0);

  int u;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    bool f = false;
    rep(v, n) {
      if (use_edge[u][v] && used[v] == false) {
        used[v] = true;
        q.push(v);
        f = true;
      }
    }
    if (not f) {
      rep(i, n) {
        if (used[i] == false && g[u][i] <= mid && edge > 0) {
          used[i] = true;
          q.push(i);
          edge--;
          break;
        }
      }
    }
  }
  rep(i, n) {
    if (used[i] == false)
      return false;
  }
  return true;
}

int main() {
  int m;
  while (cin >> n >> m, n) {
    pair<int, pair<int, int>> p[10005];
    int ans = INF;
    rep(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      a--;
      b--;
      g[a][b] = g[b][a] = c;
      p[i] = make_pair(c, make_pair(a, b));
    }
    sort(p, p + m);

    memset(use_edge, 0, sizeof(use_edge));

    int h = m / 2;
    rep(i, h) {
      int a = p[i].second.first;
      int b = p[i].second.second;
      use_edge[a][b] = use_edge[b][a] = true;
      if (bfs(n, i, p[i].first)) {
        ans = min(ans, p[i].first);
      }
    }
    rep(i, m - h) {
      int a = p[m - i - 1].second.first;
      int b = p[m - i - 1].second.second;
      use_edge[a][b] = use_edge[b][a] = true;
      if (bfs(n, i, p[m - i - 1].first)) {
        ans = min(ans, p[m - i - 1].first);
      }
    }
    cout << ans << endl;
  }
}