标签:技术分享 cos play cst stack unsigned inf table nsis
Input
Output
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
题目大意:按顺序输入n个线段的两个坐标,然后多组输入判断两个线段是否是连接的(相交即为连接)。
题解:利用计算几何的知识,建立线段,如果有线段相交,就用并查集把它们连在一起,然后判断根节点是不是一个就好啦。比较简单的模板题目。
题意题解都来自谭总。代码还好要自己摸的
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<sstream> #include<cmath> #include<stack> #include<map> #include<cstdlib> #include<vector> #include<string> #include<queue> using namespace std; #define ll long long #define llu unsigned long long #define INF 0x3f3f3f3f const double PI = acos(-1.0); const int maxn = 1e3+10; const int mod = 1e9+7; struct node{ double x,y; }; struct Line{ node a; node b; }line[15]; int father[20]; void init() { for(int i=0;i<20;i++) father[i] = i; } double cross(node a,node b,node o) { return (a.x-o.x)*(b.y-o.y) - (b.x-o.x)*(a.y-o.y); } bool connect(Line u,Line v) { return (cross(v.a,u.b,u.a) * cross(u.b,v.b,u.a) >= 0) && (cross(u.a,v.b,v.a) * cross(v.b,u.b,v.a) >=0 ) && (max(u.a.x,u.b.x) >= min(v.a.x,v.b.x)) && (max(v.a.x,v.b.x) >= min(u.a.x,u.b.x)) && (max(u.a.y,u.b.y) >= min(v.a.y,v.b.y)) && (max(v.a.y,v.b.y) >= min(u.a.y,u.b.y)); } int find(int x) { return x == father[x] ? x : father[x] = find(father[x]); } void combine(int x,int y) { x = find(x); y = find(y); if(x != y) father[x] = y; } int main() { int n; while(scanf("%d",&n) && n) { init(); for(int i=1;i<=n;i++) scanf("%lf %lf %lf %lf",&line[i].a.x,&line[i].a.y,&line[i].b.x,&line[i].b.y); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(connect(line[i],line[j])) combine(i,j); int a,b; while(scanf("%d %d",&a,&b) && a+b) { if(find(a) == find(b)) puts("CONNECTED"); else puts("NOT CONNECTED"); } } }
Jack Straws POJ - 1127 (简单几何计算 + 并查集)
标签:技术分享 cos play cst stack unsigned inf table nsis
原文地址:https://www.cnblogs.com/smallhester/p/10331472.html