题意:给出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. Redis批量查询删除KEYS

    对腾讯云的Redis集群不支持很多指令(config get * .flushdb.flushall.等相关指令) redis指令限制:https://www.qcloud.com/document/ ...

  2. vs2010 javascript代码拓展插件支持代码折叠

    参考地址

  3. Android API之android.provider.ContactsContract.RawContacts

    android.provider.ContactsContract.RawContacts Constants for the raw contacts table, which contains o ...

  4. C中字符串分割函数strtok的一个坑

    strtok的典型用法是: p = strtok(s4, split); while(p != NULL){ printf("%s\n", p); p = strtok(NULL, ...

  5. iOS10 完美降级 iOS9.3.2,保留全部数据

    本篇文章由:http://xinpure.com/downgrade-ios10-perfect-ios9-3-2-retention-of-all-data/ iOS 10 Beta版尝鲜 前段时间 ...

  6. Python websocket

    一.自己实现websocket 网上流传的都是Python2的websocket实现 # coding=utf8 # !/usr/bin/python import struct, socket im ...

  7. eclipse 垃圾回收器,内存释放

    http://zhangrong-0825-163-com.iteye.com/blog/7334071.如何在eclipse里修改web工程的访问路径,步骤如下: 点击web工程——>选择pr ...

  8. python学习笔记——信号模块signal

    基于python学习笔记——多进程间通信——Linux信号基础的学习基础,进一步学习Python标准库中的signal模块. 尽管signal是python中的模块,但是主要针对UNIX平台(比如Li ...

  9. Python学习笔记010——递归函数

    1 递归定义 函数直接或间接调用函数本身,则该函数称为递归函数 2 递归特点 Python函数递归调用,会用到栈 – 这里的栈是函数/程序运行时系统为其分配的一段内存区 – 栈具有 后进先出 的特性 ...

  10. Android 数据加密算法 Des,Base64详解

    一,DES加密: 首先网上搜索了一个DES加密算法工具类: import java.security.*;import javax.crypto.*; public class DesHelper { ...