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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define eps (1e-10)
#define equals(a, b) (fabs((a) - (b)) < eps)
#define all(n) (n).begin(), (n).end()
#define inf (1 << 29)

using namespace std;
typedef long double ld;

struct P {
  ld left, right, weight;
  P(ld left = 0, ld right = 0, ld weight = inf)
      : left(left), right(right), weight(weight) {}
  bool operator<(const P &a) const {
    if (!equals(weight, a.weight))
      return weight < a.weight;
    if (!equals(left, a.left))
      return left < a.left;
    return right < a.right;
  }
};

ld r, ans;
int s;

bool check(ld v) {
  if (equals(v, r) || v > r)
    return false;
  return true;
}

P unit(P a, P b) {
  P node;
  node.weight = a.weight + b.weight;
  ld middle = b.weight / (a.weight + b.weight);
  node.left =
      min(-b.weight / (a.weight + b.weight) + a.left, b.left + (1 - middle));
  node.right =
      max(a.weight / (a.weight + b.weight) + b.right, a.right - middle);
  return node;
}

void dfs(vector<P> vec) {
  int n = vec.size();
  if (n <= 1) {
    ld len = vec[0].right - vec[0].left;
    assert(equals(len, 0) || len > 0);
    if (check(len))
      ans = max(ans, len);
    return;
  }

  rep(i, n) {
    rep(j, n) {
      if (i == j)
        continue;
      vector<P> nvec;
      rep(k, n) if (k == i || k == j) continue;
      else nvec.push_back(vec[k]);
      P p = unit(vec[i], vec[j]);
      nvec.push_back(p);
      dfs(nvec);
    }
  }
}

int main() {
  /*
  rep(i,100000)
    {
    P a,b;
      cout << "type node a\\n";
      cin >> a.left >> a.right >> a.weight;
      cout << "type node b\\n";
      cin >> b.left >> b.right >> b.weight;
      P c = unit(a,b);
      cout << "unit c (" << c.left << "," << c.right << "," << c.weight << ")"
  << endl;
    }
  */
  int T;
  while (cin >> T) {
    while (T--) {
      cin >> r;
      cin >> s;
      vector<P> ps(s);
      rep(i, s) cin >> ps[i].weight;

      ans = -inf;

      dfs(ps);

      if (equals(ans, -inf))
        cout << -1 << endl;
      else
        cout << setiosflags(ios::fixed) << setprecision(9) << ans << endl;
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <vector>

#define eps (1e-10)
#define equals(a, b) (fabs((a) - (b)) < eps)
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define inf (1 << 29)
#define all(n) (n).begin(), (n).end()

using namespace std;
typedef long double ld;

struct Node {
  ld left, right, lweight, rweight;
  bool leaf;
  Node(ld left = 0, ld right = 0, ld lweight = inf, ld rweight = inf)
      : left(left), right(right), lweight(lweight), rweight(rweight) {
    leaf = true;
  }
};

ld r, ans;
int s;

bool check(ld v) {
  if (equals(v, r) || v > r)
    return false;
  return true;
}

void dfs(vector<Node> vec) {
  int n = vec.size();
  if (n <= 1) {
    ld len = vec[0].right - vec[0].left;
    assert(len >= 0);
    if (!check(len))
      return;
    ans = max(ans, len);
    return;
  }
  /*
  cout << "vec ---- " << endl;
  rep(i,vec.size())cout << "(" << vec[i].left << "," << vec[i].right << "," <<
  vec[i].lweight << "," << vec[i].rweight << ")\\n"; cout << "^^^^^^^^^^^^^" <<
  endl;
  */
  rep(i, n) {
    rep(j, n) {
      if (i == j)
        continue;

      vector<Node> nvec;
      rep(k, n) if (k == i || k == j) continue;
      else nvec.push_back(vec[k]);

      if (vec[i].leaf && vec[j].leaf) {
        assert(vec[i].lweight == vec[i].rweight);
        assert(vec[j].lweight == vec[j].rweight);
        Node node = Node(0, 1, vec[i].lweight, vec[j].rweight);
        node.leaf = false;
        {
          vector<Node> nvec2 = nvec;
          nvec2.push_back(node);
          dfs(nvec2);
        }

        // ループ回してるから両方やらなくて良いだろ女子高生
        /*
        node.lweight = vec[j].lweight;
        node.rweight = vec[i].rweight;

        {
          vector<Node> nvec2 = nvec;
          nvec2.push_back(node);
          dfs(nvec2);
        }
        */
      } else if (!vec[i].leaf && !vec[j].leaf) {
        Node A = vec[i];
        Node B = vec[j];
        ld lenA = A.right - A.left;
        ld lenB = B.right - B.left;
        ld ratio = A.rweight / (A.rweight + A.lweight);
        ld x = lenA * ratio - 1;
        ratio = B.rweight / (B.rweight + B.lweight);
        ld lp = x - lenB * ratio;
        ratio = B.lweight / (B.rweight + B.lweight);
        ld rp = x + lenB * ratio;

        Node C = Node(min(lp, A.left), max(rp, A.right), B.rweight + B.lweight,
                      A.rweight + A.lweight);
        C.leaf = false;

        {
          vector<Node> nvec2 = nvec;
          nvec2.push_back(C);
          dfs(nvec2);
        }

      } else if (vec[i].leaf) {
        Node A = vec[i];
        Node B = vec[j];
        ld lenB = B.right - B.left;
        ld ratio = B.rweight / (B.rweight + B.lweight);
        ld x = lenB * ratio - 1;

        assert(A.rweight == A.lweight);
        Node C =
            Node(min(x, B.left), B.right, A.rweight, B.rweight + B.lweight);
        C.leaf = false;

        {
          vector<Node> nvec2 = nvec;
          nvec2.push_back(C);
          dfs(nvec2);
        }

      } else if (vec[j].leaf) {
        Node A = vec[i];
        Node B = vec[j];
        ld lenA = A.right - A.left;
        ld ratio = A.rweight / (A.rweight + A.lweight);
        ld x = lenA * ratio + 1;

        assert(B.rweight == B.lweight);
        Node C =
            Node(A.left, max(A.right, x), A.rweight + A.lweight, B.rweight);
        C.leaf = false;

        {
          vector<Node> nvec2 = nvec;
          nvec2.push_back(C);
          dfs(nvec2);
        }
      } else
        assert(false);
    }
  }
}

int main() {
  int T;
  while (cin >> T) {
    while (T--) {
      ans = -inf;
      cin >> r;
      cin >> s;
      vector<Node> vec(s);
      assert(s != 1);
      rep(i, s) cin >> vec[i].lweight, vec[i].rweight = vec[i].lweight;

      dfs(vec);

      if (equals(ans, -inf))
        cout << -1 << endl;
      else
        cout << setiosflags(ios::fixed) << setprecision(10) << ans << endl;
    }
  }
  return 0;
}