题意:给出n个点,以及m条边,这些边代表着这些点相连,修一个电力站,若在某一点修一个站,那么与这个点相连的点都可以通电,问所有的点都通电的话至少要修多少个电力站........

思路:最多给出的是35个点,那么若是搜索的话,就是2^35......考虑状态压缩剪枝,若某个点修电力站,那么周围的所有点都有电了....

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
typedef long long ss;
ss sa[40],t[40],p=1;
int dfs(ss n,ss step,ss ans,ss ks,ss m)
{
if(ans==((p<<n)-1))
{ return 1;
}
//printf("%I64d\n",ans);
if(step==m)
return 0;
if(ks>n)
return 0;
for(int i=ks;i<=n;i++)
{
//printf("%I64d %I64d\n",ans|t[i],((p<<n)-1));
if((ans|t[i])!=((p<<n)-1))
break; if((ans|sa[i])==ans)
continue; if(dfs(n,step+1,ans|sa[i],i+1,m))
return 1;
}
return 0;
}
int main()
{
ss n,q;
//scanf("%I64d",&n);
//scanf("%I64d",&q);
//printf("%I64d %I64d\n",n,q);
//printf("")
while(scanf("%lld",&n)>0)
{
scanf("%lld",&q);
if(n==0&&q==0)
break;
for(ss i=1;i<=n;i++)
{
sa[i]=p<<(i-1);
} for(ss i=1;i<=q;i++)
{
ss tmp,tmp1;
scanf("%lld %lld",&tmp,&tmp1);
sa[tmp]|=(p<<(tmp1-1));
sa[tmp1]|=(p<<(tmp-1));
}
t[n]=sa[n];
for(ss i=n-1;i>0;i--)
{
t[i]=sa[i];
t[i]|=t[i+1];
}
for(ss i=1;i<=n;i++)
{
if(dfs(n,0,0,1,i))
{
printf("%lld\n",i);
break;
}
}
//printf("1111\n");
}
return 0;
}

uva10160(dfs+状态压缩)的更多相关文章

  1. 2101 可达性统计(拓扑排序/dfs+状态压缩)

    [题目描述] 给定一张N个点M条边的有向无环图,分别统计从每个点出发能够到达的点的数量.N,M≤30000. [题目链接] 2101 可达性统计 [算法] 拓扑排序之后逆序计算(感觉dfs更好写而且应 ...

  2. HDU 4921 Map DFS+状态压缩+乘法计数

    算最多十条链,能截取某前缀段,每种方案都可以算出一个权值,每种方案的概率都是总数分之一,问最后能构成的所有可能方案数. 对计数原理不太敏感,知道是DFS先把链求出来,但是想怎么统计方案的时候想了好久, ...

  3. POJ2743Mobile Computing[DFS 状态压缩]

    Mobile Computing Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 666   Accepted: 224   ...

  4. POJ 1632 Vase collection【状态压缩+搜索】

    题目传送门:http://poj.org/problem?id=1632 Vase collection Time Limit: 1000MS   Memory Limit: 10000K Total ...

  5. codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)

    B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...

  6. 最大联通子数组之和(dfs,记忆化搜索,状态压缩)

    最大联通子数组,这次的题目,我采用的方法为dfs搜索,按照已经取到的数v[][],来进行搜索过程的状态转移,每次对v[][]中标记为1的所有元素依次取其相邻的未被标记为1的元素,将其标记为1,然而,这 ...

  7. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

  8. UVA 1508 - Equipment 状态压缩 枚举子集 dfs

    UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...

  9. hihocoder 1334 - Word Construction - [hiho一下第170周][状态压缩+DFS]

    题目链接:https://hihocoder.com/problemset/problem/1334 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given N wo ...

随机推荐

  1. 〖Linux〗打开qtcreater出现错误的解决方法

    1. 更换了显卡驱动,发现打开qtcreater时出现了以下的错误: qtcreator: error : cannot open shared object file: No such file o ...

  2. ORACLE经常使用命令

    1 怎样查看数据库中都有什么表? select * from sys.user_tables; 2 怎样链接数据库 sqlplus /nolog;conn sys/oracle as sysdba; ...

  3. Dell笔记本Ubuntu无线网卡驱动安装

    [日期:2010-06-09]   经过长时间学习Ubuntu无线网卡,,你可能会遇到Ubuntu无线网卡问题,,这里将介绍Ubuntu无线网卡问题的解决方法整了一晚上,终于在我的dell 1440上 ...

  4. HDUOJ----1170Milk

    Milk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  5. 【LeetCode】42. Trapping Rain Water

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

  6. CodeForces 445B. DZY Loves Chemistry(并查集)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://codeforces.com/problemset/prob ...

  7. wavwrite注意事项

    前几天群里有人提出一个问题:MATLAB里,同样频率的信号写入/读取,为什么频率感觉不同? 测试code: fs = 2000; f0 = 20; t = 0:1/fs:1; subplot 211 ...

  8. JAVA class 编译jar。 控制台使用jar

    //编译jar jar -cvf  -mgtvEncode.jar -mgtvEncode.class //使用jar java -cp mgtvEncode.jar mgtvEncode

  9. Synplify9.6.2破解(转帖)

    Synplify9.6.2破解(转帖)   转载自:http://www.cnblogs.com/mark-sun/archive/2012/02/26/2368773.html Abstract本文 ...

  10. tomcat在conf/Catalina/localhost目录下配置项目路径

    转自:http://wangyl93-dl-cn.iteye.com/blog/1508517 在tomcat的conf/Catalina/localhost目录下配置项目路径,tomcat启动是会直 ...