Problem p01341 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#define N 100005
#define P pair<int, int>
#define ll long long
#define mk(a, b) make_pair(a, b)
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
int inf = 0x3f3f3f3f;
int n, m, pre[N];
struct ac {
  int v;
  double c;
};
void init() {
  for (int i = 1; i < N; ++i) {
    pre[i] = i;
  }
}
int find(int x) {
  if (x == pre[x])
    return x;
  else
    return pre[x] = find(pre[x]);
}
void join(int x, int y) {
  int fx = find(x);
  int fy = find(y);
  if (fx == fy)
    return;
  else if (fx < fy)
    pre[fy] = fx;
  else
    pre[fx] = fy;
}
double sum;
bool vis[N];
double dis[N];
vector<P> a(N);
vector<ac> g[N];
void prime(int x) {
  mem(dis, 0);
  mem(vis, false);
  dis[x] = 1;
  for (int i = 1; i <= n; ++i) {
    double MAX = 0;
    int u = -1;
    for (int j = 1; j <= n; ++j) {
      if (vis[j])
        continue;
      if (dis[j] > MAX) {
        MAX = dis[j];
        u = j;
      }
    }
    if (u == -1)
      return;
    vis[u] = true;
    if (u == x)
      MAX = 0;
    sum += MAX;
    // cout << "u = " << u << " " << MAX << endl;
    for (int j = 0; j < g[u].size(); ++j) {
      ac t = g[u][j];
      if (vis[t.v])
        continue;
      if (dis[t.v] < t.c) {
        dis[t.v] = t.c;
      }
    }
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  while (cin >> n >> m) {
    init();
    for (int i = 1; i <= n; ++i) {
      cin >> a[i].first >> a[i].second;
    }
    double ans = 0;
    for (int i = 0; i < m; ++i) {
      int u, v;
      cin >> u >> v;
      int dx = a[u].first - a[v].first;
      int dy = a[u].second - a[v].second;
      double temp = sqrt(dx * dx + dy * dy);
      // cout << "temp = " << temp << endl;
      ans += temp;
      g[u].push_back((ac){v, temp});
      g[v].push_back((ac){u, temp});
      join(u, v);
    }
    // cout << ans << endl;
    for (int i = 1; i <= n; ++i) {
      if (pre[i] == i) {
        sum = 0;
        prime(i);
        ans -= sum;
        // cout << i << "::" << sum << endl;
      }
    }
    // ans += 1;
    // cout << ans << endl;
    printf("%.3lf\n", ans);
    for (int i = 1; i <= n; ++i) {
      g[i].clear();
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#define N 100005
#define P pair<int, int>
#define ll long long
#define mk(a, b) make_pair(a, b)
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
int inf = 0x3f3f3f3f;
int n, m, pre[N];
struct ac {
  int v;
  double c;
};
void init() {
  for (int i = 1; i < N; ++i) {
    pre[i] = i;
  }
}
int find(int x) {
  if (x == pre[x])
    return x;
  else
    return pre[x] = find(pre[x]);
}
void join(int x, int y) {
  int fx = find(x);
  int fy = find(y);
  if (fx == fy)
    return;
  else if (fx < fy)
    pre[fy] = fx;
  else
    pre[fx] = fy;
}
double sum;
bool vis[N];
double dis[N];
vector<P> a(N);
vector<ac> g[N];
void prime(int x) {
  mem(dis, 0);
  mem(vis, false);
  dis[x] = 1;
  for (int i = 1; i <= n; ++i) {
    double MAX = 0;
    int u = -1;
    for (int j = 1; j <= n; ++j) {
      if (vis[j])
        continue;
      if (dis[j] > MAX) {
        MAX = dis[j];
        u = j;
      }
    }
    if (u == -1)
      return;
    vis[u] = true;
    if (u == x)
      MAX = 0;
    sum += MAX;
    // cout << "u = " << u << " " << MAX << endl;
    for (int j = 0; j < g[u].size(); ++j) {
      ac t = g[u][j];
      if (vis[t.v])
        continue;
      if (dis[t.v] < t.c) {
        dis[t.v] = t.c;
      }
    }
  }
}

int main() {
  // #ifndef ONLINE_JUDGE
  //     freopen("in.txt", "r", stdin);
  // #endif
  ios::sync_with_stdio(false);
  while (cin >> n >> m) {
    init();
    for (int i = 1; i <= n; ++i) {
      cin >> a[i].first >> a[i].second;
    }
    double ans = 0;
    for (int i = 0; i < m; ++i) {
      int u, v;
      cin >> u >> v;
      int dx = a[u].first - a[v].first;
      int dy = a[u].second - a[v].second;
      double temp = sqrt(dx * dx + dy * dy);
      // cout << "temp = " << temp << endl;
      ans += temp;
      g[u].push_back((ac){v, temp});
      g[v].push_back((ac){u, temp});
      join(u, v);
    }
    // cout << ans << endl;
    for (int i = 1; i <= n; ++i) {
      if (pre[i] == i) {
        sum = 0;
        prime(i);
        ans -= sum;
        // cout << i << "::" << sum << endl;
      }
    }
    // ans += 1;
    // cout << ans << endl;
    printf("%.3lf\n", ans);
    for (int i = 1; i <= n; ++i) {
      g[i].clear();
    }
  }

  return 0;
}