Ural 1966 Cycling Roads

================

Cycling Roads

================

Description

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.

Sample Input

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
YES

这道题主要是判相交,只要相交就把它压入并查集,一开始我是用了cnt去记录已经相交的节点,后来发现不行,因为新加如的一条线如果加进去了,它的另外一个端点也会加入,导致cnt记录的数值不准。于是用了另外一个数组c[i]去记录以i为根的所有子节点的个数。

在判断相交这里,一开始没有注意到新加入一条线段时,应该判断所有点是否在该线段上,如果端点在该线段上,则把它加入,加了OnSegment()判断之后就AC了。

#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
#define maxn 205
struct point
{
    double x,y;
    point(double x = 0,double y = 0):x(x),y(y){}
}p[maxn];

struct Line
{
    point a,b;
    int pos1,pos2;
    Line(){}
    Line(point x,point y,int ppos1,int ppos2){  a = x; b = y; pos1 = ppos1; pos2 = ppos2;}
}line[maxn];

int n,m,cnt;
int par[maxn];
int c[maxn];

typedef point Vector;
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);  }
const double eps = 1e-10;
int dcmp(double x)
{
    if(fabs(x) < eps) return 0;
    else return x < 0? -1:1;
}
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;  }
double cross(Vector A,Vector B){ return A.x*B.y-A.y*B.x;  }

bool OnSegment(point p,Line l)
{
    return dcmp(cross(l.a-p,l.b-p)) == 0 && dcmp(dot(l.a-p,l.b-p)) < 0;
}
bool SegmentProperIntersection(Line l1,Line l2)
{
    point a1 = l1.a;
    point a2 = l1.b;
    point b1 = l2.a;
    point b2 = l2.b;
    double c1 = cross(a2-a1,b1-a1);
    double c2 = cross(a2-a1,b2-a1);
    double c3 = cross(b2-b1,a1-b1);
    double c4 = cross(b2-b1,a2-b1);
    return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0;
}

void init()
{
    for(int i = 1; i <= n; i++)
        c[i] = 1;
    for(int i = 0; i < maxn;i++)
        par[i] = i;
}
int Find(int x)
{
    if(par[x] != x)
    {
        return par[x]=Find(par[x]);
    }
    else return x;
}

void Merge(int a,int b)
{
    int t1 = Find(a);
    int t2 = Find(b);
    if(t1 != t2)
    {
        par[t2] = t1;
        c[t1] += c[t2];
        //printf("%d %d   merge\n",a,b);
        //return 1;
    }
    //return 0;
}

void input()
{
    int x,y;

    for(int i = 1; i <= n; i++)
    {
        double x,y;
        scanf("%lf%lf",&x,&y);
        p[i] = point(x,y);
    }
    for(int i = 0; i < m; i++)
    {
        scanf("%d%d",&x,&y);
        line[i] = Line(p[x],p[y],x,y);
        for(int j = 1; j <= n; j++)
        {
            if(OnSegment(p[j],line[i])) Merge(j,x);
        }
        Merge(x,y);
    }
}

void deal()
{
    for(int i = 0; i < m; i++)
    {
        for(int j = i + 1; j < m; j++)
        {
            if(SegmentProperIntersection(line[i],line[j]))
            {
                Merge(line[j].pos1,line[i].pos1);
                //Merge(line[j].pos2,line[i].pos1);
            }
        }
    }

}

int main()
{
    //freopen("input.txt","r",stdin);
    while(scanf("%d%d",&n,&m) == 2)
    {
        init();
        input();
        deal();
        if(c[Find(1)] == n) printf("YES\n");
        else printf("NO\n");
    }

    return 0;
}

时间: 2025-01-31 00:41:06

Ural 1966 Cycling Roads的相关文章

URAL 1966 Cycling Roads 点在线段上、线段是否相交、并查集

F - Cycling Roads Description 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

URAL - 1966 - Cycling Roads(并检查集合 + 判刑线相交)

意甲冠军:n 积分,m 边缘(1 ≤ m < n ≤ 200),问:是否所有的点连接(两个边相交.该 4 点连接). 主题链接:http://acm.timus.ru/problem.aspx?space=1&num=1966 -->>对于每条边,边上的两端点并入集合,枚举边与边.推断他们是否相交,是的话各点并入集合,最后看集合内元素的个数是否为n.. #include <cstdio> #include <cmath> const int MAXN =

URAL - 1966 - Cycling Roads(并查集 + 判线段相交)

题意:n 个点,m 条边(1 ≤ m < n ≤ 200),问所有点是否连通(两条边相交,则该 4 点连通). 题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1966 -->>对于每条边,边上的两端点并入集合,枚举边与边,判断他们是否相交,是的话各点并入集合,最后看集合内元素的个数是否为n.. #include <cstdio> #include <cmath> const int MAXN = 200

BNUOJ33566 Cycling Roads(并查集+判断两线段相交)

Cycling Roads Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on Ural. Original ID: 1966 64-bit integer IO format: %lld      Java class name: (Any) Prev Submit Status Statistics Discuss Next Font Size:  +   - Type:   None Graph T

URAL比赛记录

URAL 1961 考虑到此人也在中国,N要加一. #include<cstdio> #include<iostream> using namespace std; int main() { double n,m,N; int ans; cin>>n>>m>>N; ans=(N+1.0)*m/n; if(ans>N) ans=N; cout<<ans<<endl; return 0; } URAL 1962 显然如果

URAL 1930 Ivan&#39;s Car(BFS)

Ivan's Car Time limit: 1.5 secondMemory limit: 64 MB The world is in danger! Awful earthquakes are detected all over the world. Houses are destroyed, rivers overflow the banks, it is almost impossible to move from one city to another. Some roads are

Ural 1004 Sightseeing Trip

Sightseeing Trip Time Limit: 2000ms Memory Limit: 16384KB This problem will be judged on Ural. Original ID: 100464-bit integer IO format: %lld      Java class name: (Any) There is a travel agency in Adelton town on Zanzibar island. It has decided to

Ural 1081 Binary Lexicographic Sequence(DP)

题目地址:Ural 1081 先用dp求出每个长度下的合法序列(开头为1)的个数.然后求前缀和.会发现正好是一个斐波那契数列.然后每次判断是否大于此时长度下的最少个数,若大于,说明这一位肯定是1,若小于,则肯定是0.就这样不断输出出来即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #in

POJ 2421 Constructing Roads 修建道路 最小生成树 Kruskal算法

题目链接:POJ 2421 Constructing Roads 修建道路 Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19698   Accepted: 8221 Description There are N villages, which are numbered from 1 to N, and you should build some roads such that e