uva 10160
一开始写的代码加上各种剪枝后还是超时, 然后看了一下状态压缩后过了,两个代码的具体思想是一样的,状态压缩后可以大大提升性能
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <string>
#include <cstring>
#include <algorithm>
#include <iostream>
#define maxn 200010
#define INF 0x7fffffff
#define inf 10000000
#define ull unsigned long long
#define ll long long
using namespace std; ll st[40], vis[40];
int n, m; bool dfs(ll state, int ans, int aim, int k)
{
if(ans == aim)
{
if(state == ((ll)1 << n)-1) return true;
return false;
}
for(int i = k; i <= n; ++ i)
{
if((state|vis[i]) != ((ll)1 << n)-1) return false;
if((state|st[i]) == state) continue;
if(dfs(state|st[i], ans+1, aim, i+1)) return true;
}
return false;
}
void init()
{
memset(vis, 0, sizeof(vis));
for(int i = 0; i < n; ++ i)
st[i+1] = ((ll)1 << i);
}
int main()
{
while(scanf("%d%d", &n, &m) == 2 && n+m)
{
init();
for(int i = 0; i < m ; ++ i)
{
int a, b;
scanf("%d%d", &a, &b);
st[a] |= ((ll)1 << (b-1));
st[b] |= ((ll)1 << (a-1));
}
vis[n] = st[n];
for(int i = n-1; i > 0; -- i)
vis[i] = vis[i+1]|st[i];
for(int i = 1; i <= n; ++i)
{
if(dfs((ll)0, 0, i, 1))
{
printf("%d\n", i);
break;
}
}
// puts("*****************");
}
return 0;
}
\*time limit*\
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <string>
#include <cstring>
#include <algorithm>
#include <iostream>
#define maxn 200010
#define INF 0x7fffffff
#define inf 10000000
#define ull unsigned long long
#define ll long long
using namespace std; vector<int> g[40];
int vis[40];
bool mm[40][40];
int n, m;
bool checkall()
{
for(int i = 1; i <= n; ++ i)
if(!vis[i]) return false;
return true;
}
bool checkii(int now)
{
for(int i = now+1; i <= n; ++ i)
if(mm[i][now]) return false;
return true;
}
bool checknow(int now)
{
if(!vis[now]) return true;
for(int i = 0; i < (int)g[now].size(); ++ i)
if(!vis[g[now][i]]) return true;
return false;
}
void ok(int now)
{
vis[now]++;
for(int i = 0; i < (int)g[now].size(); ++ i)
vis[g[now][i]]++;
}
void Nok(int now)
{
vis[now]--;
for(int i = 0; i < (int)g[now].size(); ++ i)
vis[g[now][i]]--;
}
bool dfs(int ans, int aim, int k)
{
if(ans == aim)
{
if(checkall()) return true;
return false;
}
for(int i = k; i <= n; ++ i)
{
if(checknow(i))
{
ok(i);
if(dfs(ans+1, aim, i+1)) return true;
Nok(i);
}
if(!vis[i] && checkii(i)) return false;
}
return false;
}
void init()
{
memset(vis, 0, sizeof(vis));
memset(mm, 0, sizeof(mm));
for(int i = 1; i <= n; ++ i) g[i].clear();
}
int main()
{
while(scanf("%d%d", &n, &m) == 2 && n+m)
{
init();
for(int i = 0; i < m ; ++ i)
{
int a, b;
scanf("%d%d", &a, &b);
g[a].push_back(b);
g[b].push_back(a);
mm[a][b] = mm[b][a] = true;
}
for(int i = 1; i <= n; ++i)
{
if(dfs(0, i, 1))
{
printf("%d\n", i);
break;
}
}
}
return 0;
}
uva 10160的更多相关文章
- UVA 10160 Servicing Stations(深搜 + 剪枝)
Problem D: Servicing stations A company offers personal computers for sale in N towns (3 <= N < ...
- uva 10160 Servicing Stations(DFS+剪枝)
Servicing stations A company offers personal computers for sale in N towns (3 <= N <= 35). The ...
- UVA 10160 Servicing Stations(状态压缩+迭代加深)
[题目链接] LInk [题目大意] 给出一些点和边,选择一个点就能把这个点和相邻的点都覆盖,求最小点覆盖 [题解] 我们压缩点被覆盖的状态,迭代加深搜索覆盖的最小点数, 当剩余的点全部选上时都无法完 ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- UVA&&POJ离散概率与数学期望入门练习[4]
POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...
- UVA计数方法练习[3]
UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...
- UVA数学入门训练Round1[6]
UVA - 11388 GCD LCM 题意:输入g和l,找到a和b,gcd(a,b)=g,lacm(a,b)=l,a<b且a最小 g不能整除l时无解,否则一定g,l最小 #include &l ...
随机推荐
- JVM调优总结 -Xms -Xmx -Xmn -Xss(转载)
堆大小设置JVM 中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理内存限制.32位系统下,一般限制在1.5G~2G:64为操作 ...
- ADO.NET基本操作(CRUD、Procedure、Transaction)
模型沿用上篇博客所提到的学生.教师.课程,以详细的代码进行演示. 增删改查 添加学生.教师.课程 using System.Data.SqlClient; namespace Test { class ...
- php获取系统信息的方法
php获取系统信息的方法. 用 getenv函数进行处理: <?php $root = getenv('DOCUMENT_ROOT'); ////服务器文档根目录 $port = getenv( ...
- Waring:This LinearLayout layout or its FrameLayout parent is useless; transfer the background attribute to the other view
解决方法请参考: You have a single component (row) vertical linear layout, containing another linear layout. ...
- 为checkboxSelectionModel赋值
store.on('load', function(store, records, options) { sm.clearSelections(); //清空数据 Ext.each(records ...
- Spark菜鸟学习营Day4 单元测试程序的编写
Spark菜鸟学习营Day4 单元测试程序的编写 Spark相比于传统代码是比较难以调试的,单元测试的编写是非常必要的. Step0:需求分析 在测试案例编写前,需完成需求分析工作,明确程序所有的输入 ...
- Docs list
http://www.deansys.com/doc/ldd3/index.html Github中文文档: http://www.worldhello.net/gotgithub/03-projec ...
- 一,XAML基础
RuntimeNameProperty特性:为什么<Grid x:Name="grid1"></Grid>等价于<Grid Name="gr ...
- Ubuntu中设置环境变量详解
1, 为单一用户:.bashrc: 为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取.打开用户主目录下的.bashrc,在这个文件中加入export PA ...
- Linux下Hadoop的简单安装
Hadoop 的安装极为简单,一共只有三步: 安装JDK 安装Hadoop 配置Hadoop 1,安装JDK 下载JDK,ftp传到linux或者linux中下载 切换 ...