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 ...
随机推荐
- hdu 1879 继续畅通工程
/************************************************************************/ /* hdu 1879 继续畅通工程 Time L ...
- java web 实现验证码
验证码的作用:通常的登录或者注册系统时,都会要求用户输入验证码,以此区别用户行为和计算机程序行为,目的是有人防止恶意注册.暴力破解密码等. 实现验证码的思路:用 server 实现随机生成数字和字母组 ...
- AngularJS(13)-包含
AngularJS 包含 使用 AngularJS, 你可以使用 ng-include 指令来包含 HTML 内容: 实例 <body> <div class="conta ...
- java 语法糖
package syntax.autoCase; import java.util.Arrays; import java.util.List; public class autoCase { pub ...
- Delphi 二次开发 CorelDRAW
我们首先通过一个简单的程序例子来看一下Delphi对CorelDRAW二次开发有什么样的效果.本程序实现CorelDRAW程序的打开和关闭. 1.1 Delphi与CorelDRAW连接 在程序与Co ...
- Mysql主从同步(复制)
目录: mysql主从同步定义 主从同步机制 配置主从同步 配置主服务器 配置从服务器 使用主从同步来备份 使用mysqldump来备份 备份原始文件 ...
- SQLdiag Utility
使用SQLdiag 会进行信息搜集类型 Windows 系统性能日志 Windows 系统日志 SQL Server 性能信息 SQL Server 阻塞信息 SQL Server 配置信息 如何使用 ...
- Codeforces 559A 第六周 O题
Description Gerald got a very curious hexagon for his birthday. The boy found out that all the angle ...
- ORA-00845
系统版本: [root@yoon ~]# more /etc/oracle-releaseOracle Linux Server release 5.7 数据库版本: Oracle Database ...
- vs2010的11个调试技巧和方法
调试是软件开发周期中很重要的一部分.它具有挑战性,同时也很让人疑惑和烦恼.总的来说,对于稍大一点的程序,调试是不可避免的.最近几年,调试工具的发展让很多调试任务变的越来越简单和省时. 这篇文章总结了可 ...