Problem p01341 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> pi;
typedef pair<double, pi> pdi;
#define MK make_pair
#define F first
#define S second
const int MX = 1e9 + 7;
vector<int> a[16000];
bool done[20000] = {};

double dis(pi c, pi b) {
  return (double)pow((c.F - b.F) * (c.F - b.F) + (c.S - b.S) * (c.S - b.S),
                     1.0 / 2);
}

int DFS(int s, int g) {
  done[s] = true;
  int count = 0;
  for (int i = 0; i < a[s].size(); i++) {
    if (!done[a[s][i]]) {
      if (a[s][i] == g) {
        count++;
        continue;
      }
      count += DFS(a[s][i], g);
      if (count >= 2) {
        break;
      }
    }
  }
  return count;
}

int main() {
  cout << fixed << setprecision(12);
  int n, m;
  vector<pi> point;

  priority_queue<pdi> q;
  point.push_back(MK(0, 0));
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int s, g;
    cin >> s >> g;
    point.push_back(MK(s, g));
  }
  for (int i = 0; i < m; i++) {
    int s, g;
    cin >> s >> g;
    a[s].push_back(g);
    a[g].push_back(s);
    q.push(MK(-1.0 * dis(point[s], point[g]), MK(s, g)));
  }
  double count = 0;

  while (!q.empty()) {
    pdi di = q.top();
    q.pop();
    int l = DFS(di.S.F, di.S.S);
    fill(&done[0], &done[15000], false);
    for (int i = 0; i < a[di.S.F].size(); i++) {
      if (a[di.S.F][i] == di.S.S) {
        a[di.S.F].erase(a[di.S.F].begin() + i);
      }
    }
    for (int i = 0; i < a[di.S.S].size(); i++) {
      if (a[di.S.S][i] == di.S.F) {
        a[di.S.S].erase(a[di.S.S].begin() + i);
      }
    }
    if (l < 2) {
      continue;
    }
    // cout<<count<<" + "<<di.F<<" "<<di.S.F<<" "<<di.S.S<<endl;
    count -= di.F;
  }

  cout << count << endl;

  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> pi;
typedef pair<double, pi> pdi;
#define MK make_pair
#define F first
#define S second
const int MX = 1e9 + 7;
vector<int> a[16000];
bool done[20000] = {};

double dis(pi c, pi b) {
  return (double)pow((c.F - b.F) * (c.F - b.F) + (c.S - b.S) * (c.S - b.S),
                     1.0 / 2);
}

int DFS(int s, int g) {
  done[s] = true;
  int count = 0;
  for (int i = 0; i < a[s].size(); i++) {
    if (!done[a[s][i]]) {
      if (a[s][i] == g) {
        count++;
        continue;
      }
      count += DFS(a[s][i], g);
      if (count >= 2) {
        break;
      }
    }
  }
  return count;
}

int main() {
  int n, m;
  vector<pi> point;

  priority_queue<pdi> q;
  point.push_back(MK(0, 0));
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int s, g;
    cin >> s >> g;
    point.push_back(MK(s, g));
  }
  for (int i = 0; i < m; i++) {
    int s, g;
    cin >> s >> g;
    a[s].push_back(g);
    a[g].push_back(s);
    q.push(MK(-1.0 * dis(point[s], point[g]), MK(s, g)));
  }
  double count = 0;

  while (!q.empty()) {
    pdi di = q.top();
    q.pop();
    int l = DFS(di.S.F, di.S.S);
    fill(&done[0], &done[15000], false);
    for (int i = 0; i < a[di.S.F].size(); i++) {
      if (a[di.S.F][i] == di.S.S) {
        a[di.S.F].erase(a[di.S.F].begin() + i);
      }
    }
    for (int i = 0; i < a[di.S.F].size(); i++) {
      if (a[di.S.S][i] == di.S.F) {
        a[di.S.S].erase(a[di.S.S].begin() + i);
      }
    }
    if (l < 2) {
      continue;
    }
    // cout<<count<<" + "<<di.F<<endl;
    count -= di.F;
  }

  cout << count << endl;

  return 0;
}