uva 11665 Chinese Ink (几何+并查集)
随便给12的找了一道我没做过的几何基础题。这题挺简单的,不过uva上通过率挺低,通过人数也不多。
题意是要求给出的若干多边形组成多少个联通块。做的时候要注意这题是不能用double浮点类型的,然后判多边形交只需要两个条件,存在边规范相交,或者存在一个多边形上的顶点在另一个多边形上或者在多边形内。
代码如下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const double EPS = 1e-;
const int N = ;
const int M = ;
inline int sgn(double x) { return (x > EPS) - (x < -EPS);} struct Point {
int x, y;
Point() {}
Point(int x, int y) : x(x), y(y) {}
Point operator + (Point a) { return Point(x + a.x, y + a.y);}
Point operator - (Point a) { return Point(x - a.x, y - a.y);}
Point operator * (int p) { return Point(x * p, y * p);}
Point operator / (int p) { return Point(x / p, y / p);}
} ; inline int cross(Point a, Point b) { return a.x * b.y - a.y * b.x;}
inline int dot(Point a, Point b) { return a.x * b.x + a.y * b.y;} inline bool onseg(Point x, Point a, Point b) { return sgn(cross(a - x, b - x)) == && sgn(dot(a - x, b - x)) <= ;}
bool ptinpoly(Point x, Point *pt, int n) {
pt[n] = pt[];
int wn = ;
for (int i = ; i < n; i++) {
if (onseg(x, pt[i], pt[i + ])) return true;
int dr = sgn(cross(pt[i + ] - pt[i], x - pt[i]));
int k1 = sgn(pt[i + ].y - x.y);
int k2 = sgn(pt[i].y - x.y);
if (dr > && k1 > && k2 <= ) wn++;
if (dr < && k2 > && k1 <= ) wn--;
}
return wn != ;
} struct MFS {
int fa[N], cnt;
void init() { for (int i = ; i < N; i++) fa[i] = i; cnt = ;}
int find(int x) { return fa[x] = fa[x] == x ? x : find(fa[x]);}
void merge(int x, int y) {
int fx = find(x);
int fy = find(y);
if (fx == fy) return ;
cnt++;
fa[fx] = fy;
}
} mfs; Point poly[N][M];
char buf[];
int sz[N]; bool ssint(Point a, Point b, Point c, Point d) {
int s1 = sgn(cross(a - c, b - c));
int s2 = sgn(cross(a - d, b - d));
int t1 = sgn(cross(c - a, d - a));
int t2 = sgn(cross(c - b, d - b));
return s1 * s2 < && t1 * t2 < ;
} bool polyint(int a, int b) {
poly[a][sz[a]] = poly[a][];
poly[b][sz[b]] = poly[b][];
for (int i = ; i < sz[a]; i++) {
for (int j = ; j < sz[b]; j++) {
if (ssint(poly[a][i], poly[a][i + ], poly[b][j], poly[b][j + ])) return true;
}
}
return false;
} bool test(int a, int b) {
for (int i = ; i < sz[a]; i++) if (ptinpoly(poly[a][i], poly[b], sz[b])) return true;
for (int i = ; i < sz[b]; i++) if (ptinpoly(poly[b][i], poly[a], sz[a])) return true;
if (polyint(a, b)) return true;
return false;
} int main() {
//freopen("in", "r", stdin);
int n;
while (cin >> n && n) {
gets(buf);
char *p;
mfs.init();
for (int i = ; i < n; i++) {
gets(buf);
p = strtok(buf, " ");
for (sz[i] = ; p; sz[i]++) {
sscanf(p, "%d", &poly[i][sz[i]].x);
p = strtok(NULL, " ");
sscanf(p, "%d", &poly[i][sz[i]].y);
p = strtok(NULL, " ");
}
//cout << sz[i] << endl;
for (int j = ; j < i; j++) if (test(i, j)) {
mfs.merge(i, j);
//cout << i << ' ' << j << endl;
}
}
cout << n - mfs.cnt << endl;
}
return ;
}
——written by Lyon
uva 11665 Chinese Ink (几何+并查集)的更多相关文章
- UVA 572 油田连通块-并查集解决
题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...
- UVA 12232 - Exclusive-OR(带权并查集)
UVA 12232 - Exclusive-OR 题目链接 题意:有n个数字.一開始值都不知道,每次给定一个操作,I a v表示确认a值为v,I a b v,表示确认a^b = v,Q k a1 a2 ...
- UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环
X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...
- uva 1493 - Draw a Mess(并查集)
题目链接:uva 1493 - Draw a Mess 题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...
- UVA 11987 Almost Union-Find (并查集+删边)
开始给你n个集合,m种操作,初始集合:{1}, {2}, {3}, … , {n} 操作有三种: 1 xx1 yy1 : 合并xx1与yy1两个集合 2 xx1 yy1 :将xx1元素分离出来合到yy ...
- UVA - 1160(简单建模+并查集)
A secret service developed a new kind of explosive that attain its volatile property only when a spe ...
- UVA 1493 Draw a Mess(并查集+set)
这题我一直觉得使用了set这个大杀器就可以很快的过了,但是网上居然有更好的解法,orz... 题意:给你一个最大200行50000列的墙,初始化上面没有颜色,接着在上面可能涂四种类型的形状(填充): ...
- UVa 1455 Kingdom 线段树 并查集
题意: 平面上有\(n\)个点,有一种操作和一种查询: \(road \, A \, B\):在\(a\),\(b\)两点之间加一条边 \(line C\):询问直线\(y=C\)经过的连通分量的个数 ...
- UVA 1329 Corporative Network【并查集】
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...
随机推荐
- neo4j遍历和图算法
阅读更多 这篇blog主要和大家分享一下neo4j中是如何对节点进行遍历,和其中集成的图论的一些常用算法. 遍历 http://docs.neo4j.org.cn/tutorials-java-emb ...
- Spring 的初次见面
简介: Spring Framework 是一个开源的企业级应用程序框架,为构建满足企业级需求的应用程序提供了大量的工具集.推出该框架的原因是在时候用J2EE进行开发是会提高复杂性. Spring三大 ...
- SSH 相关基础
检查是否安装: sudo apt-cache policy openssh-client sudo apt-cache policy openssh-server 也可直接用 sudo apt-ca ...
- python实现六大分群质量评估指标(兰德系数、互信息、轮廓系数)
python实现六大分群质量评估指标(兰德系数.互信息.轮廓系数) 1 R语言中的分群质量--轮廓系数 因为先前惯用R语言,那么来看看R语言中的分群质量评估,节选自笔记︱多种常见聚类模型以及分群质量评 ...
- 【django后端分离】mysql原生查询命令后,RawQueryset类型的自定义序列化返回json格式
1:设置mysql原生分页 # 监控系统首页显示分页 def MyPagination(limitid,offsetid): limitid =str(limitid) offsetid =str(o ...
- MySQL学习-- UNION与UNION ALL
UNION用于把来自许多SELECT语句的结果组合到一个结果集合中,也叫联合查询. ? 1 2 3 4 5 SELECT ... UNION [ALL | DISTINCT] SELECT ... [ ...
- spring中 使用说明
在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类 ...
- 【水滴石穿】ReactNative-Redux-Thunk
老实说,运行出来的项目让人失望,毕竟我想看各种有趣的demo啊- 先放上源码地址:https://github.com/ludejun/ReactNative-Redux-Thunk 我们来一起看看代 ...
- pytest 用 @pytest.mark.usefixtures("fixtureName")装饰类,可以让执行每个case前,都执行一遍指定的fixture
conftest.py import pytest import uuid @pytest.fixture() def declass(): print("declass:"+st ...
- JavaReflection(转载)
平时看代码时,总是碰到这些即熟悉又陌生的名次,每天都与他们相见,但见面后又似曾没有任何的交集,所以今天我就来认识下这两个江湖侠客的背景: CLASS 在Java中,每个class都有一个相应的Clas ...