CodeForces-1017E The Supersonic Rocket(凸包+KMP)

E. The Supersonic Rocket

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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+(1−k)xb,kya+(1−k)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 (3≤n,m≤1053≤n,m≤105) — the number of power sources in each engine.

Each of the next nn lines contains two integers xixi and yiyi (0≤xi,yi≤1080≤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 (0≤xi,yi≤1080≤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

Copy

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.

传送门:CF-1017E

题意:题目讲了很长一段题意,其实就是问两个凸包经过旋转平移变换,是否能重合。也就是判断2个凸包是否相同。

题解:一开始写了个假算法(判断边和面积是否相等)居然PP了,赛后果不其然被卡掉。。

先构造两个凸包出来,首先判断凸包的顶点数是否相等,然后根据判断多边形相似的方法,接着判断对应角和对应边是否相同,因为是可以旋转的,所以用KMP匹配一下即可。

#include <bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define x first
#define y second
#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=a-1;i>=b;--i)
#define fuck(x) cout<<'['<<#x<<' '<<(x)<<']'
#define FIN freopen("in.txt","r",stdin);
#define eps 1e-8
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> PII;

const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fll;
const int mod = 1e9 + 7;
const int MX = 1e5 + 5;

struct Point {
    double x, y;
    Point() {}
    Point(double x, double y): x(x), y(y) {}
} P1[MX], P2[MX], R[MX];
typedef Point Vector;
int dcmp(double x) { //返回x的正负
    if(fabs(x) < eps)return 0;
    return x < 0 ? -1 : 1;
}
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);}
bool operator==(const Point&a, const Point&b) {return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;}
double Dot(Vector A, Vector B) { //点积
    return A.x * B.x + A.y * B.y; //如果改成整形记得加LL
}
double Cross(Vector A, Vector B) { //叉积
    return A.x * B.y - A.y * B.x; //如果改成整形记得加LL
}
//向量长度,为了不损失精度,不开根号
double Length(Vector A) {
    return Dot(A, A);
}
//2个向量之间的夹角,为了不损失精度,不用acos
//180度以内的cos是不会相等的
double Angle(Vector A, Vector B) {
    return Dot(A, B) / Length(A) / Length(B);
}
int Convex(Point *P, Point *R, int n) {
    int m = 0, k;
    sort(P, P + n);
    for(int i = 0; i < n; i++) {
        while(m > 1 && dcmp(Cross(R[m - 1] - R[m - 2], P[i] - R[m - 2])) <= 0) m--;
        R[m++] = P[i];
    }
    k = m;
    for(int i = n - 2; i >= 0; i--) {
        while(m > k && dcmp(Cross(R[m - 1] - R[m - 2], P[i] - R[m - 2])) <= 0) m--;
        R[m++] = P[i];
    }
    if(n > 1) m--;
    return m;
}
double a1[MX << 1], a2[MX], len1[MX << 1], len2[MX];
int Nxt[MX];
void makeNext(double P[], int m) {
    Nxt[0] = 0;
    for (int q = 1, k = 0; q < m; ++q) {
        while (k > 0 && dcmp(P[q] - P[k]) != 0) k = Nxt[k - 1];
        if (dcmp(P[q] - P[k]) == 0) k++;
        Nxt[q] = k;
    }
}
int kmp(double T[], double P[], int n, int m) {
    makeNext(P, m);
    for (int i = 0, q = 0; i < n; ++i) {
        while (q > 0 && dcmp(P[q] - T[i]) != 0) q = Nxt[q - 1];
        if (dcmp(P[q] - T[i]) == 0) q++;
        if (q == m) return 1;
    }
    return 0;
}
int main() {
    //FIN;
    int n, m, x, y; cin >> n >> m;
    rep(i, 0, n) scanf("%d%d", &x, &y), P1[i].x = x, P1[i].y = y;
    rep(i, 0, m) scanf("%d%d", &x, &y), P2[i].x = x, P2[i].y = y;
    n = Convex(P1, R, n);
    rep(i, 0, n) P1[i] = R[i];
    m = Convex(P2, R, m);
    rep(i, 0, m) P2[i] = R[i];
    if(n == m) {
        rep(i, 0, n) a1[i] = Angle(P1[(i - 1 + n) % n] - P1[i], P1[(i + 1) % n] - P1[i]);
        rep(i, 0, n) a2[i] = Angle(P2[(i - 1 + n) % n] - P2[i], P2[(i + 1) % n] - P2[i]);
        rep(i, n, 2 * n) a1[i] = a1[i - n];
        int flag = kmp(a1, a2, 2 * n, m);
        if(flag) {
            rep(i, 0, n) len1[i] = Length(P1[(i + 1) % n] - P1[i]);
            rep(i, 0, n) len2[i] = Length(P2[(i + 1) % n] - P2[i]);
            rep(i, n, 2 * n) len1[i] = len1[i - n];
            flag = kmp(len1, len2, 2 * n, m);
        }
        printf("%s\n", flag ? "YES" : "NO");
    } else puts("NO");
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值