意甲冠军: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 + 10;
const double EPS = 1e-8; struct POINT
{
double x;
double y; POINT(){} POINT(double x, double y) : x(x), y(y){}
} p[MAXN]; int n, m;
int fa[MAXN], cnt[MAXN];
int u[MAXN], v[MAXN]; typedef POINT Vector;
Vector operator - (Vector A, Vector B)
{
return Vector(A.x - B.x, A.y - B.y);
} int Dcmp(double x)
{
if (fabs(x) < EPS) return 0;
return x > 0 ? 1 : -1;
} 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 SegmentProperIntersection(POINT a1, POINT a2, POINT b1, POINT b2)
{
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;
} bool OnSegment(POINT p, POINT a1, POINT a2)
{
return Dcmp(Cross(a1 - p, a2 - p)) == 0 && Dcmp(Dot(a1 - p, a2 - p)) < 0;
} void Init()
{
for (int i = 1; i <= n; ++i)
{
fa[i] = i;
cnt[i] = 1;
}
} int Find(int x)
{
return x == fa[x] ? x : (fa[x] = Find(fa[x]));
} void Union(int x, int y)
{
int xroot = Find(x);
int yroot = Find(y); if (xroot != yroot)
{
fa[yroot] = xroot;
cnt[xroot] += cnt[yroot];
}
} void Read()
{
for (int i = 1; i <= n; ++i)
{
scanf("%lf%lf", &p[i].x, &p[i].y);
}
for (int i = 0; i < m; ++i)
{
scanf("%d%d", u + i, v + i);
Union(u[i], v[i]);
for (int j = 1; j <= n; ++j)
{
if (j != u[i] && j != v[i] && OnSegment(p[j], p[u[i]], p[v[i]]))
{
Union(j, u[i]);
}
}
}
} void Merge()
{
for (int i = 0; i < m; ++i)
{
for (int j = i + 1; j < m; ++j)
{
if (SegmentProperIntersection(p[u[i]], p[v[i]], p[u[j]], p[v[j]]))
{
Union(u[i], u[j]);
}
}
}
} void Output()
{
cnt[Find(1)] == n ? puts("YES") : puts("NO");
} int main()
{
while (scanf("%d%d", &n, &m) == 2)
{
Init();
Read();
Merge();
Output();
} return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

URAL - 1966 - Cycling Roads(并检查集合 + 判刑线相交)的更多相关文章

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

    F - Cycling Roads     Description When Vova was in Shenzhen, he rented a bike and spent most of the ...

  2. Ural 1966 Cycling Roads

    ================ Cycling Roads ================   Description When Vova was in Shenzhen, he rented a ...

  3. URAL 1966 Cycling Roads 计算几何

    Cycling Roads 题目连接: http://acm.hust.edu.cn/vjudge/contest/123332#problem/F Description When Vova was ...

  4. HDU 1272 小希迷宫(并检查集合)

    意甲冠军:被判处无向图无环和连接无处不在 思考:并检查集合,trap 您可能有一个直接输入0 0 并且....合并的时候按某一个方向会爆栈,爆了好几次...下次考虑一下直接递归找祖先吧 #includ ...

  5. hdu1325 Is It A Tree?并检查集合

    pid=1325">职务地址 试想一下,在词和话题hdu1272是一样的. 可是hdu1272的博文中我也说了.数据比較水,所以我用非并查集的方法就AC了. 可是这题的数据没那么水,要 ...

  6. POJ 1984 Navigation Nightmare (数据结构-并检查集合)

    Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4072   Accepted: 1 ...

  7. CodeForces 277A Learning Languages (并检查集合)

    A. Learning Languages time limit per test:2 seconds memory limit per test:256 megabytes The "Be ...

  8. HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. zoj 3659 并检查集合

    http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=4882 现在在牡丹江,明天regional直播比赛,我会在一个月内退休.求祝福 ...

随机推荐

  1. JarSearch

    个人做的小工具分享给大家~~. 支持从压缩文件搜索文件,特别是根据部分类文件名在jar里查找文件,比较方便,效率也还不错. 也支持从目录查找 http://pan.baidu.com/s/1feYaM ...

  2. Java反射机制小例子

    package com.wjy.main; import java.io.Console; import java.lang.reflect.Constructor; import java.lang ...

  3. Django写的投票系统2(转)

    在上一篇中 django实例:创建你的第一个应用投票系统(一) 已经介绍基本的功能,并已经启动服务了.这一节介绍数据库相关的东东. 首页打开mysite/settings.py配置文件, 设置数据库打 ...

  4. extjs_11_mvc模式

    1.非mvc模式 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWRhbV93enM=/font/5a6L5L2T/fontsize/400/fill/I ...

  5. js获取设备信息

    var su = navigator.userAgent.toLowerCase(), mb = ['ipad', 'iphone os', 'midp', 'rv:1.2.3.4', 'ucweb' ...

  6. 中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030

    中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030 cp936是微软自己发布的用在文件系统中的编码方式.而bg2312是中国国家标准.我明白mount -t vfa ...

  7. 图解:Activity生命周期

    当用户需要对手机通过屏幕进行交互时,比如打一个电话,拍张照片,发送一个邮件,或者查看地图.开发者就需要实现一个活动(Activity).每个活动都将作为一个提供用户使用接口的窗口.它可以填满整个屏幕, ...

  8. MyEclipse中“擅自乱改”项目名导致项目报错的处理

    最近几天培训的过程中,经常有同学手一抖,默默的修改了本来配置部署好的项目名,导致项目报错…… 遇到这种事情,我一般会做的处理就是重新新建项目,然后把包和各种文件ctrl+c ctrl+v,遇到项目小还 ...

  9. Codeforces Round #198 (Div. 2) C. Tourist Problem (数学+dp)

    C. Tourist Problem time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  10. TCP/IP详细说明--滑模、拥塞窗口、慢启动、Negle算法

    TCP的数据流大致能够分为两类,交互数据流与成块的数据流. 交互数据流就是发送控制命令的数据流.比方relogin,telnet.ftp命令等等.成块数据流是用来发送数据的包,网络上大部分的TCP包都 ...