意甲冠军: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. Oracle数据库案例整理-Oracle系统执行时故障-Shared Pool内存不足导致数据库响应缓慢

    1.1       现象描写叙述 数据库节点响应缓慢,部分用户业务受到影响. 查看数据库告警日志,開始显示ORA-07445错误,然后是大量的ORA-04031错误和ORA-00600错误. 检查数据 ...

  2. BGP拓扑错误模拟配置

    R1配置 --------------------------------------------- version 5.20, ESS 2207P45# sysname RT1# super pas ...

  3. centos7图形配置 firewall-config

    图形配置 firewall-config centos 7:systemctl stop firewalld.service #停止

  4. Java的JXL操作xls形式

    jxl这是一个韩国的写作java操作excel工具, 源世界中,有两套比較有影响的API可供使用.一个是POI,一个是jExcelAPI.当中功能相对POI比較弱一点.但jExcelAPI对中文支持非 ...

  5. Knockout应用开发指南 第三章:绑定语法(1)

    原文:Knockout应用开发指南 第三章:绑定语法(1) 第三章所有代码都需要启用KO的ko.applyBindings(viewModel);功能,才能使代码生效,为了节约篇幅,所有例子均省略了此 ...

  6. web.xml的运行顺序

    整体上的顺序为 <context-param> <listener> <filter> <servlet> 往下依次运行. 当中,每一个类别内部都是按序 ...

  7. Codeforces Jzzhu and Sequences(圆形截面)

    # include <stdio.h> int f[10]; int main() { int x,y,n,j; while(~scanf("%d%d%d",& ...

  8. 利用Javamail接收QQ邮箱和Gmail邮箱(转)

    求大神解答 Java代码: public class SendMailController { //@Autowired private JavaMailSenderImpl mailSender; ...

  9. What the difference between rebuild index and re-organize index?

    avg_fragmentation_in_percent value Corrective statement > 5% and < = 30% ALTER INDEX REORGANIZ ...

  10. windows phone 页面传值(7)

    原文:windows phone 页面传值(7) 在windows phone 中微软为我们提供了页面间传递参数的解决方案,下面就为大家介绍使用方法,页面传值的案例中我们建立两个页面,一个是MainP ...