1840: Jack Straws 

Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByte
Total Submit: 154            Accepted:119

Description

In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the table and players try to remove them one-by-one without disturbing the other straws. Here, we are only concerned with if various pairs of straws are connected by a path of touching straws. You will be given a list of the endpoints for some straws (as if they were dumped on a large piece of graph paper) and then will be asked if various pairs of straws are connected. Note that touching is connecting, but also two straws can be connected indirectly via other connected straws.

Input

Input consist multiple case,each case consists of multiple lines. The first line will be an integer n (1 < n < 13) giving the number of straws on the table. Each of the next n lines contain 4 positive integers,x1,y1,x2 and y2, giving the coordinates, (x1,y1),(x2,y2) of the endpoints of a single straw. All coordinates will be less than 100. (Note that the straws will be of varying lengths.) The first straw entered will be known as straw #1, the second as straw #2, and so on. The remaining lines of the current case(except for the final line) will each contain two positive integers, a and b, both between 1 and n, inclusive. You are to determine if straw a can be connected to straw b. When a = 0 = b, the current case is terminated.

When n=0,the input is terminated.

There will be no illegal input and there are no zero-length straws.

Output

You should generate a line of output for each line containing a pair a and b, except the final line where a = 0 = b. The line should say simply "CONNECTED", if straw a is connected to straw b, or "NOT CONNECTED", if straw a is not connected to straw b. For our purposes, a straw is considered connected to itself.

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

Source

East Central North America 1994

我的代码不优秀啊,卡不到0ms,但是这个思想还是挺好的,记录下吧

#include<stdio.h>
#include<algorithm>
using namespace std;
int fa[],n,a,b;
struct Point
{
int x1,x2,y1,y2;
Point(int x1=,int x2=,int y1=,int y2=):x1(x1),x2(x2),y1(y1),y2(y2){}
void read()
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
}
} p[];
int find(int x)
{
return x==fa[x]?x:fa[x]=find(fa[x]);
}
int cross(int x1,int y1,int x2,int y2)
{
return x1*y2-x2*y1;
}
int la(Point A,Point B)
{
if(max(A.x1,A.x2)<min(B.x1,B.x2)||max(A.y1,A.y2)<min(B.y1,B.y2)||max(B.x1,B.x2)<min(A.x1,A.x2)||max(B.y1,B.y2)<min(A.y1,A.y2)) return ;
int a=cross(A.x2-A.x1,A.y2-A.y1,B.x1-A.x1,B.y1-A.y1)*cross(A.x2-A.x1,A.y2-A.y1,B.x2-A.x1,B.y2-A.y1),b=cross(B.x2-B.x1,B.y2-B.y1,A.x1-B.x1,A.y1-B.y1)*cross(B.x2-B.x1,B.y2-B.y1,A.x2-B.x1,A.y2-B.y1);
if(a<=&&b<=)return ;
return ;
}
int main()
{
while(scanf("%d",&n),n)
{
for(int i=; i<=n; i++)
fa[i]=i,p[i].read();
for(int i=; i<n; i++)
for(int j=i+; j<=n; j++)
if(la(p[i],p[j]))
{
a=find(i),b=find(j);
if(a!=b)fa[a]=b;
}
while(scanf("%d%d",&a,&b),a||b)
{
a=find(a),b=find(b);
if(a!=b)printf("NOT ");
printf("CONNECTED\n");
}
}
return ;
}

TOJ1840: Jack Straws 判断两线段相交+并查集的更多相关文章

  1. poj 1127:Jack Straws(判断两线段相交 + 并查集)

    Jack Straws Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2911   Accepted: 1322 Descr ...

  2. poj 1127 -- Jack Straws(计算几何判断两线段相交 + 并查集)

    Jack Straws In the game of Jack Straws, a number of plastic or wooden "straws" are dumped ...

  3. hdu 1147:Pick-up sticks(基本题,判断两线段相交)

    Pick-up sticks Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  4. You can Solve a Geometry Problem too (hdu1086)几何,判断两线段相交

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3276 ...

  5. hdu 1086:You can Solve a Geometry Problem too(计算几何,判断两线段相交,水题)

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  6. hdu 1558 线段相交+并查集

    题意:要求相交的线段都要塞进同一个集合里 sol:并查集+判断线段相交即可.n很小所以n^2就可以水过 #include <iostream> #include <cmath> ...

  7. TZOJ 1840 Jack Straws(线段相交+并查集)

    描述 In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the ta ...

  8. poj1127 Jack Straws(线段相交+并查集)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Jack Straws Time Limit: 1000MS   Memory L ...

  9. [poj 1127]Jack Straws[线段相交][并查集]

    题意: 给出一系列线段,判断某两个线段是否连通. 思路: 根据线段相交情况建立并查集, 在同一并查集中则连通. (第一反应是强连通分量...实际上只要判断共存即可, 具体的方向啊是没有关系的..) 并 ...

随机推荐

  1. 学习Linux的好网站

    http://www.linuxcommand.org/ 很不错的学习shell和script的教程.包含:Learning the shell 和 writing shell scripts 两个内 ...

  2. UVA 624 CD(01背包,要记录路径)

    题意: 有n张CD(n<=20),每张能播放的时长不同.给定一个时长限制t,挑出部分的CD使得总播放时间最长.顺便输出路径! 思路: 重点在输出路径,否则这题很普通.那就要用二维数组记录每个CD ...

  3. Ubuntu系统Apache 2部署SSL证书

    几天前用Apache 2部署了一个静态网页,但通过域名访问时Google提示“不安全”,经了解,原来是缺少证书. 什么是SSL证书? SSL 是指安全套接字层,简而言之,它是一项标准技术,可确保互联网 ...

  4. Array - Remove Element

    /** * 无额外空间.顺序可以被改变.不需要修改后面的数字. * @param nums 数组 * @param val 目标值 * @return nums中移除val后的长度 */ public ...

  5. Array - Two Sum

    import java.util.HashMap; import java.util.Map; /** * 分析: * 普通实现-嵌套循环两次,时间O(n2),空间O(1) * 复杂实现-循环一次,时 ...

  6. session添加登录次数限制

    session 中设置了生存期,20分钟,输入密码错误次数保存到session 中,过一段时间自动解除: //登陆的用户名或者密码出错次数 int n = 0; if(logintimes == nu ...

  7. VMware的centos的配置分区

    /      ext3 8189 固定大小空     swap 509  固定大小/boot  ext3 100  固定大小/home  ext3 全部(使用全部可用空间) 利用的工具   AMFTP ...

  8. Linux curl命令中,HTTP 302处理

    在Linux中使用curl命令时,偶尔会遇到一些URL跳转到新的URL,即HTTP中的3XX(redirection,重定向 ). $curl -s -I $URL > log 这时在返回的报文 ...

  9. 【单调队列】P1886 滑动窗口

    GET 单调队列 题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: Th ...

  10. mysql命令行复制数据库

    为了方便快速复制一个数据库,可以用以下命令将db1数据库的数据以及表结构复制到newdb数据库创建新的数据库#mysql -u root -p123456 mysql>CREATE DATABA ...