hdu1558(并查集)
来源:互联网 发布:餐饮成本核算软件 编辑:程序博客网 时间:2024/05/16 11:51
这道题的难点在于如何判断两条线段是否有交点
首先,我们可以想象一下,两个线段如果相交,则从一条线段可以看到一个另一个线段的点在其两侧
如果设横向的为ab线段,cd为另一条边,可以发现abc与abd的旋转方向不相同。
由此我们得到以下算法
double imem(node a,node b,node c){ return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);//判断旋转方向}
c在ab之间或者延长线上,此时上算法算出结果为0.所以我们需要加处理
bool if_in(node a,node b,node c){ return c.x>=min(a.x,b.x) && c.x<=max(a.x,b.x) && c.y>=min(a.y,b.y) && c.y<=max(a.y,b.y);}
这样判断出点是否在线上。
由此,获得了判断的结果
bool judge(node a,node b,node c,node d){ double no1,no2,no3,no4; no1=imem(c,d,a); no2=imem(c,d,b); no3=imem(a,b,c); no4=imem(a,b,d); if(no1*no2<0 &&no3*no4<0) return true; if(no1==0 && ifin(c,d,a)) return true; else if(no2==0 && ifin(c,d,b)) return true; else if(no3==0 && ifin(a,b,c)) return true; else if(no4==0 && ifin(a,b,d)) return true; return false;}
这个解决了,整个问题就很简单了,并查集的处理也很基础。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#define N 1005using namespace std;int G[N],num[N];struct node{ double x,y;};struct flow{ node node1,node2;}Flow[N];int findx(int x){ return G[x]==x?x:G[x]=findx(G[x]);}void set(int a,int b){ int p=findx(a),q=findx(b); if(p!=q) { G[p]=q; num[q]+=num[p]; }}double imem(node a,node b,node c){ return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);}bool ifin(node a,node b,node c){ return c.x>=min(a.x,b.x) && c.x<=max(a.x,b.x) && c.y>=min(a.y,b.y) && c.y<=max(a.y,b.y);}bool judge(node a,node b,node c,node d){ double no1,no2,no3,no4; no1=imem(c,d,a); no2=imem(c,d,b); no3=imem(a,b,c); no4=imem(a,b,d); if(no1*no2<0 &&no3*no4<0) return true; if(no1==0 && ifin(c,d,a)) return true; else if(no2==0 && ifin(c,d,b)) return true; else if(no3==0 && ifin(a,b,c)) return true; else if(no4==0 && ifin(a,b,d)) return true; return false;}int main(){ int T,n,i,j,a,number; char ch[10]; scanf("%d",&T); while(T--) { scanf("%d",&n); a=0; for(i=1;i<=n;i++) { G[i]=i; num[i]=1; } for(i=0;i<n;i++) { scanf("%s",&ch); if(ch[0]=='P') { a++; scanf("%lf%lf%lf%lf",&Flow[a].node1.x,&Flow[a].node1.y,&Flow[a].node2.x,&Flow[a].node2.y); for(int j=1;j<a;j++) { if(findx(j)!=findx(a) &&judge(Flow[j].node1,Flow[j].node2,Flow[a].node1,Flow[a].node2)) set(j,a); } } else if(ch[0]=='Q') { scanf("%d",&number); number = findx(number); printf("%d\n",num[number]); } } if(T) printf("\n"); } return 0;}
0 0
- hdu1558(并查集)
- hdu1558(并查集和简单几何的结合)
- hdu1558 Segment set (判断线段相交+并查集)
- hdu1558 几何处理 + 并查集
- hdu1558并查集+线段相交
- hdu1558--并查集+判断线段相交
- hdu1558线段相交与并查集
- HDU1558 Segment set(计算几何+并查集)
- Segment set(hdu1558并查集+计算机和)
- hdu1558 Segment set(线段是否有交点+并查集)
- HDU1558 - Segment set 并查集 + 判断线段相交
- 【计算几何初步-线段相交+并查集】【HDU1558】Segment set
- hdu1558
- 并查集(集并查)
- 并查集(含关系并查集)
- 【并查集】食物链(关系并查集)
- 【并查集】并查集详解(转)
- 分组并查集(种类并查集)
- Linux学习笔记 - 一些常見的裝置代號
- Qt自定义事件的实现
- WebService生成客户端和服务端
- JAVA 注解示例 详解
- Android RotateAnimation详解
- hdu1558(并查集)
- ios开发必备10款第三方类库
- iOS开发UI— QQ好友列表(处理头部控件的点击)viewForHeaderInSection:
- Qt 获取文件的名称和路径
- Django 1.4 升级 Django 1.6
- 【Aho-Corasick Automaton】
- Android编译中m、mm、mmm的区别
- QT截取屏幕
- linux下解压命令大全