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

#include <algorithm>
#include <assert.h>
#include <complex>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;

typedef complex<double> Complex;
typedef Complex P;

static const double EPS = 1e-6;
inline bool EQ(double a, double b) { return abs(a - b) < EPS; }
inline bool EQP(Complex a, Complex b) { return norm(a - b) < EPS; }

struct Seg;
struct Line;
struct Circle;

double X(P p) { return p.real(); }
double Y(P p) { return p.imag(); }
double dot(P a, P b) { return X(conj(a) * b); }
double cross(P a, P b) { return Y(conj(a) * b); }
double ccw(P p1, P p2, P p3) { return cross(p2 - p1, p3 - p1); }
P proj(P p, P q) { return q * dot(p, q) / norm(q); }
P rot(P p) { return p * P(0, 1); }

struct Line {
  P p, v;
  double distance(P q) { return abs(cross(v, q - p)) / abs(v); }
  bool is_contain(P q) { return EQ(cross(v, q - p), 0.0); }
  bool is_parallel(Line rhs) { return EQ(cross(v, rhs.v), 0.0); }
  P intersection(Line rhs) {
    assert(!is_parallel(rhs));
    return p + v * cross(rhs.v, rhs.p - p) / cross(rhs.v, v);
  }
};

struct Triangle {
  P p1, p2, p3;
  Triangle(P p1, P p2, P p3) : p1(p1), p2(p2), p3(p3) { ; }
  P incenter() {
    double le1 = abs(p2 - p3), le2 = abs(p3 - p1), le3 = abs(p1 - p2);
    return (p1 * le1 + p2 * le2 + p3 * le3) / (le1 + le2 + le3);
  }

  vector<P> excenter() {
    vector<P> ret;
    double le1 = abs(p2 - p3), le2 = abs(p3 - p1), le3 = abs(p1 - p2);
    ret.push_back((-p1 * le1 + p2 * le2 + p3 * le3) / (-le1 + le2 + le3));
    ret.push_back((p1 * le1 - p2 * le2 + p3 * le3) / (le1 - le2 + le3));
    ret.push_back((p1 * le1 + p2 * le2 - p3 * le3) / (le1 + le2 - le3));
    return ret;
  }
};

Line line[300];

int main() {
  int n;
  while (scanf("%d", &n), n) {
    for (int i = 0; i < n; i++) {
      int x1, y1, x2, y2;
      scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
      line[i].p = Complex(x1, y1);
      line[i].v = Complex(x2 - x1, y2 - y1);
      line[i].v /= abs(line[i].v);
    }
    if (n < 3) {
      puts("Many");
      continue;
    }
    if (line[0].is_parallel(line[1]) && line[0].is_parallel(line[2])) {
      puts("None");
      continue;
    }
    vector<P> point;
    vector<double> dist;
    bool fail[4];
    memset(fail, false, sizeof(fail));
    if (line[0].is_parallel(line[1]) || line[0].is_parallel(line[2]) ||
        line[1].is_parallel(line[2])) {
      if (line[0].is_parallel(line[1])) {
      } else if (line[0].is_parallel(line[2])) {
        swap(line[1], line[2]);
      } else if (line[2].is_parallel(line[2])) {
        swap(line[0], line[2]);
      }
      P p1 = line[2].intersection(line[0]);
      P p2 = line[2].intersection(line[1]);
      P c = (p1 + p2) / 2.0;
      point.push_back(c + line[0].v * abs(p1 - c));
      point.push_back(c - line[0].v * abs(p1 - c));
      dist.push_back(line[0].distance(point[0]));
      dist.push_back(line[0].distance(point[1]));
    } else {
      P p1 = line[0].intersection(line[1]);
      P p2 = line[1].intersection(line[2]);
      P p3 = line[2].intersection(line[0]);
      if (EQP(p1, p2) && EQP(p1, p3)) {
        point.push_back(p1);
        dist.push_back(0.0);
      } else {
        Triangle tri(p1, p2, p3);
        point = tri.excenter();
        point.push_back(tri.incenter());
        dist.push_back(line[0].distance(point[0]));
        dist.push_back(line[0].distance(point[1]));
        dist.push_back(line[0].distance(point[2]));
        dist.push_back(line[0].distance(point[3]));
      }
    }
    int rest = (int)point.size();
    for (int i = 3; i < n; i++) {
      for (int j = 0; j < (int)point.size(); j++) {
        if (fail[j]) {
          continue;
        }
        double d = line[i].distance(point[j]);
        if (!EQ(d, dist[j])) {
          fail[j] = true;
          rest--;
        }
      }
    }
    P ans;
    for (int i = 0; i < (int)point.size(); i++) {
      if (!fail[i]) {
        ans = point[i];
      }
    }
    if (rest == 0) {
      puts("None");
    } else if (rest > 1) {
      puts("Many");
    } else {
      printf("%.4lf %.4lf\\n", ans.real(), ans.imag());
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <complex>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;

typedef complex<double> Complex;
typedef Complex P;

static const double EPS = 1e-6;
inline bool EQ(double a, double b) { return abs(a - b) < EPS; }
inline bool EQP(Complex a, Complex b) { return norm(a - b) < EPS; }

struct Seg;
struct Line;
struct Circle;

double X(P p) { return p.real(); }
double Y(P p) { return p.imag(); }
double dot(P a, P b) { return X(conj(a) * b); }
double cross(P a, P b) { return Y(conj(a) * b); }
double ccw(P p1, P p2, P p3) { return cross(p2 - p1, p3 - p1); }
P proj(P p, P q) { return q * dot(p, q) / norm(q); }
P rot(P p) { return p * P(0, 1); }

struct Line {
  P p, v;
  double distance(P q) { return abs(cross(v, q - p)) / abs(v); }
  bool is_contain(P q) { return EQ(cross(v, q - p), 0.0); }
  bool is_parallel(Line rhs) { return EQ(cross(v, rhs.v), 0.0); }
  P intersection(Line rhs) {
    assert(!is_parallel(rhs));
    return p + v * cross(rhs.v, rhs.p - p) / cross(rhs.v, v);
  }
};

struct Triangle {
  P p1, p2, p3;
  Triangle(P p1, P p2, P p3) : p1(p1), p2(p2), p3(p3) { ; }
  P incenter() {
    double le1 = abs(p2 - p3), le2 = abs(p3 - p1), le3 = abs(p1 - p2);
    return (p1 * le1 + p2 * le2 + p3 * le3) / (le1 + le2 + le3);
  }

  vector<P> excenter() {
    vector<P> ret;
    double le1 = abs(p2 - p3), le2 = abs(p3 - p1), le3 = abs(p1 - p2);
    ret.push_back((-p1 * le1 + p2 * le2 + p3 * le3) / (-le1 + le2 + le3));
    ret.push_back((p1 * le1 - p2 * le2 + p3 * le3) / (le1 - le2 + le3));
    ret.push_back((p1 * le1 + p2 * le2 - p3 * le3) / (le1 + le2 - le3));
    return ret;
  }
};

Line line[300];

int main() {
  int n;
  while (scanf("%d", &n), n) {
    if (n < 3) {
      puts("Many");
      continue;
    }
    for (int i = 0; i < n; i++) {
      int x1, y1, x2, y2;
      scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
      line[i].p = Complex(x1, y1);
      line[i].v = Complex(x2 - x1, y2 - y1);
      line[i].v /= abs(line[i].v);
    }
    if (line[0].is_parallel(line[1]) && line[0].is_parallel(line[2])) {
      puts("None");
      continue;
    }
    vector<P> point;
    vector<double> dist;
    bool fail[4];
    memset(fail, false, sizeof(fail));
    if (line[0].is_parallel(line[1]) || line[0].is_parallel(line[2]) ||
        line[1].is_parallel(line[2])) {
      if (line[0].is_parallel(line[1])) {
      } else if (line[0].is_parallel(line[2])) {
        swap(line[1], line[2]);
      } else if (line[2].is_parallel(line[2])) {
        swap(line[0], line[2]);
      }
      P p1 = line[2].intersection(line[0]);
      P p2 = line[2].intersection(line[1]);
      P c = (p1 + p2) / 2.0;
      point.push_back(c + line[0].v * abs(p1 - c));
      point.push_back(c - line[0].v * abs(p1 - c));
      dist.push_back(line[0].distance(point[0]));
      dist.push_back(line[0].distance(point[1]));
    } else {
      P p1 = line[0].intersection(line[1]);
      P p2 = line[1].intersection(line[2]);
      P p3 = line[2].intersection(line[0]);
      if (EQP(p1, p2) && EQP(p1, p3)) {
        point.push_back(p1);
        dist.push_back(0.0);
      } else {
        Triangle tri(p1, p2, p3);
        point = tri.excenter();
        point.push_back(tri.incenter());
        dist.push_back(line[0].distance(point[0]));
        dist.push_back(line[0].distance(point[1]));
        dist.push_back(line[0].distance(point[2]));
        dist.push_back(line[0].distance(point[3]));
      }
    }
    int rest = (int)point.size();
    for (int i = 3; i < n; i++) {
      for (int j = 0; j < (int)point.size(); j++) {
        if (fail[j]) {
          continue;
        }
        double d = line[i].distance(point[j]);
        if (!EQ(d, dist[j])) {
          fail[j] = true;
          rest--;
        }
      }
    }
    P ans;
    for (int i = 0; i < (int)point.size(); i++) {
      if (!fail[i]) {
        ans = point[i];
      }
    }
    if (rest == 0) {
      puts("None");
    } else if (rest > 1) {
      puts("Many");
    } else {
      printf("%.4lf %.4lf\\n", ans.real(), ans.imag());
    }
  }
}