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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
// #define INF (1LL<<59)

class UF {
private:
  vector<int> par, rank, elm;

public:
  int groups;

  UF(int __size)
      : par(__size), rank(__size, 0), elm(__size, 1), groups(__size) {
    for (int i = 0; i < __size; i++)
      par[i] = i;
  }

  int elements(int n) { return elm[find(n)]; }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    groups--;
    if (rank[x] < rank[y]) {
      par[x] = y;
      elm[y] += elm[x];
    } else {
      par[y] = x;
      elm[x] += elm[y];
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool isSame(int x, int y) { return find(x) == find(y); }
};

#define MAX_N 1000
struct edge {
  int to, cost;
};
vector<edge> G[MAX_N];
int kraskal(int n, vector<int> &list) {
  int ret = 0;
  vector<pair<int, pii>> edges;

  rep(i, MAX_N) {
    rep(j, G[i].size()) {
      edges.pb(make_pair(G[i][j].cost, make_pair(i, G[i][j].to)));
    }
  }

  sort(all(edges));

  UF uf(n);

  rep(i, edges.size()) {
    pii e = edges[i].second;

    if (!uf.isSame(e.first, e.second)) {
      uf.unite(e.first, e.second);
      ret += edges[i].first;
      list.pb(edges[i].first);
    }
  }
  return ret;
}

int main() {
  int n, m;
  while (cin >> n >> m && (n || m)) {
    rep(i, MAX_N) G[i].clear();
    rep(i, m) {
      int s, t, c;
      cin >> s >> t >> c;
      s--, t--;
      G[s].pb(edge{t, c});
      G[t].pb(edge{s, c});
    }
    vector<int> list;
    kraskal(n, list);
    sort(all(list));
    cout << list[list.size() / 2] << endl;
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

#define INF 999999;

int edge[1000][1000];
int n, m;

double prim(bool used[1000], int mincost[1000], vi res, int nextnode) {
  //    cout<<"node : "<<nextnode<<endl;
  double mini = INF;
  if (nextnode == -1) {
    sort(all(res));
    //        rep(i,res.size())cout<<res[i]<<" ";cout<<endl;
    if (res.size() % 2 == 1) {
      return min(mini,
                 (res[(res.size() - 1) / 2] + res[(res.size() + 2) / 2]) / 2.0);
    } else {
      return min(mini, (double)res[res.size() / 2]);
    }
  }
  used[nextnode] = true;
  res.pb(mincost[nextnode]);

  rep(u, n) {
    //        cout<<endl<<"-----------------"<<endl;
    //        cout<<mincost[u]<<endl;
    //        cout<<nextnode<<" "<<u<<" "<<edge[nextnode][u]<<endl;
    mincost[u] = min(mincost[u], edge[nextnode][u]);
  }

  int v = -1;
  rep(u, n) {
    if (used[u] == false && (v == -1 || mincost[u] < mincost[v])) {
      v = u;
    }
  }
  bool flag = false;
  rep(u, n) {
    if (mincost[v] == mincost[u]) {
      if (used[u] == false) {
        flag = true;
        used[u] = true;
        mini = min(mini, prim(used, mincost, res, u));
        used[u] = false;
      }
    }
  }
  if (flag == false)
    mini = min(mini, prim(used, mincost, res, -1));

  return mini;
}

int main() {
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;

    rep(i, 1000) rep(j, 1000) edge[i][j] = INF;

    rep(i, m) {
      int s, t, c;
      cin >> s >> t >> c;
      s--;
      t--;
      edge[s][t] = c;
      edge[t][s] = c;
    }

    bool used[1000];
    int mincost[1000];
    vi res;

    rep(i, n) {
      mincost[i] = INF;
      used[i] = false;
    }

    mincost[0] = 0;
    while (res.size())
      res.pop_back();

    cout << prim(used, mincost, res, 0) << endl;
  }

  return 0;
}