CodeForces - 1017E :The Supersonic Rocket (几何+KMP,判定凸包是否同构)

After the war, the supersonic rocket became the most common public transportation.

Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has nn power sources, and the second one has mm power sources. A power source can be described as a point (xi,yi)(xi,yi) on a 2-D plane. All points in each engine are different.

You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want.

  1. For every power source as a whole in that engine: (xi,yi)(xi,yi) becomes (xi+a,yi+b)(xi+a,yi+b), aa and bb can be any real numbers. In other words, all power sources will be shifted.
  2. For every power source as a whole in that engine: (xi,yi)(xi,yi) becomes (xicosθyisinθ,xisinθ+yicosθ)(xicos⁡θ−yisin⁡θ,xisin⁡θ+yicos⁡θ), θθ can be any real number. In other words, all power sources will be rotated.

The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(xa,ya)A(xa,ya) and B(xb,yb)B(xb,yb) exist, then for all real number kk that 0<k<10<k<1, a new power source will be created Ck(kxa+(1k)xb,kya+(1k)yb)Ck(kxa+(1−k)xb,kya+(1−k)yb). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred).

A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well.

Given a supersonic rocket, check whether it is safe or not.

Input

The first line contains two integers nn, mm (3n,m1053≤n,m≤105) — the number of power sources in each engine.

Each of the next nn lines contains two integers xixi and yiyi (0xi,yi1080≤xi,yi≤108) — the coordinates of the ii-th power source in the first engine.

Each of the next mm lines contains two integers xixi and yiyi (0xi,yi1080≤xi,yi≤108) — the coordinates of the ii-th power source in the second engine.

It is guaranteed that there are no two or more power sources that are located in the same point in each engine.

Output

Print "YES" if the supersonic rocket is safe, otherwise "NO".

You can print each letter in an arbitrary case (upper or lower).

Examples

Input
3 4
0 0
0 2
2 0
0 2
2 2
2 0
1 1
Output
YES
Input
3 4
0 0
0 2
2 0
0 2
2 2
2 0
0 0
Output
NO

Note

The first sample:

  Those near pairs of blue and orange points actually coincide.

First, manipulate the first engine: use the second operation with θ=πθ=π (to rotate all power sources 180180 degrees).

The power sources in the first engine become (0,0)(0,0), (0,2)(0,−2), and (2,0)(−2,0).

Second, manipulate the second engine: use the first operation with a=b=2a=b=−2.

The power sources in the second engine become (2,0)(−2,0), (0,0)(0,0), (0,2)(0,−2), and (1,1)(−1,−1).

You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0,0)(0,0), (2,0)(−2,0), (0,2)(0,−2).

In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it.

题意:题面很长,但其实就是给定两个多边形,问其凸包是否旋转同构。

思路:先得到两个多边形的凸包,然后把多边形看成字符串,而判定字符串S1和S2旋转同构的方法是,将S1加倍,去匹配S2,KMP即可搞定。

现在把多边形像字符串一样保存,角-边-角-边...,而角可以用点乘表示,因为角两旁的边相等,而点乘也相同,说明角度相同。而叉积不具有这样的性质,平行四边形的相邻角就是反例。

(hash也可以判定旋转同构

#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
typedef long long ll;
const double inf=1e200;
const double eps=1e-12;
const double pi=4*atan(1.0);
struct point{
    double x,y;
    point(double a=0,double b=0):x(a),y(b){}
};
int dcmp(double x){ return fabs(x)<eps?0:(x<0?-1:1);}
point operator +(point A,point B) { return point(A.x+B.x,A.y+B.y);}
point operator -(point A,point B) { return point(A.x-B.x,A.y-B.y);}
point operator *(point A,double p){ return point(A.x*p,A.y*p);}
point operator /(point A,double p){ return point(A.x/p,A.y/p);}
point rotate(point A,double rad){
    return point(A.x*cos(rad)-A.y*sin(rad), A.x*sin(rad)+A.y*cos(rad));
}
bool operator ==(const point& a,const point& b) { 
     return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
double dot(point A,point B){ return A.x*B.x+A.y*B.y;}
double det(point A,point B){ return A.x*B.y-A.y*B.x;}
double dot(point O,point A,point B){ return dot(A-O,B-O);}
double det(point O,point A,point B){ return det(A-O,B-O);}
/*double length(point A){ return sqrt(dot(A,A));}
double angle(point A,point B){ return acos(dot(A,B)/length(A)/length(B));}
double distoline(point P,point A,point B)
{ 
    //点到直线距离 
    point v1=B-A,v2=P-A;
    return fabs(det(v1,v2)/length(v1));
}
double distoseg(point P,point A,point B)
{
    //点到线段距离 
    if(A==B) return length(P-A);
    point v1=B-A,v2=P-A,v3=P-B;
    if(dcmp(dot(v1,v2))<0) return length(v2);
    else if(dcmp(dot(v1,v3))>0) return length(v3);
    return fabs(det(v1,v2)/length(v1));
}
double Ployarea(vector<point>p)
{
    //多边形面积 
    double ans=0; int sz=p.size();
    for(int i=1;i<sz-1;i++) ans+=det(p[i]-p[0],p[i+1]-p[0]);
    return ans/2.0;
}
bool SegmentProperIntersection(point a1,point a2,point b1,point b2)   {
    //规范相交 
    double c1=det(a2-a1,b1-a1),c2=det(a2-a1,b2-a1);
    double c3=det(b2-b1,a1-b1),c4=det(b2-b1,a2-b1);
    return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;    
}
bool isPointOnSegment(point p,point a1,point a2)
{
    //点是否在线段上 
    return dcmp(det(a1-p,a2-p)==0&&dcmp(dot(a1-p,a2-p))<0);
}
int isPointInPolygon(point p,vector<point>poly)
{
    //判断点与多边形的位置关系 
    int wn=0,sz=poly.size();
    for(int i=0;i<sz;i++){
        //在边上
        if(isPointOnSegment(p,poly[i],poly[(i+1)%sz])) return -1;
        int k=dcmp(det(poly[(i+1)%sz]-poly[i],p-poly[i]));     
        int d1=dcmp(poly[i].y-p.y); int d2=dcmp(poly[(i+1)%sz].y-p.y);
        if(k>0&&d1<=0&&d2>0) wn++;
        if(k<0&&d2<=0&&d1>0) wn--;
    }
    if(wn!=0) return 1;//内部
    return 0;          //外部
}
double seg(point O,point A,point B){
    if(dcmp(B.x-A.x)==0) return (O.y-A.y)/(B.y-A.y);
    return (O.x-A.x)/(B.x-A.x);
}
pair<double,int>s[110*60]; 
double polyunion(vector<point>*p,int N){ //有多个才加*,单个不加,有改变的加& 
    //求多边形面积并
    double res=0;
    for(int i=0;i<N;i++){
        int sz=p[i].size();
        for(int j=0;j<sz;j++){
            int m=0;
            s[++m]=mp(0,0);
            s[++m]=mp(1,0);
            point a=p[i][j],b=p[i][(j+1)%sz];
            for(int k=0;k<N;k++){
                if(i!=k){
                    int sz2=p[k].size();
                    for(int ii=0;ii<sz2;ii++){
                        point c=p[k][ii],d=p[k][(ii+1)%sz2];
                        int c1=dcmp(det(b-a,c-a));
                        int c2=dcmp(det(b-a,d-a));
                        if(c1==0&&c2==0){
                            if(dcmp(dot(b-a,d-c))){
                                s[++m]=mp(seg(c,a,b),1);
                                s[++m]=mp(seg(c,a,b),-1);
                            }
                        }
                        else{
                            double s1=det(d-c,a-c);
                            double s2=det(d-c,b-c);
                            if(c1>=0&&c2<0) s[++m]=mp(s1/(s1-s2),1);
                            else if(c1<0&&c2>=0) s[++m]=mp(s1/(s1-s2),-1);
                        }
                    }
                }    
            }
            sort(s+1,s+m+1);
            double pre=min(max(s[1].first,0.0),1.0),now,sum=0;
            int cov=s[0].second;
            for(int j=2;j<=m;j++){
                now=min(max(s[j].first,0.0),1.0);
                if(!cov) sum+=now-pre;
                cov+=s[j].second;
                pre=now;
            }
            res+=det(a,b)*sum;
        }
    }
    return -(res/2); 
}
point jiaopoint(point p,point v,point q,point w)
{   //p+tv q+tw,点加向量表示直线,求直线交点 
    point u=p-q;
    double t=det(w,u)/det(v,w);
    return p+v*t;
}
point GetCirPoint(point a,point b,point c)
{
    point p=(a+b)/2;    //ad中点 
    point q=(a+c)/2;    //ac中点 
    point v=rotate(b-a,pi/2.0),w=rotate(c-a,pi/2.0);   //中垂线的方向向量 
    if (dcmp(length(det(v,w)))==0)    //平行 
    {
        if(dcmp(length(a-b)+length(b-c)-length(a-c))==0) return (a+c)/2;
        if(dcmp(length(b-a)+length(a-c)-length(b-c))==0) return (b+c)/2;
        if(dcmp(length(a-c)+length(c-b)-length(a-b))==0) return (a+b)/2;
    }
    return jiaopoint(p,v,q,w);
}
void MinCircular()
{
    //最小圆覆盖 ,看起来是O(N^3),期望复杂度为O(N) 
    point P[10000]; int n;
    random_shuffle(P+1,P+n+1);    //随机化
    point c=P[1]; double r=0;   //c 圆心,,//r 半径
    for (int i=2;i<=n;i++)
        if (dcmp(length(c-P[i])-r)>0)    //不在圆内
        {
            c=P[i],r=0;
            for (int j=1;j<i;j++)
                if (dcmp(length(c-P[j])-r)>0)
                {
                    c=(P[i]+P[j])/2.0;
                    r=length(c-P[i]);
                    for (int k=1;k<j;k++)
                        if (dcmp(length(c-P[k])-r)>0)
                        {
                            c=GetCirPoint(P[i],P[j],P[k]);
                            r=length(c-P[i]);
                        }
                }
        } 
}*/
const int maxn=400010;
bool cmp(point a,point b){ return a.x==b.x?a.y<b.y:a.x<b.x; } 
point f[maxn],c[maxn],ch[maxn][2]; int top1,top2,L1,L2;
void convexhull(point *a,int n,int &top,int opt)
{
    //水平序的Andrew算法求凸包。
    sort(a+1,a+n+1,cmp); top=0;
    for(int i=1;i<=n;i++){ //求下凸包 
        while(top>=2&&det(ch[top-1][opt],ch[top][opt],a[i])<=0) top--;
        ch[++top][opt]=a[i];
    }
    int ttop=top;
    for(int i=n-1;i>=1;i--){ //求上凸包 
        while(top>ttop&&det(ch[top-1][opt],ch[top][opt],a[i])<=0) top--;
        ch[++top][opt]=a[i]; 
    }
}
double a[maxn],b[maxn]; int Next[maxn];
bool KMP()
{
    Next[1]=0;
    for(int i=2,k=0;i<=L2;i++){
        while(k&&b[i]!=b[k+1]) k=Next[k];
        if(b[i]==b[k+1]) k++; Next[i]=k;
       }
       for(int i=1,k=0;i<=L1+L1;i++){
           while(k&&a[i]!=b[k+1]) k=Next[k];
           if(a[i]==b[k+1]) k++;
           if(k==L2) return true;
    }
    return false;
}
int main()
{
    int N,M,i;
    scanf("%d%d",&N,&M);
    for(i=1;i<=N;i++) scanf("%lf%lf",&f[i].x,&f[i].y);
    for(i=1;i<=M;i++) scanf("%lf%lf",&c[i].x,&c[i].y);
    convexhull(f,N,top1,0);
    convexhull(c,M,top2,1);
    if(top1!=top2) return puts("NO"),0;
    for(i=1;i<top1;i++){
        a[++L1]=dot(ch[i][0],ch[i-1==0?top1-1:i-1][0],ch[i+1][0]);
        a[++L1]=(ch[i+1][0].x-ch[i][0].x)*(ch[i+1][0].x-ch[i][0].x)+(ch[i+1][0].y-ch[i][0].y)*(ch[i+1][0].y-ch[i][0].y);
    }
    for(i=1;i<top2;i++){
        b[++L2]=dot(ch[i][1],ch[i-1==0?top2-1:i-1][1],ch[i+1][1]);
        b[++L2]=(ch[i+1][1].x-ch[i][1].x)*(ch[i+1][1].x-ch[i][1].x)+(ch[i+1][1].y-ch[i][1].y)*(ch[i+1][1].y-ch[i][1].y);
    }
    for(i=1;i<=L1;i++) a[i+L1]=a[i];
    if(KMP()) puts("YES");
    else puts("NO");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/hua-dong/p/9487291.html