意甲冠军: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. 【POJ3159】Candies 裸的pqspfa模版题

    不多说了.就是裸的模版题. 贴代码: <span style="font-family:KaiTi_GB2312;font-size:18px;">#include & ...

  2. java 中间 final修饰符

    修饰符final:它是一个常数,我不同意改变 ,可以修改 变数,办法 ,分类 final修改变量:是final成常量,一旦赋值不能改变 常量能够在初始化时直接赋值.也能够在构造方法里赋值.仅仅能在这两 ...

  3. java的url 中国的争论导致了扭曲

    话不多说,,直接粘代码 发件人 UrlParaCode.jsp <%@ page language="java" import="java.util.*" ...

  4. 【译】ASP.NET MVC 5 教程 - 8:搜索查询

    原文:[译]ASP.NET MVC 5 教程 - 8:搜索查询 添加一个搜索的方法和搜索的视图 在本节中,我们为 Index 方法添加查询功能,使我们能够根据电影的题材或名称进行查找. 修改 Inde ...

  5. Monkey源代码分析之执行流程

    在<MonkeyRunner源代码分析之与Android设备通讯方式>中.我们谈及到MonkeyRunner控制目标android设备有多种方法.当中之中的一个就是在目标机器启动一个mon ...

  6. Matlab实现PCA

    在主成分分析(PCA)中,介绍了PCA的数学原理,其有用Matlab能够非常方便地对矩阵进行操作! 比方,用Matlab求多个样本的协方差矩阵.求矩阵的特征根和特征向量等. 以下介绍用Matlab实现 ...

  7. flex 错误信息类型及解决方法

    总结一些经常出现的异常信息及处理方法(会一直持续更新): 异常1: 写actionscript3.0时遇到了错误.报错为:Error #2044: 未处理的 ioError:. text=Error ...

  8. java中取得上下文路径的方法

    1.request.getContextPath(); 获得web根的上下文环境 如 /tree tree是web项目的root context 2.可以在servlet的init方法里 String ...

  9. css3仿山猫侧边栏

    演示:http://jsfiddle.net/Adce2/ 其主要思想: 1, 先画边栏html. 2, 使用css3分别财产close sidebar-content动图片. 3, 使用css3的k ...

  10. Python多线程的threading Event

    Python threading模块提供Event对象用于线程间通信.它提供了一组.拆除.等待用于线程间通信的其他方法. event它是沟通中最简单的一个过程之中,一个线程产生一个信号,号.Pytho ...