题意:给出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. JS获取浏览器高宽度,屏幕分辨率和一些定位空隙等

    IE中: document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 document.d ...

  2. python 版websocket实现

    ubuntu下python2.76 windows python 2.79, chrome37 firefox35通过 代码是在别人(cddn有人提问)基础上改的, 主要改动了parsedata和se ...

  3. Android API之java.lang.String

      boolean java.lang.String.contains(CharSequence cs) String字符串是否包含CharSequence(cs).

  4. Nginx中Laravel的配置

    server { listen 80; server_name sub.domain.com; set $root_path '/var/www/html/application_name/publi ...

  5. How to forcefully delete a daemonset or a pod in kubernetes cluster

    I have setup a kubernetes cluster which is working fine. I created deployment with type as daemonset ...

  6. ios block常见的错误(一)

    Block是OC中的一种数据类型,在iOS开发中被广泛使用. 在实际的使用和面试过程中,经常发现同事或面试者对block了解得不够透彻,本博文将列举常见block错误来对其加深理解. 示例代码1: ; ...

  7. <转>C++ explicit关键字详解

    要文转自:http://www.cnblogs.com/ymy124/p/3632634.html 首先, C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用是表明该构造 ...

  8. 重写 View 的 Touch 方法,实现一个酷炫的九宫格图片

    前几天翻看代码库,发现一个之前写过的一个有意思的小玩意,共享给大家

  9. 【js】右下角浮动窗口

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. PO_本地一揽子采购协议(流程)

    2014-06-04 Created By BaoXinjian