Jack Straws POJ - 1127 (简单几何计算 + 并查集)
Input
When n=0,the input is terminated.
There will be no illegal input and there are no zero-length straws.
Output
Sample Input
7
1 6 3 3
4 6 4 9
4 5 6 7
1 4 3 5
3 5 5 5
5 2 6 3
5 4 7 2
1 4
1 6
3 3
6 7
2 3
1 3
0 0 2
0 2 0 0
0 0 0 1
1 1
2 2
1 2
0 0 0
Sample Output
CONNECTED
NOT CONNECTED
CONNECTED
CONNECTED
NOT CONNECTED
CONNECTED
CONNECTED
CONNECTED
CONNECTED
题目大意:按顺序输入n个线段的两个坐标,然后多组输入判断两个线段是否是连接的(相交即为连接)。
题解:利用计算几何的知识,建立线段,如果有线段相交,就用并查集把它们连在一起,然后判断根节点是不是一个就好啦。比较简单的模板题目。
题意题解都来自谭总。代码还好要自己摸的
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<map>
#include<cstdlib>
#include<vector>
#include<string>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
const double PI = acos(-1.0);
const int maxn = 1e3+;
const int mod = 1e9+; struct node{
double x,y;
};
struct Line{
node a;
node b;
}line[];
int father[];
void init()
{
for(int i=;i<;i++)
father[i] = i;
}
double cross(node a,node b,node o)
{
return (a.x-o.x)*(b.y-o.y) - (b.x-o.x)*(a.y-o.y);
}
bool connect(Line u,Line v)
{
return (cross(v.a,u.b,u.a) * cross(u.b,v.b,u.a) >= ) &&
(cross(u.a,v.b,v.a) * cross(v.b,u.b,v.a) >= ) &&
(max(u.a.x,u.b.x) >= min(v.a.x,v.b.x)) &&
(max(v.a.x,v.b.x) >= min(u.a.x,u.b.x)) &&
(max(u.a.y,u.b.y) >= min(v.a.y,v.b.y)) &&
(max(v.a.y,v.b.y) >= min(u.a.y,u.b.y));
}
int find(int x)
{
return x == father[x] ? x : father[x] = find(father[x]);
}
void combine(int x,int y)
{
x = find(x);
y = find(y);
if(x != y)
father[x] = y;
}
int main()
{
int n;
while(scanf("%d",&n) && n)
{
init();
for(int i=;i<=n;i++)
scanf("%lf %lf %lf %lf",&line[i].a.x,&line[i].a.y,&line[i].b.x,&line[i].b.y);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(connect(line[i],line[j]))
combine(i,j);
int a,b;
while(scanf("%d %d",&a,&b) && a+b)
{
if(find(a) == find(b))
puts("CONNECTED");
else
puts("NOT CONNECTED");
}
}
}
Jack Straws POJ - 1127 (简单几何计算 + 并查集)的更多相关文章
- Jack Straws POJ - 1127 (几何计算)
Jack Straws Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5428 Accepted: 2461 Descr ...
- Jack Straws(POJ 1127)
原题如下: Jack Straws Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5555 Accepted: 2536 ...
- poj 1127:Jack Straws(判断两线段相交 + 并查集)
Jack Straws Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2911 Accepted: 1322 Descr ...
- poj 1127(直线相交+并查集)
Jack Straws Description In the game of Jack Straws, a number of plastic or wooden "straws" ...
- Jack Straws(判断线段是否相交 + 并查集)
/** http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1840 题意: 判断线段 ...
- poj 2236:Wireless Network(并查集,提高题)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 16065 Accepted: 677 ...
- LA3027简单带权并查集
题意: 有n个点,一开始大家都是独立的点,然后给出一些关系,a,b表示a是b的父亲节点,距离是abs(a-b)%1000,然后有一些询问,每次询问一个节点a到父亲节点的距离是多少? 思路: ...
- POJ 1611 The Suspects(并查集,简单)
为什么ACM的题意都这么难懂,就不能说的直白点吗?还能不能好好的一起刷题了? 题意:你需要建一个n的并查集,有m个集合,最后要输出包含0的那个集合的元素的个数. 这是简单并查集应用,所以直接看代码吧! ...
- poj 2492 a bug's life 简单带权并查集
题意大致为找同性恋的虫子.... 这个比食物链要简单些.思路完全一致,利用取余操作实现关系之间的递推. 个人感觉利用向量,模和投影可能可以实现具有更加复杂关系的并查集. #include<ios ...
随机推荐
- Unity Download Assistant Error: 'SendRequest Error' while downloading ini file from http://files.unity3d.com/bootstrapper/29055738eb78/unity-5.3.6f1-win.ini
Unity 官网的哥们如此说道 I open the exe on Compatibility Mode , it's solved. You can try. :) 翻译就是 我用兼容模式打开,就能 ...
- nginx fpm生产环境的权限设置
http://www.2cto.com/Article/201307/231770.html
- 做一个vue模态弹出框如何
运用的知识点包括: 路由的配置 插槽 vue的过渡动画 路由重定向 router/index.js里面配置路由 import Vue from 'vue' import Router from 'vu ...
- Every ending is just a new beginning.
Every ending is just a new beginning.每次结束都是新的开始.
- 本地eclipse启动tomcat后无法访问
转自博文:http://blog.csdn.net/wqjsir/article/details/7169838/ 症状: tomcat在eclipse里面能正常启动,而在浏览器中访问http://l ...
- 性能调优--大事务与Alwayson 之间的关系
最近性能调优的事比较多,所以摘一些比较有特点的 案例分享下. 业务系统用的是sql server 2016 ,搭建的ALWAYSON 两节点的 群集,今天早上突然辅助 副本的只读库出现大量的等待导致系 ...
- LeetCode Find Peak Element 找临时最大值
Status: AcceptedRuntime: 9 ms 题意:给一个数组,用Vector容器装的,要求找到一个临时最高点,可以假设有num[-1]和num[n]两个元素,都是无穷小,那么当只有一个 ...
- hdu1150&&POJ1325 Machine Schedule---最小点覆盖
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1150 题目大意: 给你两台机器A和B,A机器有n种模式,B机器有m种模式,初始时都是0,现在给你k个 ...
- vuejs组件的重要选项
new Vue({ el:'#demo', data:{ message:'Hello vue.js!' } }) 我们看到这个括号里面包含了很多中间的选项,小括号里面其实是一些参数,这些参数指定了实 ...
- 5分钟了解Java 12 八大新特性
Java 12 终于发布了,我们一起来看一看 Java 12 中的新特性.通过本文可以在5分钟内快速了解 Java 12 新特性. 1 Switch 表达式 使用Java 12,switch不仅可以作 ...