POJ3697+BFS+hash存边
/*
疾速优化+hash存边
题意:给定一个包含N(1 ≤ N ≤ 10,000)个顶点的无向完全图,图中的顶点从1到N依次标号。从这个图中去掉M(0 ≤ M ≤ 1,000,000)条边,求最后与顶点1联通的顶点的数目
思路(BFS):从顶点1开始不断扩展,广度优先搜索所有的与当前扩展点联通的顶点。开始每次都要判断所有的顶点是否与cur相连,
若相连则push,反之跳过。
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<math.h>
using namespace std;
typedef long long int64;
//typedef __int64 int64;
typedef pair<int64,int64> PII;
#define MP(a,b) make_pair((a),(b))
const int maxn = ;
const int maxm = ;
const int smod = ;
const int mod = ;
const int inf = 0x7fffffff;
const double pi=acos(-1.0);
const double eps = 1e-; struct Edge{
int u,v,next;
}edge[ maxm<< ];
int cnt,myhash[ maxm<< ];
queue<int>q;
int vis[ maxn ]; void init(){
cnt = ;
while( !q.empty() )
q.pop();
memset( myhash,-,sizeof( myhash ) );
} void addedge( int a,int b ){
//if( a>b ) swap( a,b );
int tt = (a*smod+b)%mod;
edge[ cnt ].u = a;
edge[ cnt ].v = b;
edge[ cnt ].next = myhash[ tt ];
myhash[ tt ] = cnt++; tt = (b*smod+a)%mod;
edge[ cnt ].u = a;
edge[ cnt ].v = b;
edge[ cnt ].next = myhash[ tt ];
myhash[ tt ] = cnt++; } bool find( int u,int v ){
//if( u>v ) swap( u,v );
int tt = ( u*smod+v )%mod;
for( int i=myhash[tt];i!=-;i=edge[i].next ){
if( edge[ i ].u == u && edge[ i ].v == v ){
return true;
}
}
return false;
} int main(){
int n,m;
int Case = ;
while( scanf("%d%d",&n,&m)==,n+m ){
init();
int x,y;
while( m-- ){
scanf("%d%d",&x,&y);
addedge( x,y );
addedge( y,x );
}
int ans = ;
cnt = ;
q.push( );
for( int i=;i<=n;i++ ){
vis[ cnt++ ] = i;
}
while( !q.empty() ){
int cur = q.front();
q.pop();
int tmp_cnt = ;
for( int i=;i<cnt;i++ ){
if( !find( cur,vis[i] ) ){
ans ++;
q.push( vis[i] );
}
else vis[ tmp_cnt++ ] = vis[i];
}
cnt = tmp_cnt;
}
printf("Case %d: %d\n",Case++,ans);
}
return ;
}
POJ3697+BFS+hash存边的更多相关文章
- 【BZOJ】1054: [HAOI2008]移动玩具(bfs+hash)
http://www.lydsy.com/JudgeOnline/problem.php?id=1054 一开始我还以为要双向广搜....但是很水的数据,不需要了. 直接bfs+hash判重即可. # ...
- [BZOJ1054][HAOI2008]移动玩具 bfs+hash
1054: [HAOI2008]移动玩具 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2432 Solved: 1355[Submit][Stat ...
- HDU-1043 Eight八数码 搜索问题(bfs+hash 打表 IDA* 等)
题目链接 https://vjudge.net/problem/HDU-1043 经典的八数码问题,学过算法的老哥都会拿它练搜索 题意: 给出每行一组的数据,每组数据代表3*3的八数码表,要求程序复原 ...
- NOIP 模拟 玩积木 - 迭代加深搜索 / bfs+hash+玄学剪枝
题目大意: 有一堆积木,0号节点每次可以和其上方,下方,左上,右下的其中一个交换,问至少需要多少次达到目标状态,若步数超过20,输出too difficult 目标状态: 0 1 1 2 2 2 3 ...
- poj 2046 Gap(bfs+hash)
Description Let's play a card game called Gap. You have cards labeled with two-digit numbers. The fi ...
- UVA 10798 - Be wary of Roses (bfs+hash)
10798 - Be wary of Roses You've always been proud of your prize rose garden. However, some jealous f ...
- hdu.1067.Gap(bfs+hash)
Gap Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- BFS+Hash(储存,判重) HDOJ 1067 Gap
题目传送门 题意:一个图按照变成指定的图,问最少操作步数 分析:状态转移简单,主要是在图的存储以及判重问题,原来队列里装二维数组内存也可以,判重用神奇的hash技术 #include <bits ...
- poj 2697 A Board Game(bfs+hash)
Description Dao was a simple two-player board game designed by Jeff Pickering and Ben van Buskirk at ...
随机推荐
- Page 的生命周期学习小结(翻译兼笔记)
初始化(Initialization) 页面被请求时,第一个被执行的总是下面接着执行的是 接着是 然后是 恢复和加载(Restore and Load) 接下来的 ViewState 被取回后,接着 ...
- jqGrid API 相关
取消所有选中的行: $("jqgridtableid").trigger("reloadGrid"): 设定选中行,可设定多行选中: $("jqgri ...
- 判断IFeatureClass图形是否含有Z值信息,若有为IPoint赋Z值
判断IFeatureClass图形是否含有Z值信息 IFeatureClass featureClass = this.pLayer.FeatureClass; string shapeFieldNa ...
- How to: Create Your Own Test Certificate (.pfx)
Original MSDN Link: https://msdn.microsoft.com/en-us/library/ff699202.aspx
- C++与Lua交互(一)
引言 之前做手游项目时,客户端用lua做脚本,基本所有游戏逻辑都用它完成,玩起来有点不爽,感觉"太重"了.而我又比较偏服务端这边(仅有C++),所以热情不高.最近,加入了一个端游项 ...
- C++ VARIANT 学习小记录
一:为什么会有这个? 目前,计算机语言有很多(大哥,为什么不能就那么一样呢?),如C++.Java,此外还有JavaScript.VBScript等脚本语言,它们自立门派,各自维护自己的数据类型. C ...
- java学习笔记_GUI(5)
demo如何为不同的button创建对应的响应函数 import javax.swing.*; import java.awt.event.*; import java.awt.*; class My ...
- Linux防火墙基本知识
一.防火墙的分类 (一).包过滤防火墙. 数据包过滤(packet Filtering)技术是在网络层对数据包进行选择,选择的依据是系统内设置的过滤逻辑,称为访问控制表(access control ...
- IAR ERROR --- [Li006]
今天移植代码时遇到一个比较奇葩的问题,记录如下: Error[Li006]: duplicate definitions for "Uart3"; in "E:\IAR_ ...
- 【C#】添加鼠标管轮事件
对FlowLayoutPanel添加鼠标滚轮事件 在mainform中添加事件 his.flowLayoutPanel1.MouseWheel += new System.Windows.Forms. ...