sgu174 walls 并查集 + 哈希
来源:互联网 发布:如何查询电脑mac地址 编辑:程序博客网 时间:2024/05/16 05:43
提议:给定n个墙(线段) 求第一个出现封闭空间的墙编号
解析:可以看成无向图上的点的关系,当一个线段连接时如果出现环时候退出,用并查集维护连通性信息,点的映射用hash来做
#include<iostream>#include<cstring>#include<cstdio> #include<algorithm>#include<map>#define MAXN 400010using namespace std;struct point{ int x,y;}; bool operator <(const point &x,const point &y) { if(x.x!=y.x)return x.x<y.x; return x.y<y.y; }struct segment{ point a,b;}a[MAXN];int n,p[MAXN];map<point,int>hash;int findset(int &x){ if(p[x]!=x) p[x]=findset(p[x]); return p[x];}void solve(){ for(int i=0;i<2*n;i++) p[i]=i; int cnt=0; for(int i=1,u,v;i<=n;i++) { if(hash.count(a[i].a)) u=hash[a[i].a]; else u=hash[a[i].a]=cnt++; if(hash.count(a[i].b)) v=hash[a[i].b]; else v=hash[a[i].b]=cnt++; u=findset(u),v=findset(v); if(u==v) { printf("%d\n",i); return; } p[u]=v; } printf("0\n");}int main(){ while(scanf("%d",&n)!=EOF) { for(int i=1;i<=n;i++) scanf("%d%d%d%d",&a[i].a.x,&a[i].a.y,&a[i].b.x,&a[i].b.y); solve(); } return 0;}
- sgu174 walls 并查集 + 哈希
- sgu174:Walls(并查集判无向图环)
- sgu 174 Walls (并查集)
- sgu 174 Walls hash+并查集
- sgu 174 Walls 并查集+hash
- sgu174
- HDU3938 并查集 并查集
- 并查集(集并查)
- HDU1232 并查集<并>
- SDUT (并查集+哈希)
- 并查集
- 数据结构-并查集
- 并查集
- 并查集!
- 并查集
- 并查集
- 并查集
- 并查集
- Uart串口实验问题
- SVM入门(二)线性分类器Part 1
- LogMiner
- VB.Net程序设计:Dictionary和Collection搭配使用
- ubuntu 11.10 下面如何进入单用户模式
- sgu174 walls 并查集 + 哈希
- Android中万能的BaseAdapter的使用 .
- IE6下实现position:fixed
- C++实现水波纹、火焰和血浆效果
- php导出中文内容excel文件类
- 大黑山与冒险
- ArrayList、Vector、LinkedList的区别及其优缺点?
- ubuntu环境下反编译android apk
- 天龙座流星雨