转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

Jack Straws
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3512   Accepted: 1601

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

这题还是比较简单的,就是问两条线段是否直接或者间接的相连。注意考虑好有一段是重叠的情况即可

 /**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author xyiyy @https://github.com/xyiyy
*/ #include <iostream>
#include <fstream> //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype> using namespace std;
#define rep(X, N) for(int X=0;X<N;X++)
#define rep2(X, L, R) for(int X=L;X<=R;X++) const int MAXN = ;
//
// Created by xyiyy on 2015/8/8.
// #ifndef JHELPER_EXAMPLE_PROJECT_UNIONFINDSET_HPP
#define JHELPER_EXAMPLE_PROJECT_UNIONFINDSET_HPP int pa[MAXN], ra[MAXN]; void init(int n) {
rep(i, n + )pa[i] = i, ra[i] = ;
} int find(int x) {
if (pa[x] != x)pa[x] = find(pa[x]);
return pa[x];
} int unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)return ;
if (ra[x] < ra[y])pa[x] = y;
else {
pa[y] = x;
if (ra[x] == ra[y])ra[x]++;
}
return ;
} bool same(int x, int y) {
return find(x) == find(y);
} #endif //JHELPER_EXAMPLE_PROJECT_UNIONFINDSET_HPP //
// Created by xyiyy on 2015/8/10.
// #ifndef JHELPER_EXAMPLE_PROJECT_P_HPP
#define JHELPER_EXAMPLE_PROJECT_P_HPP const double EPS = 1e-; class P {
public:
double x, y; P() { } P(double _x, double _y) {
x = _x;
y = _y;
} double add(double a, double b) {
if (fabs(a + b) < EPS * (fabs(a) + fabs(b)))return ;
return a + b;
} P operator+(const P &p) {
return P(add(x, p.x), add(y, p.y));
} P operator-(const P &p) {
return P(add(x, -p.x), add(y, -p.y));
} P operator*(const double &d) {
return P(x * d, y * d);
} P operator/(const double &d) {
return P(x / d, y / d);
} double det(P p) {
return add(x * p.y, -y * p.x);
} //线段相交判定
bool crsSS(P p1, P p2, P q1, P q2) {
if (max(p1.x, p2.x) + EPS < min(q1.x, q2.x))return false;
if (max(q1.x, q2.x) + EPS < min(p1.x, p2.x))return false;
if (max(p1.y, p2.y) + EPS < min(q1.y, q2.y))return false;
if (max(q1.y, q2.y) + EPS < min(p1.y, p2.y))return false;
/*(if((p1 - p2).det(q1 - q2) == 0){
return (on_seg(p1,p2,q1) || on_seg(p1,p2,q2) || on_seg(q1,q2,p1) || on_seg(q1,q2,p2));
}else{
P r = intersection(p1,p2,q1,q2);
return on_seg(p1,p2,r) && on_seg(q1,q2,r); }*/
return (p2 - p1).det(q1 - p1) * (p2 - p1).det(q2 - p1) <=
&& (q2 - q1).det(p1 - q1) * (q2 - q1).det(p2 - q1) <= ;
} //直线和直线的交点
/*P isLL(P p1,P p2,P q1,P q2){
double d = (q2 - q1).det(p2 - p1);
if(sig(d)==0)return NULL;
return intersection(p1,p2,q1,q2);
}*/ //四点共圆判定
/*bool onC(P p1,P p2,P p3,P p4){
P c = CCenter(p1,p2,p3);
if(c == NULL) return false;
return add((c - p1).abs2(), -(c - p4).abs2()) == 0;
}*/ //三点共圆的圆心
/*P CCenter(P p1,P p2,P p3){
//if(disLP(p1, p2, p3) < EPS)return NULL;//三点共线
P q1 = (p1 + p2) * 0.5;
P q2 = q1 + ((p1 - p2).rot90());
P s1 = (p3 + p2) * 0.5;
P s2 = s1 + ((p3 - p2).rot90());
return isLL(q1,q2,s1,s2);
}*/ }; #endif //JHELPER_EXAMPLE_PROJECT_P_HPP class poj1127 {
public:
void solve(std::istream &in, std::ostream &out) {
int n;
P *p = new P[];
P *q = new P[];
while (in >> n && n) {
init(n + );
rep2(i, , n) {
in >> p[i].x >> p[i].y >> q[i].x >> q[i].y;
}
rep2(i, , n) {
rep2(j, , n) {
if (p[i].crsSS(p[i], q[i], p[j], q[j]))unite(i, j);
}
}
int u, v;
while (in >> u >> v && (u && v)) {
if (same(u, v))out << "CONNECTED" << endl;
else out << "NOT CONNECTED" << endl;
}
}
}
}; int main() {
std::ios::sync_with_stdio(false);
std::cin.tie();
poj1127 solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
solver.solve(in, out);
return ;
}

代码君

poj1127 Jack Straws(线段相交+并查集)的更多相关文章

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

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

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

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

  3. TTTTTTTTTTTTTT poj 1127 Jack Straws 线段相交+并查集

    题意: 有n个木棍,给出木棍的两个端点的x,y坐标,判断其中某两个线段是否连通(可通过其他线段连通) #include <iostream> #include <cstdio> ...

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

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

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

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

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

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

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

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

  8. 判断线段相交(hdu1558 Segment set 线段相交+并查集)

    先说一下题目大意:给定一些线段,这些线段顺序编号,这时候如果两条线段相交,则把他们加入到一个集合中,问给定一个线段序号,求在此集合中有多少条线段. 这个题的难度在于怎么判断线段相交,判断玩相交之后就是 ...

  9. hdu 1558 (线段相交+并查集) Segment set

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1558 题意是在坐标系中,当输入P(注意是大写,我当开始就wa成了小写)的时候输入一条线段的起点坐标和终点坐 ...

随机推荐

  1. 数据库和Doctrine(转载自http://www.111cn.net/phper/332/85987.htm)

    对于任何应用程序来说最为普遍最具挑战性的任务,就是从数据库中 读取和持久化数据信息.尽管symfony完整的框架没有默认集成ORM,但是symfony标准版,集成了很多程序,还自带集成了Doctrin ...

  2. JS之对象数组遍历?

    一.js实现遍历对象 <script> ","destroy":"97%"}; var props = ""; for ...

  3. dede 删除栏目文章后, 让ID从1开始

    1)删除所有栏目,新建ID从1开始: ALTER TABLE `dede_arctype` AUTO_INCREMENT =1; 2)删除所有文章,新发布文章ID从1开始: ALTER TABLE ` ...

  4. 【结构型】Composite模式

    组合模式意在将对象组合成树形结构以表示部分与整体的层次结构关系,并且用户对单个对象的操作以有对组合对象的操作都是一致的.即:组合对象 is-a 单个对象,同时又可以组合着 n 个的单个对象(甚至于其他 ...

  5. day03

    1.set集合--无序的,不重复的序列,类似dict,但是只有key,没有value 创建一个集合: s1 = {11,22,33} s2 = set((22,33,44))必须传入一个可迭代对象(t ...

  6. iOS开发——C篇&动态内存分配

    再C语言中关于内存是一个很重要的知识点,所以今天我就从c语言的内存分配开始为大家解析一下C语言再iOS开发中非常重要的一些知识. 1:malloc函数的介绍 C语言中开辟内存空间:malloc函数 再 ...

  7. MySQL执行外部sql脚本

    1:-/mysql_test/test.sql create table student( sno int not null primary key auto_increment, sname ) n ...

  8. Visual Studio 2015 Owin+MVC+WebAPI+ODataV4+EntityFrawork+Identity+Oauth2.0+AngularJS 1.x 学习笔记之"坑"

    1.AngularJS route 与 MVC route http://www.cnblogs.com/usea/p/4211989.html public class SingleRoute : ...

  9. MFC动态创建按钮,并在按钮上实现位图的切换显示

    动态创建按钮,并在按钮中添加位图,通过单击按钮显示不同的位图,可设置为显示按钮按下和弹起两种状态.只要判断a值从而输入不同的响应代码. 1.在头文件中添加: CButton *pBtn; 2.在初始化 ...

  10. 转:Yii 常量的轻松管理

    问题 我经常在不同的地方使用模型中的常量(基本状态常量),当常量改变时我不得不在使用每处它的代码中修改. 获取常量 为了解决这个问题我使用了一个方法 getConstants(). public st ...