LA 3263 (平面图的欧拉定理) That Nice Euler Circuit

时间:2023-03-08 16:12:12

题意:

平面上有n个端点的一笔画,最后一个端点与第一个端点重合,即所给图案是闭合曲线。求这些线段将平面分成多少部分。

分析:

平面图中欧拉定理:设平面的顶点数、边数和面数分别为V、E和F。则 V+F-E=2

所求结果不容易直接求出,因此我们可以转换成 F=E-V+2

枚举两条边,如果有交点则顶点数+1,并将交点记录下来

所有交点去重(去重前记得排序),如果某个交点在线段上,则边数+1

 //#define LOCAL
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; const int maxn = + ; struct Point
{
double x, y;
Point(double x=, double y=) :x(x),y(y) {}
};
typedef Point Vector;
const double EPS = 1e-; Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); } Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); } Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); } Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); } bool operator < (const Point& a, const Point& b)
{ return a.x < b.x || (a.x == b.x && a.y < b.y); } int dcmp(double x)
{ if(fabs(x) < EPS) return ;
else return x < ? - : ; } bool operator == (const Point& a, const Point& b)
{ return dcmp(a.x-b.x) == && dcmp(a.y-b.y) == ; } double Dot(Vector A, Vector B)
{ return A.x*B.x + A.y*B.y; } double Length(Vector A) { return sqrt(Dot(A, A)); } double Angle(Vector A, Vector B)
{ return acos(Dot(A, B) / Length(A) / Length(B)); } double Cross(Vector A, Vector B)
{ return A.x*B.y - A.y*B.x; } double Area2(Point A, Point B, Point C)
{ return Cross(B-A, C-A); } Vector VRotate(Vector A, double rad)
{
return Vector(A.x*cos(rad) - A.y*sin(rad), A.x*sin(rad) + A.y*cos(rad));
} Point PRotate(Point A, Point B, double rad)
{
return A + VRotate(B-A, rad);
} Vector Normal(Vector A)
{
double l = Length(A);
return Vector(-A.y/l, A.x/l);
} Point GetLineIntersection(Point P, Vector v, Point Q, Vector w)
{
Vector u = P - Q;
double t = Cross(w, u) / Cross(v, w);
return P + v*t;
}
double DistanceToLine(Point P, Point A, Point B)
{
Vector v1 = B - A, v2 = P - A;
return fabs(Cross(v1, v2)) / Length(v1);
} double DistanceToSegment(Point P, Point A, Point B)
{
if(A == B) return Length(P - A);
Vector v1 = B - A, v2 = P - A, v3 = P - B;
if(dcmp(Dot(v1, v2)) < ) return Length(v2);
else if(dcmp(Dot(v1, v3)) > ) return Length(v3);
else return fabs(Cross(v1, v2)) / Length(v1);
} Point GetLineProjection(Point P, Point A, Point B)
{
Vector v = B - A;
return A + v * (Dot(v, P - A) / Dot(v, v));
} bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2)
{
double c1 = Cross(a2-a1, b1-a1), c2 = Cross(a2-a1, b2-a1);
double c3 = Cross(b2-b1, a1-b1), c4 = Cross(b2-b1, a2-b1);
return dcmp(c1)*dcmp(c2)< && dcmp(c3)*dcmp(c4)<;
} bool OnSegment(Point P, Point a1, Point a2)
{
Vector v1 = a1 - P, v2 = a2 - P;
return dcmp(Cross(v1, v2)) == && dcmp(Dot(v1, v2)) < ;
} Point P[maxn], V[maxn*maxn]; int main(void)
{
#ifdef LOCAL
freopen("3263in.txt", "r", stdin);
#endif int n, kase = ;
while(scanf("%d", &n) == && n)
{
for(int i = ; i < n; ++i)
{
scanf("%lf%lf", &P[i].x, &P[i].y);
V[i] = P[i];
}
n--;
int c = n, e = n; for(int i = ; i < n; ++i)
for(int j = i+; j < n; ++j)
if(SegmentProperIntersection(P[i], P[i+], P[j], P[j+]))
V[c++] = GetLineIntersection(P[i], P[i+]-P[i], P[j], P[j+]-P[j]); sort(V, V+c);
c = unique(V, V+c) - V; for(int i = ; i < c; ++i)
for(int j = ; j < n; ++j)
if(OnSegment(V[i], P[j], P[j+])) e++; printf("Case %d: There are %d pieces.\n", ++kase, e+-c);
} return ;
}

代码君