【HDU 5305】Friends 多校第二场(双向DFS)
来源:互联网 发布:如何复制淘宝上的图片 编辑:程序博客网 时间:2024/06/05 05:37
根据题意的话最多32条边,直接暴力的话 2 ^ 32肯定超时了,我们可以分两次搜索时间复杂度减少为 2 * 2 ^ 16
唯一需要注意的就是对目前状态的哈希处理,
我采用的是 十进制表示法
跑的还是比较快的,可能是用STL函数的原因增加了一些常数复杂度。
#include<map>#include<vector>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;typedef pair<int,int> pill;const int maxn = 55;struct Edge{ int a,b;}edge[maxn];int n,m;LL cnt,base[15];int _count[15];map<pill,int>vis;void init(){ base[1] = 1; for(int i = 2; i < 10; i++) base[i] = base[i - 1] * 10;}void calc(int state1,int state2,int &a,int &b){ int base = 1; for(int i = 1; i <= n; i++){ int d1 = state1 % 10, d2 = state2 % 10; int v1 = _count[i] - d1, v2 = _count[i] - d2; a += base * v1; b += base * v2; base *= 10; state1 /= 10; state2 /= 10; }}void dfs1(int now,int finish,int state1,int state2){ if(now == finish){ vis[make_pair(state1,state2)] ++; return; } int a = edge[now].a, b = edge[now].b; dfs1(now + 1,finish,state1 + base[a] + base[b],state2); dfs1(now + 1,finish,state1,state2 + base[a] + base[b]);}void dfs2(int now,int finish,int state1,int state2){ if(now == finish){ int x = 0,y = 0; calc(state1,state2,x,y); if(x >= 0 && y >= 0) cnt += vis[make_pair(x,y)]; return; } int a = edge[now].a, b = edge[now].b; dfs2(now + 1,finish,state1 + base[a] + base[b],state2); dfs2(now + 1,finish,state1,state2 + base[a] + base[b]);}int main(){ int T; init(); scanf("%d",&T); while(T--){ vis.clear(); memset(_count,0,sizeof(_count)); scanf("%d%d",&n,&m); for(int i = 0; i < m; i++){ scanf("%d%d",&edge[i].a,&edge[i].b); _count[edge[i].a] ++; _count[edge[i].b] ++; } int ok = 1; for(int i = 1; i <= n; i++){ if(_count[i] % 2 != 0){ ok = 0; break; } _count[i] /= 2; } cnt = 0; if(ok){ dfs1(0,m / 2,0,0); dfs2(m / 2,m,0,0); } printf("%d\n",cnt); } return 0;}
0 0
- 【HDU 5305】Friends 多校第二场(双向DFS)
- HDU 5305 Friends(2015多校第二场 dfs + 剪枝)
- 多校第二场 1006 hdu 5305 Friends(爆搜)
- [DFS] 多校联合第二场 F题 Friends
- hdu 5305 Friends(2015多校第二场第6题)记忆化搜索
- HDU 5305 Friends (搜索+剪枝) 2015多校联合第二场
- HDU 5305 Friends(dfs)
- HDU 5305 Friends(dfs)
- hdu 5305 Friends(dfs)
- 2015多校联合第二场 5305 friends 深搜
- HDU 5305 Friends (DFS)
- HDU 5305 Friends (DFS)
- hdu 5305 Friends (dfs)
- HDU 5305 Friends DFS
- hdu 5305 Friends dfs
- HDU 5305 Friends(DFS)
- HDU 5305 Friends(dfs)
- HDU 5305 Friends(简单DFS)
- Android控件开发之TextView
- Android开发EditText属性详解
- Sklearn导入数据集20newsgroups报错-no handlers could be fetch_20newsgroups
- 苹果自带的APP下载分析统计工具
- 常用Unity3d方法及接口
- 【HDU 5305】Friends 多校第二场(双向DFS)
- Android控件开发之EditView
- Windows下Python包和模块的安装方法
- 2.16-switch
- Unity 之 C# 利用回调函数实现C++匿名函数
- 课堂笔记03
- Android控件开发之CheckBox
- linux input子系统(一)
- hdu 4251 划分树入门题