Cycling Roads

幾何

When Vova was in Shenzhen, he rented a bike and spent most of the time cycling around the city. Vova was approaching one of the city parks when he noticed the park plan hanging opposite the central entrance. The plan had several marble statues marked on it. One of such statues stood right there, by the park entrance. Vova wanted to ride in the park on the bike and take photos of all statues. The park territory has multiple bidirectional cycling roads. Each cycling road starts and ends at a marble statue and can be represented as a segment on the plane. If two cycling roads share a common point, then Vova can turn on this point from one road to the other. If the statue stands right on the road, it doesn’t interfere with the traffic in any way and can be photoed from the road.
Can Vova get to all statues in the park riding his bike along cycling roads only?
Input
The first line contains integers n and m that are the number of statues and cycling roads in the park (1 ≤ m < n ≤ 200) . Then n lines follow, each of them contains the coordinates of one statue on the park plan. The coordinates are integers, their absolute values don’t exceed 30 000. Any two statues have distinct coordinates. Each of the following m lines contains two distinct integers from 1 to n that are the numbers of the statues that have a cycling road between them.
Output
Print “YES” if Vova can get from the park entrance to all the park statues, moving along cycling roads only, and “NO” otherwise.
Example
input output

4 2
0 0
1 0
1 1
0 1
1 3
4 2



YES

4 3
0 0
1 0
1 1
0 1
1 2
2 1
3 4



NO

3 2
0 0
1 0
1 1
1 3
3 2

題目大意:給出n個點,m條線段,問所有的點是否可以相互到達。
給出的點有兩個變量,x,y。給出的線段,有兩個變量,每一條線段代表第一個點和第二個點之間有一條線段。

可以使用並查集+線段, 首先,給出線段之後,把在線段上的點都合併,然後遍歷看看這條線段是否與其他線段相交,如果相交,則合併其他點。如果最後只有一個祖先,則是YES,否則,NO。

#include <bits/stdc++.h>
using namespace std;
int pre[322];
int ans;
const double pi = acos(-1.0);
const double eps = 1e-8;
int Find(int a)
{
    if(a!=pre[a])
        return pre[a] = Find(pre[a]);
    return a;
}
void Merge(int a, int b)
{
    int aa = Find(a);
    int bb = Find(b);
    if(aa!=bb)
    {
        pre[aa] = bb;
        ans--;
    }
}
int cmp(double x)
{
    if(fabs(x)<eps)
        return 0;
    if(x>0)
        return 1;
    return -1;
}
struct point
{
    double x, y;
    int id;
    point(){}
    point(double a, double b)
    {
        x = a;
        y = b;
    }
    friend point operator + (const point &a, const point &b)
    {
        return point (a.x + b.x, a.y + b.y);
    }
    friend point operator - (const point &a, const point &b)
    {
        return point (a.x - b.x, a.y - b.y);
    }
}Point[345];
double dot(const point &a, const point &b)//點積
{
    return a.x*b.x+a.y*b.y;
}
double det(const point &a, const point &b)//叉積
{
    return a.x*b.y - a.y*b.x;
}
struct line
{
    point a, b;
    line(){};
    line(point x, point y):a(x), b(y){}
}Line[322];
line point_make_line(const point a, const point b)
{
    return line(a, b);
}
//bool PointOnSegment (point p, point s, point t)
//{
//    return cmp(det(p-s, t-s))==0&&cmp(dot(p-s, p-t))<=0;
//}
bool PointOnSegment (point p, line A)
{
    return cmp(det(A.a - p, A.b-p))==0&&cmp(dot(A.a-p, A.b-p))<=0;
}
//bool parallel(line a, line b)
//{
//    return !cmp(det(a.a-a.b, b.a-b.b));
//}
//bool line_make_point(line a, line b)
//{
//    if(parallel(a, b))
//        return false;
//    return true;
//}
bool line_make_point(line l1, line l2)
{
    if (PointOnSegment(l1.a, l2) || PointOnSegment(l1.b, l2) || PointOnSegment(l2.a, l1) || PointOnSegment(l2.b, l1))
    {
        return true;
    }
    double c1 = det(l1.a - l2.a, l1.a - l1.b);
    double c2 = det(l1.a - l2.b, l1.a - l1.b);
    double c3 = det(l2.a - l1.a, l2.a - l2.b);
    double c4 = det(l2.a - l1.b, l2.a - l2.b);
    return cmp(c1) * cmp(c2) < 0 && cmp(c3) * cmp(c4) < 0;
}
int main()
{
    int n, m;
    cin>>n>>m;
    ans = n;
    for(int i=1;i<=n;i++)
    {
        cin>>Point[i].x>>Point[i].y;
        Point[i].id = i;
    }
    for(int i=1;i<=n;i++)
    {
        pre[i] = i;
    }
    for(int i=1;i<=m;i++)
    {
        int a, b;
        cin>>a>>b;
        Line[i] = point_make_line(Point[a], Point[b]);
        for(int j=1;j<=n;j++)
        {
//            if(PointOnSegment(Point[j], Point[a], Point[b]))
//                {
//                    Merge(Point[j].id, Point[a].id);
//                    Merge(Point[j].id, Point[b].id);
//                }
                if(PointOnSegment(Point[j], Line[i]))
                {
                    Merge(Point[j].id, Line[i].a.id);
                    Merge(Point[j].id, Line[i].b.id);
                }
        }
        for(int j=1;j<i;j++)
        {
            if(line_make_point(Line[i], Line[j]))
            {
                Merge(Line[i].a.id, Line[j].a.id);
                Merge(Line[i].a.id, Line[j].b.id);
                Merge(Line[i].b.id, Line[j].a.id);
                Merge(Line[i].b.id, Line[j].b.id);
            }
        }
    }
    if(ans==1)
        cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;
    return 0;
}
發佈了378 篇原創文章 · 獲贊 55 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章