Jack Straws
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)n (1 < n < 13)n(1<n<13) giving the number of straws on the table. Each of the next nnn lines contain 444 positive integers,x1,y1,x2x_1,y_1,x_2x1,y1,x2 and y2y_2y2, giving the coordinates, (x1,y1),(x2,y2)(x_1,y_1),(x_2,y_2)(x1,y1),(x2,y2) of the endpoints of a single straw. All coordinates will be less than 100100100. (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 111 and nnn, inclusive. You are to determine if straw a can be connected to straw b. When a=0=ba = 0 = ba=0=b, the current case is terminated.
When n=0n=0n=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 aaa and bbb, except the final line where a=0=ba = 0 = ba=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<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
namespace IO{
#define BUF_SIZE 100000
#define OUT_SIZE 100000
#define ll long long
bool IOerror=0;
inline char nc(){
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if (p1==pend){
p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin);
if (pend==p1){IOerror=1;return -1;}
}
return *p1++;
}
inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
inline bool read(int &x){
bool sign=0; char ch=nc(); x=0;
for (;blank(ch);ch=nc());
if (IOerror) return false;
if (ch=='-')sign=1,ch=nc();
for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
if (sign)x=-x; return true;
}
inline void read(ll &x){
bool sign=0; char ch=nc(); x=0;
for (;blank(ch);ch=nc());
if (IOerror)return;
if (ch=='-')sign=1,ch=nc();
for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
if (sign)x=-x;
}
inline void read(double &x){
bool sign=0; char ch=nc(); x=0;
for (;blank(ch);ch=nc());
if (IOerror)return;
if (ch=='-')sign=1,ch=nc();
for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
if (ch=='.'){
double tmp=1; ch=nc();
for (;ch>='0'&&ch<='9';ch=nc())tmp/=10.0,x+=tmp*(ch-'0');
}
if (sign)x=-x;
}
inline void read(char *s){
char ch=nc();
for (;blank(ch);ch=nc());
if (IOerror)return;
for (;!blank(ch)&&!IOerror;ch=nc())*s++=ch;
*s=0;
}
inline void read(char &c){
for (c=nc();blank(c);c=nc());
if (IOerror){c=-1;return;}
}
//fwrite->write
struct Ostream_fwrite{
char *buf,*p1,*pend;
Ostream_fwrite(){buf=new char[BUF_SIZE];p1=buf;pend=buf+BUF_SIZE;}
void out(char ch){
if (p1==pend){
fwrite(buf,1,BUF_SIZE,stdout);p1=buf;
}
*p1++=ch;
}
void print(int x){
static char s[15],*s1;s1=s;
if (!x)*s1++='0';if (x<0)out('-'),x=-x;
while(x)*s1++=x%10+'0',x/=10;
while(s1--!=s)out(*s1);
}
void println(int x){
static char s[15],*s1;s1=s;
if (!x)*s1++='0';if (x<0)out('-'),x=-x;
while(x)*s1++=x%10+'0',x/=10;
while(s1--!=s)out(*s1); out('\n');
}
void print(ll x){
static char s[25],*s1;s1=s;
if (!x)*s1++='0';if (x<0)out('-'),x=-x;
while(x)*s1++=x%10+'0',x/=10;
while(s1--!=s)out(*s1);
}
void println(ll x){
static char s[25],*s1;s1=s;
if (!x)*s1++='0';if (x<0)out('-'),x=-x;
while(x)*s1++=x%10+'0',x/=10;
while(s1--!=s)out(*s1); out('\n');
}
void print(double x,int y){
static ll mul[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,
1000000000,10000000000LL,100000000000LL,1000000000000LL,10000000000000LL,
100000000000000LL,1000000000000000LL,10000000000000000LL,100000000000000000LL};
if (x<-1e-12)out('-'),x=-x;x*=mul[y];
ll x1=(ll)floor(x); if (x-floor(x)>=0.5)++x1;
ll x2=x1/mul[y],x3=x1-x2*mul[y]; print(x2);
if (y>0){out('.'); for (size_t i=1;i<y&&x3*mul[i]<mul[y];out('0'),++i) {}; print(x3);}
}
void println(double x,int y){print(x,y);out('\n');}
void print(char *s){while (*s)out(*s++);}
void println(char *s){while (*s)out(*s++);out('\n');}
void flush(){if (p1!=buf){fwrite(buf,1,p1-buf,stdout);p1=buf;}}
~Ostream_fwrite(){flush();}
}Ostream;
inline void print(int x){Ostream.print(x);}
inline void println(int x){Ostream.println(x);}
inline void print(char x){Ostream.out(x);}
inline void println(char x){Ostream.out(x);Ostream.out('\n');}
inline void print(ll x){Ostream.print(x);}
inline void println(ll x){Ostream.println(x);}
inline void print(double x,int y){Ostream.print(x,y);}
inline void println(double x,int y){Ostream.println(x,y);}
inline void print(char *s){Ostream.print(s);}
inline void println(char *s){Ostream.println(s);}
inline void println(){Ostream.out('\n');}
inline void flush(){Ostream.flush();}
#undef ll
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace IO;
const int maxn=20;
const double eps=1e-10;
struct point{ //点结构体
double x,y;
point(double a=0,double b=0) {
x=a;y=b;
}
point operator+(point other) {
return point(x+other.x,y+other.y);
}
point operator-(point other) {
return point(x-other.x,y-other.y);
}
point operator*(double k) {
return point(x*k,y*k);
}
double dot(point other) { //点乘,即数量积,内积
return x*other.x+y*other.y;
}
double cha(point other) { //叉乘,即向量积,外积
return x*other.y-y*other.x;
}
bool onseg(point p1,point p2) {//判断q是否在线段p1-p2上
point q=*this;
return fabs((p1-q).cha(p2-q))<=eps&&(p1-q).dot(p2-q)<=0;
}
friend point intersect(point p1,point p2,point q1,point q2) {//计算直线p1-p2是否与直线q1-q2的交点
return p1+(p2-p1)*((q2-q1).cha(q1-p1)/(q2-q1).cha(p2-p1));
}
friend bool parallel(point p1,point p2,point q1,point q2) { //判断线段p1-p2是否与线段q1-q2平行,除去重合
return fabs((q2-q1).cha(p2-p1))<=eps;
}
friend bool parallel_intersect(point p1,point p2,point q1,point q2) { //判断线段p1-p2和线段q1-q2在平行的情况下是否有交点
return p1.onseg(q1,q2)||p2.onseg(q1,q2)||q1.onseg(p1,p2)||q2.onseg(p1,p2);
}
void print() {
printf("x:%.3lf y:%.3lf\n",x,y);
}
}p[maxn][2];
int n,connect[maxn][maxn];
struct dsu{
int fa[maxn],rank[maxn];
void init(int k){
for(int i=1;i<=k;i++) fa[i]=i,rank[i]=0;
}
int fin(int k){
return fa[k]==k?k:(fa[k]=fin(fa[k]));
}
void unite(int a,int b){
int x=fin(a),y=fin(b);
if(x==y) return;
if(rank[x]<rank[y])
fa[x]=y;
else{
fa[y]=x;
if(rank[x]==rank[y]) rank[x]++;
}
}
bool same(int a,int b){
return fin(a)==fin(b);
}
}tree;
int main()
{
//freopen("/Users/wzw/Documents/ACM模板/计算几何/1.in","r",stdin);
while(read(n)&&n){
memset(connect,0,sizeof(connect));tree.init(n);
for(int i=1;i<=n;i++) for(int j=0;j<=1;j++) read(p[i][j].x),read(p[i][j].y);
for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) {
if(parallel(p[i][0],p[i][1],p[j][0],p[j][1])) {if(parallel_intersect(p[i][0],p[i][1],p[j][0],p[j][1])) connect[i][j]=1;}
else {
point inter=intersect(p[i][0],p[i][1],p[j][0],p[j][1]);
if(inter.onseg(p[i][0],p[i][1])&&inter.onseg(p[j][0],p[j][1])) connect[i][j]=1;
}
}
for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) if(connect[i][j]) tree.unite(i,j);
int u,v;
while(read(u)&&read(v)&&u) printf(tree.same(u,v)?"CONNECTED\n":"NOT CONNECTED\n");
}
}