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 ...
随机推荐
- 关于document.write
document.write的用处 document.write是JavaScript中对document.open所开启的文档流(document stream操作的API方法,它能够直接在文档流中 ...
- hdu 1963 Investment 多重背包
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1963 //多重背包 #include <cstdio> #include <cstr ...
- Mybatis-Generator插件自动生成Dao、Model、Mapping相关文件
最近做项目,mapping 有点多而且容易写错,于是试着用了Mybatis-Generator 插件自动生成 dao, domain mapping 文件.感觉还挺好用.把相关配置分享,一边以后做项 ...
- apache commons-email1.3使用
apache commons-email1.3下载地址: https://repository.apache.org/content/repositories/orgapachecommons-0 ...
- 同一个tomcat多个web应用共享session
tomcat版本:apache-tomcat-6.0.29(次方tomcat6和tomcat7支持) 1.修改D:\apache-tomcat-6.0.29\conf\server.xml文件 ...
- Linux多线程编程(不限Linux)
前言 线程?为什么有了进程还需要线程呢,他们有什么区别?使用线程有什么优势呢?还有多线程编程的一些细节问题,如线程之间怎样同步.互斥,这些东西将在本文中介绍.我在某QQ群里见到这样一道面试题: 是否熟 ...
- 在Android项目中调用已有.so库
注意该.so库指的是android平台的,非一般linux.unix平台:1.现有库libcom_ycan_testLib.so2.新建android项目TestLib23.添加新类:类名:testL ...
- MFC控件随窗口大小变化原理及实现
本文主要针对MFC的dialog,实现控件随窗口大小变化. 原理:首先获取dialog的初始大小,当窗口发送变动时,调用OnSize事件和方法,计算缩放比例,然后对界面中的所有控件进行缩放和布局. 实 ...
- struts2中constant参数设置
序号 方法 说明 1 <constant name="struts.i18n.encoding" value="UTF-8"/> 指定web应用默认 ...
- 让backspace键默认为删除键
在/root/.bashrc 中插入一条: stty erase ^H