UVALive 3263(欧拉公式,V-E+F==2,几何模版 is under-construction...)



B - That Nice Euler Circuit
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Appoint description: 

Description

Download as PDF

Little Joey invented a scrabble machine that he called Euler, after the great mathematician. In his primary school Joey heard about the nice story of how Euler started the study about graphs. The problem in that story was - let me remind you - to draw a graph on a paper without lifting your pen, and finally return to the original position. Euler proved that you could do this if and only if the (planar) graph you created has the following two properties: (1) The graph is connected; and (2) Every vertex in the graph has even degree.


Joey's Euler machine works exactly like this. The device consists of a pencil touching the paper, and a control center issuing a sequence of instructions. The paper can be viewed as the infinite two-dimensional plane; that means you do not need to worry about if the pencil will ever go off the boundary.

In the beginning, the Euler machine will issue an instruction of the form (X0, Y0) which moves the pencil to some starting position (X0,Y0). Each subsequent instruction is also of the form (X'Y'), which means to move the pencil from the previous position to the new position (X'Y'), thus draw a line segment on the paper. You can be sure that the new position is different from the previous position for each instruction. At last, the Euler machine will always issue an instruction that move the pencil back to the starting position (X0, Y0). In addition, the Euler machine will definitely not draw any lines that overlay other lines already drawn. However, the lines may intersect.

After all the instructions are issued, there will be a nice picture on Joey's paper. You see, since the pencil is never lifted from the paper, the picture can be viewed as an Euler circuit.

Your job is to count how many pieces (connected areas) are created on the paper by those lines drawn by Euler.

Input

There are no more than 25 test cases. Ease case starts with a line containing an integer N$ \ge$4, which is the number of instructions in the test case. The following N pairs of integers give the instructions and appear on a single line separated by single spaces. The first pair is the first instruction that gives the coordinates of the starting position. You may assume there are no more than 300 instructions in each test case, and all the integer coordinates are in the range (-300, 300). The input is terminated when N is 0.

Output

For each test case there will be one output line in the format


Case x: There are w pieces.,


where x is the serial number starting from 1.


Note: The figures below illustrate the two sample input cases.

\epsfbox{p3263.eps}

Sample Input

5
0 0 0 1 1 1 1 0 0 0 
7 
1 1 1 5 2 1 2 5 5 1 3 5 1 1 
0

Sample Output

Case 1: There are 2 pieces. 
Case 2: There are 5 pieces.

设G=<V,E>是连通平面图,若它有n个结点,m条边和r个面,则有n-m+r=2

用这个公式,只要求出n和m,r就是答案了

n个点直接求,注意染色判重

m条边怎么办?首先有n条边,是画出来的,但有可能线段相交使得边数增大。

这时候,枚举所有点,对于每个点,枚举所有线段,如果该点严格在该线段内,那么边数+=1。

我暂时不知道为什么,就是这样。



//Hello. I'm Peter.
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double ld;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define randin srand((unsigned int)time(NULL))
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define gsize(a) (int)a.size()
#define len(a) (int)strlen(a)
#define slen(s) (int)s.length()
#define pb(a) push_back(a)
#define clr(a) memset(a,0,sizeof(a))
#define clr_minus1(a) memset(a,-1,sizeof(a))
#define clr_INT(a) memset(a,INT,sizeof(a))
#define clr_true(a) memset(a,true,sizeof(a))
#define clr_false(a) memset(a,false,sizeof(a))
#define clr_queue(q) while(!q.empty()) q.pop()
#define clr_stack(s) while(!s.empty()) s.pop()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define dep(i, a, b) for (int i = a; i > b; i--)
#define repin(i, a, b) for (int i = a; i <= b; i++)
#define depin(i, a, b) for (int i = a; i >= b; i--)
#define pi 3.1415926535898
#define eps 1e-6
#define MOD 1000000007
#define MAXN
#define N 500
#define M
int dcmp(double x){
    if(fabs(x)<eps) return 0;
    else if(x<0) return -1;
    else return 1;
}
int n,nume,numv,numr;
struct Point
{
    double x,y;
    Point(){};
    Point(double xx,double yy):x(xx),y(yy){};
    friend bool operator<(const Point a,const Point b){
        if(dcmp(a.x-b.x)!=0) return a.x<b.x;
        else return a.y<b.y;
    }
}poi[N],vec[N];
typedef Point Vector;
map<Point,bool>vis;
vector<Point>newpoi;
void read(Point &b){
    scanf("%lf %lf",&b.x,&b.y);
}
Vector operator +(const Vector a,const Vector b){
    return Vector(a.x+b.x,a.y+b.y);
}
Vector operator -(const Vector a,const Vector b){
    return Vector(a.x-b.x,a.y-b.y);
}
Vector operator *(const double a,const Vector b){
    return Vector(a*b.x,a*b.y);
}
Vector operator *(const Vector b,const double a){
    return Vector(a*b.x,a*b.y);
}
double operator *(const Vector a,const Vector b){
    return a.x*b.x+a.y*b.y;
}
double operator &(const Vector a,const Vector b){
    return a.x*b.y-a.y*b.x;
}
Point LineIntersect(Point p,Vector v,Point q,Vector w){
    Vector u=p-q;
    double t=(w&u)/(v&w);
    return p+v*t;
}
bool SegmentStrictlyIntersect(Point p1,Vector v,Point q1,Vector w){
    Point p2=p1+v;
    Point q2=q1+w;
    Point p1p2=p2-p1,p1q1=q1-p1,p1q2=q2-p1;
    double c1=p1p2&p1q1,c2=p1p2&p1q2;
    Point q1q2=q2-q1,q1p1=p1-q1,q1p2=p2-q1;
    double c3=q1q2&q1p1,c4=q1q2&q1p2;
    return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}
bool PointStrictlyOnSegment(Point p,Point q1,Vector w){
    Point q2=q1+w;
    Point pq1=q1-p,pq2=q2-p;
    double c1=pq1&pq2,c2=pq1*pq2;
    return dcmp(c1)==0 && dcmp(c2)<0;
}
int main()
{
    int kase=1;
    while(~scanf("%d",&n) && n)
    {
        vis.clear();
        newpoi.clear();
        repin(i,1,n)
        {
            read(poi[i]);
            if(!vis[poi[i]])
            {
                vis[poi[i]]=true;
                newpoi.pb(poi[i]);
            }
        }
        n-=1;
        repin(i,1,n)
        {
            vec[i]=poi[i+1]-poi[i];
        }
        repin(i,1,n)
        {
            repin(j,i+1,n)
            {
                if(SegmentStrictlyIntersect(poi[i],vec[i],poi[j],vec[j]))
                {
                    Point p=LineIntersect(poi[i],vec[i],poi[j],vec[j]);
                    if(!vis[p])
                    {
                        vis[p]=true;
                        newpoi.pb(p);
                    }
                }
            }
        }
        int len_newpoi=gsize(newpoi);
        numv=len_newpoi;
        nume=n;
        //求边数
        rep(i,0,len_newpoi)
        {
            Point p=newpoi[i];
            repin(j,1,n)
            {
                if(PointStrictlyOnSegment(p,poi[j],vec[j]))
                {
                    nume+=1;
                }
            }
        }
        //Euler told us: numv-nume+numr==2;
        //So numr=nume-numv+2;
        numr=nume-numv+2;
        printf("Case %d: There are %d pieces.\n",kase++,numr);
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值