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

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

typedef pair<int, int> P;

struct edge {
  int u;       //?????????
  int v;       //??°??????
  double cost; //??????
  //???????????????????????§?????§?°???¢????????????
  bool operator<(const edge &another) const { return cost > another.cost; }
};

const int MAX_N = 10002;
const int MAX_M = 500000;

P a[MAX_N];
int n, m;
edge es[MAX_M];
int par[MAX_N];
int rrank[MAX_N];

void initial(int n) { //?????????
  for (int i = 0; i < n; i++) {
    par[i] = i;
    rrank[i] = 0;
  }
}

int find(int x) { //??¨??????????±???????
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

void unite(int x, int y) { // x???????????¨??¨y???????????¨?????????
  x = find(x);             // x???????????¨??????????????????x??¨??????
  y = find(y);             // y???????????¨??????????????????y??¨??????
  if (x == y)
    return;
  if (rrank[x] < rrank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rrank[x] == rrank[y])
      rrank[x]++;
  }
}

bool same(int x, int y) { // x,y???????????¨???????????§?????????
  return find(x) == find(y);
}

double kruskal(int n, int m) {
  initial(n);
  double res = 0;
  for (int i = 0; i < m; i++) {
    edge e = es[i];
    if (!same(e.u, e.v)) {
      unite(e.u, e.v);
      res += e.cost;
    }
  }
  return res;
}

double dir(P p1, P p2) {
  return sqrt((p2.first - p1.first) * (p2.first - p1.first) +
              (p2.second - p1.second) * (p2.second - p1.second));
}

int main() {
  scanf("%d%d", &n, &m);
  rep(i, n) { scanf("%d%d", &a[i].first, &a[i].second); }
  rep(i, m) {
    int p, q;
    scanf("%d%d", &p, &q);
    es[i] = (edge){p - 1, q - 1, dir(a[p - 1], a[q - 1])};
  }
  double sm = 0.0;
  rep(i, m) { sm += es[i].cost; }
  sort(es, es + m);
  printf("%.3f\\n", sm - kruskal(n, m));
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

typedef pair<int, int> P;

struct edge {
  int u;       //?????????
  int v;       //??°??????
  double cost; //??????
  //???????????????????????§?????§?°???¢????????????
  bool operator<(const edge &another) const { return cost > another.cost; }
};

const int MAX_N = 10002;
const int MAX_M = 500000;

P a[MAX_N];
int n, m;
edge es[MAX_M];
int par[MAX_N];
int rrank[MAX_N];

void initial(int n) { //?????????
  for (int i = 0; i < n; i++) {
    par[i] = i;
    rrank[i] = 0;
  }
}

int find(int x) { //??¨??????????±???????
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

void unite(int x, int y) { // x???????????¨??¨y???????????¨?????????
  x = find(x);             // x???????????¨??????????????????x??¨??????
  y = find(y);             // y???????????¨??????????????????y??¨??????
  if (x == y)
    return;
  if (rrank[x] < rrank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rrank[x] == rrank[y])
      rrank[x]++;
  }
}

bool same(int x, int y) { // x,y???????????¨???????????§?????????
  return find(x) == find(y);
}

double kruskal(int n, int m) {
  initial(n);
  double res = 0;
  for (int i = 0; i < m; i++) {
    edge e = es[i];
    if (!same(e.u, e.v)) {
      unite(e.u, e.v);
      res += e.cost;
    }
  }
  return res;
}

double dir(P p1, P p2) {
  return sqrt((p2.first - p1.first) * (p2.first - p1.first) +
              (p2.second - p1.second) * (p2.second - p1.second));
}

int main() {
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0) {
      break;
    }
    rep(i, n) { scanf("%d%d", &a[i].first, &a[i].second); }
    rep(i, m) {
      int p, q;
      scanf("%d%d", &p, &q);
      es[i] = (edge){p - 1, q - 1, dir(a[p - 1], a[q - 1])};
    }
    double sm = 0.0;
    rep(i, m) { sm += es[i].cost; }
    sort(es, es + m);
    printf("%.3f\\n", sm - kruskal(n, m));
  }
  return 0;
}