/*
疾速优化+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存边的更多相关文章

  1. 【BZOJ】1054: [HAOI2008]移动玩具(bfs+hash)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1054 一开始我还以为要双向广搜....但是很水的数据,不需要了. 直接bfs+hash判重即可. # ...

  2. [BZOJ1054][HAOI2008]移动玩具 bfs+hash

    1054: [HAOI2008]移动玩具 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2432  Solved: 1355[Submit][Stat ...

  3. HDU-1043 Eight八数码 搜索问题(bfs+hash 打表 IDA* 等)

    题目链接 https://vjudge.net/problem/HDU-1043 经典的八数码问题,学过算法的老哥都会拿它练搜索 题意: 给出每行一组的数据,每组数据代表3*3的八数码表,要求程序复原 ...

  4. NOIP 模拟 玩积木 - 迭代加深搜索 / bfs+hash+玄学剪枝

    题目大意: 有一堆积木,0号节点每次可以和其上方,下方,左上,右下的其中一个交换,问至少需要多少次达到目标状态,若步数超过20,输出too difficult 目标状态: 0 1 1 2 2 2 3 ...

  5. poj 2046 Gap(bfs+hash)

    Description Let's play a card game called Gap. You have cards labeled with two-digit numbers. The fi ...

  6. 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 ...

  7. hdu.1067.Gap(bfs+hash)

    Gap Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  8. BFS+Hash(储存,判重) HDOJ 1067 Gap

    题目传送门 题意:一个图按照变成指定的图,问最少操作步数 分析:状态转移简单,主要是在图的存储以及判重问题,原来队列里装二维数组内存也可以,判重用神奇的hash技术 #include <bits ...

  9. 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 ...

随机推荐

  1. Eclipse 调试maven test

    在eclipse中调试maven test 一般情况下,使用如下方式都不能使myeclipse检测到程序中的断点: 项目 -> Run As -> maven test 或 项目 -> ...

  2. IE下不支持option的onclick事件

    <select> <option onclick="test('www.hao123.com')"value="www.hao123.com" ...

  3. 网站如何做到完全不需要jQuery

    jQuery是现在最流行的JavaScript工具库. 据统计,目前全世界57.3%的网站使用它.也就是说,10个网站里面,有6个使用jQuery.如果只考察使用工具库的网站,这个比例就会上升到惊人的 ...

  4. 6个好用的Web开发工具

    在过去的几年间,涌现出了很多Web开发工具,它们大多还是比较吸引人的,方便了我们的工作.我们可以学习一下这些新东西,短时间就可以拓宽思路(PHP100推荐:学习10分钟,改变你的程序员生涯).这些应用 ...

  5. DB2 SQL 递归实现多行合并

    最终效果 原始数据: 转换脚本: WITH post_a AS ( SELECT DISTINCT T.EMP_NO,S.CODE_ FROM inscndb.DTFMA000_EMP_POST T ...

  6. wampserver安装后的基本配置

    wampserver安装后的基本配置 1.WampServer的安装 下载好安装包后,你能在保存其文件夹中找到这样一个图标: 双击它,会弹出如下提示 提示信息:不要试图从Wamp5 1.x(x代表任意 ...

  7. 【非原】c语言之声明和定义的区别

    原创地址:http://www.cnblogs.com/haore147/p/3647466.html 什么是定义?什么是声明?它们有何区别? 举个例子: 1 2 A)int i; B)extern  ...

  8. web sevice 生成代理类及使用

    一.生成代理类: VS2008下这样写 wsdl.exe /l:cs /out:D:/ProxyServices.cs http://localhost/WebService.asmx VS2010下 ...

  9. 新的MOVE结构,和在项目中实际的感受

    关于MVC/MVP的瑕疵 MVC 和 MVP是最简单,最脍炙人口的框架结构. 有一段时间, 凡事有一定规模的代码,我都会架在上面,甚至后台程序也不例外(预留出可以注册的用户交互接口,作为后台控制器). ...

  10. 开源YYKit-b

    转自 ibireme的博客,大家可以去他博客看看,有很多开发过程的一些调研和评测. YYModel 类似 Mantle/JSONModel 的工具,性能比 Mantle 高一个数量级,有更好的容错性, ...