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

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

  1 /**
2  * code generated by JHelper
4  * @author xyiyy @https://github.com/xyiyy
5  */
6
7 #include <iostream>
8 #include <fstream>
9
10 //#####################
11 //Author:fraud
12 //Blog: http://www.cnblogs.com/fraud/
13 //#####################
15 #include <iostream>
16 #include <sstream>
17 #include <ios>
18 #include <iomanip>
19 #include <functional>
20 #include <algorithm>
21 #include <vector>
22 #include <string>
23 #include <list>
24 #include <queue>
25 #include <deque>
26 #include <stack>
27 #include <set>
28 #include <map>
29 #include <cstdio>
30 #include <cstdlib>
31 #include <cmath>
32 #include <cstring>
33 #include <climits>
34 #include <cctype>
35
36 using namespace std;
37 #define rep(X, N) for(int X=0;X<N;X++)
38 #define rep2(X, L, R) for(int X=L;X<=R;X++)
39
40 const int MAXN = 110;
41 //
42 // Created by xyiyy on 2015/8/8.
43 //
44
45 #ifndef JHELPER_EXAMPLE_PROJECT_UNIONFINDSET_HPP
46 #define JHELPER_EXAMPLE_PROJECT_UNIONFINDSET_HPP
47
48 int pa[MAXN], ra[MAXN];
49
50 void init(int n) {
51     rep(i, n + 1)pa[i] = i, ra[i] = 0;
52 }
53
54 int find(int x) {
55     if (pa[x] != x)pa[x] = find(pa[x]);
56     return pa[x];
57 }
58
59 int unite(int x, int y) {
60     x = find(x);
61     y = find(y);
62     if (x == y)return 0;
63     if (ra[x] < ra[y])pa[x] = y;
64     else {
65         pa[y] = x;
66         if (ra[x] == ra[y])ra[x]++;
67     }
68     return 1;
69 }
70
71 bool same(int x, int y) {
72     return find(x) == find(y);
73 }
74
75 #endif //JHELPER_EXAMPLE_PROJECT_UNIONFINDSET_HPP
76
77 //
78 // Created by xyiyy on 2015/8/10.
79 //
80
81 #ifndef JHELPER_EXAMPLE_PROJECT_P_HPP
82 #define JHELPER_EXAMPLE_PROJECT_P_HPP
83
84
85 const double EPS = 1e-9;
86
87 class P {
88 public:
89     double x, y;
90
91     P() { }
92
93     P(double _x, double _y) {
94         x = _x;
95         y = _y;
96     }
97
98     double add(double a, double b) {
99         if (fabs(a + b) < EPS * (fabs(a) + fabs(b)))return 0;
100         return a + b;
101     }
102
103     P  operator+(const P &p) {
105     }
106
107     P operator-(const P &p) {
109     }
110
111     P operator*(const double &d) {
112         return P(x * d, y * d);
113     }
114
115     P operator/(const double &d) {
116         return P(x / d, y / d);
117     }
118
119     double det(P p) {
120         return add(x * p.y, -y * p.x);
121     }
122
123     //线段相交判定
124     bool crsSS(P p1, P p2, P q1, P q2) {
125         if (max(p1.x, p2.x) + EPS < min(q1.x, q2.x))return false;
126         if (max(q1.x, q2.x) + EPS < min(p1.x, p2.x))return false;
127         if (max(p1.y, p2.y) + EPS < min(q1.y, q2.y))return false;
128         if (max(q1.y, q2.y) + EPS < min(p1.y, p2.y))return false;
129         /*(if((p1 - p2).det(q1 - q2) == 0){
130             return (on_seg(p1,p2,q1) || on_seg(p1,p2,q2) || on_seg(q1,q2,p1) || on_seg(q1,q2,p2));
131         }else{
132             P r = intersection(p1,p2,q1,q2);
133             return on_seg(p1,p2,r) && on_seg(q1,q2,r);
134
135         }*/
136         return (p2 - p1).det(q1 - p1) * (p2 - p1).det(q2 - p1) <= 0
137                && (q2 - q1).det(p1 - q1) * (q2 - q1).det(p2 - q1) <= 0;
138     }
139
140     //直线和直线的交点
141     /*P isLL(P p1,P p2,P q1,P q2){
142         double d = (q2 - q1).det(p2 - p1);
143         if(sig(d)==0)return NULL;
144         return intersection(p1,p2,q1,q2);
145     }*/
146
147     //四点共圆判定
148     /*bool onC(P p1,P p2,P p3,P p4){
149         P c = CCenter(p1,p2,p3);
150         if(c == NULL) return false;
151         return add((c - p1).abs2(), -(c - p4).abs2()) == 0;
152     }*/
153
154     //三点共圆的圆心
155     /*P CCenter(P p1,P p2,P p3){
156         //if(disLP(p1, p2, p3) < EPS)return NULL;//三点共线
157         P q1 = (p1 + p2) * 0.5;
158         P q2 = q1 + ((p1 - p2).rot90());
159         P s1 = (p3 + p2) * 0.5;
160         P s2 = s1 + ((p3 - p2).rot90());
161         return isLL(q1,q2,s1,s2);
162     }*/
163
164
165 };
166
167
168 #endif //JHELPER_EXAMPLE_PROJECT_P_HPP
169
170 class poj1127 {
171 public:
172     void solve(std::istream &in, std::ostream &out) {
173         int n;
174         P *p = new P[110];
175         P *q = new P[110];
176         while (in >> n && n) {
177             init(n + 5);
178             rep2(i, 1, n) {
179                 in >> p[i].x >> p[i].y >> q[i].x >> q[i].y;
180             }
181             rep2(i, 1, n) {
182                 rep2(j, 1, n) {
183                     if (p[i].crsSS(p[i], q[i], p[j], q[j]))unite(i, j);
184                 }
185             }
186             int u, v;
187             while (in >> u >> v && (u && v)) {
188                 if (same(u, v))out << "CONNECTED" << endl;
189                 else out << "NOT CONNECTED" << endl;
190             }
191         }
192     }
193 };
194
195 int main() {
196     std::ios::sync_with_stdio(false);
197     std::cin.tie(0);
198     poj1127 solver;
199     std::istream &in(std::cin);
200     std::ostream &out(std::cout);
201     solver.solve(in, out);
202     return 0;
203 }

posted on 2015-08-30 23:30  xyiyy  阅读(...)  评论(...编辑  收藏

• 随笔 - 102
• 文章 - 0
• 评论 - 7
• 引用 - 0