UVA 11665

  随便给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 (几何+并查集)的更多相关文章

  1. UVA 572 油田连通块-并查集解决

    题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...

  2. 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 ...

  3. UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环

    X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...

  4. uva 1493 - Draw a Mess(并查集)

    题目链接:uva 1493 - Draw a Mess 题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...

  5. UVA 11987 Almost Union-Find (并查集+删边)

    开始给你n个集合,m种操作,初始集合:{1}, {2}, {3}, … , {n} 操作有三种: 1 xx1 yy1 : 合并xx1与yy1两个集合 2 xx1 yy1 :将xx1元素分离出来合到yy ...

  6. UVA - 1160(简单建模+并查集)

    A secret service developed a new kind of explosive that attain its volatile property only when a spe ...

  7. UVA 1493 Draw a Mess(并查集+set)

    这题我一直觉得使用了set这个大杀器就可以很快的过了,但是网上居然有更好的解法,orz... 题意:给你一个最大200行50000列的墙,初始化上面没有颜色,接着在上面可能涂四种类型的形状(填充):  ...

  8. UVa 1455 Kingdom 线段树 并查集

    题意: 平面上有\(n\)个点,有一种操作和一种查询: \(road \, A \, B\):在\(a\),\(b\)两点之间加一条边 \(line C\):询问直线\(y=C\)经过的连通分量的个数 ...

  9. UVA 1329 Corporative Network【并查集】

    题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...

随机推荐

  1. 【模板】Vector存图 + SPFA

    最近愉快地决定要由边集数组转向Vector存图,顺便开始图论集训 老惯例,以题写模板 P1339 [USACO09OCT]热浪Heat Wave 题目描述 The good folks in Texa ...

  2. 那些年,我们见过的Java服务端乱象

    导读 查尔斯·狄更斯在<双城记>中写道:“这是一个最好的时代,也是一个最坏的时代.”移动互联网的快速发展,出现了许多新机遇,很多创业者伺机而动:随着行业竞争加剧,互联网红利逐渐消失,很多创 ...

  3. jquery tooltip.js

    1.引用关联的js脚本 <script type="text/javascript" src="script/jquery-1.3.2.min.js"&g ...

  4. HR招聘_(七)_招聘方法论(面试环节·动机判断)

    候选人选择一般会看硬性技能,软性技能,动机意愿三个方面的匹配程度,硬性技能主要指纵向的业务能力,部门面试官也会着重看这方面,软性技能包括沟通,情商,气质等.动机意愿非常重要,再优秀的如果没有意愿,动机 ...

  5. Android——兼容性

    两种类型的兼容性:设备兼容性和应用兼容性. 设备的Android兼容性就是app能够正确运行的Android执行环境(Android execution environment).Android执行环 ...

  6. 生成mysql数据字典

    data_dictionary.php <?php /** * 生成mysql数据字典 */ header("Content-type: text/html; charset=utf- ...

  7. java 3 接口与多态&输入输出流

    接口 中的所有方法都是方法 抽象 使用接口实现多继承 类型的装换 数据成员就变成了static 和 final food 和 snow 都是可以吃的 可以同时实现多个接口 接口与接口之间也可以有继承关 ...

  8. Ubuntu中NS2安装详细教程

    前言: NS2是指 Network Simulator version 2,NS(Network Simulator) 是一种针对网络技术的源代码公开的.免费的软件模拟平台,研究人员使用它可以很容易的 ...

  9. 移动项目到centos中运行报错:failed to open stream: Permission denied

    这是文件夹没有读写权限的错误: (注意:TP5.0权限给runtime文件夹就行了,官方文档在安装tp5的方法中有介绍到权限问题) 在需要赋予权限的文件夹的前一级输入: chmod -R 文件夹名字

  10. Java中时间和日期的处理

    一.日期转换为字符串 1.日期以特定的格式输出: // 创建日期并转换为yyyy-MM-dd格式_(MM一定要大写,以免与hh:mm:ss中的mm冲突) SimpleDateFormat sdf = ...