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

判断两线段是否相交,直接上模板。

先用并查集预处理好两线段相交,最后判断两两是否在同一个集合就可以了。

 #include <stdio.h>
#define eps 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps) int cnt;
int lis[];
struct Point{
double x;
double y;
};
struct Line{
Point a1;
Point a2;
}ll[]; double xmult(Point p1, Point p2, Point p0){
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
} int dots_inline(Point p1, Point p2, Point p3){
return zero(xmult(p1,p2,p3));
} int dot_online_in(Point p,Line l){
return zero(xmult(p,l.a1,l.a2))&&(l.a1.x-p.x)*(l.a2.x-p.x)<eps
&&(l.a1.y-p.y)*(l.a2.y-p.y)<eps;
} int same_side(Point p1,Point p2,Line l){
return xmult(l.a1,p1,l.a2)*xmult(l.a1,p2,l.a2)>eps;
} int intersect_in(Line u, Line v){
if(!dots_inline(u.a1,u.a2,v.a1)||!dots_inline(u.a1,u.a2,v.a1)){
return !same_side(u.a1,u.a2,v)&&!same_side(v.a1,v.a2,u);
}
return dot_online_in(u.a1,v)||dot_online_in(u.a2,v)||
dot_online_in(v.a1,u)||dot_online_in(v.a1,u);
} void set(){
for(int i=; i<=cnt; i++){
lis[i]=i;
}
} int find(int u){
while(lis[u]!=u){
u=lis[u];
}
return u;
} int main(int argc, char *argv[])
{
while( scanf("%d",&cnt)!=EOF && cnt ){
for(int i=; i<=cnt; i++){
scanf("%lf %lf %lf %lf",&ll[i].a1.x ,&ll[i].a1.y ,&ll[i].a2.x ,&ll[i].a2.y);
}
set();
for(int i=; i<=cnt; i++){
for(int j=i+; j<=cnt; j++){
if(intersect_in(ll[i], ll[j])){
int tx=find(i);
int ty=find(j);
if(tx!=ty)
lis[tx]=ty;
}
}
}
int u,v;
while( scanf("%d %d" ,&u ,&v)!=EOF ){
if(u==&& v==)break;
if(find(u)==find(v))
puts("CONNECTED");
else
puts("NOT CONNECTED");
}
}
return ;
}

TOJ 1840 Jack Straws的更多相关文章

  1. 1840: Jack Straws

    1840: Jack Straws 时间限制(普通/Java):1000MS/10000MS     内存限制:65536KByte 总提交: 168            测试通过:129 描述 I ...

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

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

  3. TOJ1840: Jack Straws 判断两线段相交+并查集

    1840: Jack Straws  Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByteTotal Submit: 1 ...

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

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

  5. Jack Straws(poj 1127) 两直线是否相交模板

    http://poj.org/problem?id=1127   Description In the game of Jack Straws, a number of plastic or wood ...

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

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

  7. Jack Straws POJ - 1127 (简单几何计算 + 并查集)

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

  8. Jack Straws POJ - 1127 (几何计算)

    Jack Straws Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5428   Accepted: 2461 Descr ...

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

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

随机推荐

  1. web.xml配置及详解

    1.web.xml 是网络程序中的一个很重要的配置文件. 2.XML基础标准是为XML的进一步实用化制定的标准,它规定了采用XML制定标准时的一些公用特征.方法或规则.XML Schema描述了更加严 ...

  2. springcloud提供开放api接口签名验证

    一.MD5参数签名的方式 我们对api查询产品接口进行优化: 1.给app分配对应的key.secret 2.Sign签名,调用API 时需要对请求参数进行签名验证,签名方式如下: a. 按照请求参数 ...

  3. HTML <area> 标签区域map标签

    1.距形:(左上角顶点坐标为(x1,y1),右下角顶点坐标为(x2,y2)) <area shape="rect" coords="x1,y1,x2,y2" ...

  4. 什么是“光照度(Illuminance)”?

    光照度是光度学的概念,了解光照度,要从人眼的特性说起. 人眼的光谱响应 传统的辐射度学的概念(如“功率”,单位为“瓦”)可以客观描述“能量”,但当其用来描述“光照”时却是不合适的,原因在于:人眼对不同 ...

  5. ORB SLAM2 学习笔记

    cd ~/Documents/demos/ORB_SLAM2 ./Examples/RGB-D/rgbd_tum Vocabulary/ORBvoc.txt Examples/RGB-D/TUM1.y ...

  6. P1129 [ZJOI2007]矩阵游戏(二分图,网络流)

    传送门 这推导过程真的有点可怕的说……完全想不出来…… 最终状态是$(1,1),(2,2),(3,3)...(n,n)$都有一个黑点 我们可以理解为每一个行和列都形成了一个匹配 换句话说,只要$n$行 ...

  7. js原形链

    基本规则 1.每个对象都具有一个名为__proto__的属性: 2.每个构造函数(构造函数标准为大写开头,如Function(),Object()等等JS中自带的构造函数,以及自己创建的)都具有一个名 ...

  8. Python元类__prepare__方法深入理解

    学习元类的时候,对__prepare__不是很理解,书上讲解的也不是很详细,最后通过查看stackoverflow的一些帖子对该方法有了一些理解,记录如下: 先看代码: class member_ta ...

  9. Reviewing notes 2.1 of Mathematical analysis

    Chapter2 Numerical sequence and function Cartesian product set If S and T are sets,then the cartesia ...

  10. docker下ubutun没有ifconfig命令问题

    解决: apt-get update #更新apt-get apt install net-tools       # ifconfig apt install iputils-ping     # ...