HDU 5765 Bonds(状压DP)
【题目链接】 http://acm.hdu.edu.cn/showproblem.php?pid=5765
【题目大意】
给出一张图,求每条边在所有边割集中出现的次数。
【题解】
利用状压DP,计算不同的连通块,对于每条边,求出两边的联通块的划分方案数,就是对于该点的答案。
【代码】
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int n,m,T,Cas=1,x[400],y[400],mp[(1<<20)+5],ans,sta[21];
bool g[21][21],dp[(1<<21)+5];
int main(){
scanf("%d",&T);
memset(mp,-1,sizeof(mp));
for(int i=0;i<20;i++)mp[1<<i]=i;
while(T--){
scanf("%d%d",&n,&m);
memset(g,0,sizeof(g));
memset(sta,0,sizeof(sta));
for(int i=1;i<=m;i++){
scanf("%d%d",&x[i],&y[i]);
if(x[i]<y[i])swap(x[i],y[i]);
g[x[i]][y[i]]=g[y[i]][x[i]]=1;
sta[x[i]]|=1<<y[i];
sta[y[i]]|=1<<x[i];
}dp[0]=1;
for(int s=1;s<(1<<n);s++){
dp[s]=0;
if(s==(s&-s)){dp[s]=1;continue;}
for(int x=0;x<n;x++){
if(s&(1<<x)){if(dp[s^(1<<x)]&&(sta[x]&s)){dp[s]=1;break;}}
}
}printf("Case #%d:",Cas++);
for(int i=1;i<=m;i++){
ans=0;
for(int s1=0;s1<(1<<(n-x[i]-1));s1++)
for(int s2=0;s2<(1<<(x[i]-y[i]-1));s2++)
for(int s3=0;s3<(1<<y[i]);s3++){
int s=s3|(s2<<(y[i]+1))|(1<<x[i])|(s1<<(x[i]+1));
if(dp[s]&&dp[((1<<n)-1)^s])ans++;
}printf(" %d",ans);
}puts("");
}return 0;
}
HDU 5765 Bonds(状压DP)的更多相关文章
- HDU 4284Travel(状压DP)
HDU 4284 Travel 有N个城市,M条边和H个这个人(PP)必须要去的城市,在每个城市里他都必须要“打工”,打工需要花费Di,可以挣到Ci,每条边有一个花费,现在求PP可不可以从起点1 ...
- HDU 4336 容斥原理 || 状压DP
状压DP :F(S)=Sum*F(S)+p(x1)*F(S^(1<<x1))+p(x2)*F(S^(1<<x2))...+1; F(S)表示取状态为S的牌的期望次数,Sum表示 ...
- HDU 3001 Travelling ——状压DP
[题目分析] 赤裸裸的状压DP. 每个点可以经过两次,问经过所有点的最短路径. 然后写了一发四进制(真是好写) 然后就MLE了. 懒得写hash了. 改成三进制,顺利A掉,时间垫底. [代码] #in ...
- HDU - 5117 Fluorescent(状压dp+思维)
原题链接 题意 有N个灯和M个开关,每个开关控制着一些灯,如果按下某个开关,就会让对应的灯切换状态:问在每个开关按下与否的一共2^m情况下,每种状态下亮灯的个数的立方的和. 思路1.首先注意到N< ...
- hdu 4114(状压dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4114 思路:首先是floyd预处理出任意两点之间的最短距离.dp[state1][state2][u] ...
- HDU 3091 - Necklace - [状压DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3091 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- HDU 3811 Permutation 状压dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3811 Permutation Time Limit: 6000/3000 MS (Java/Othe ...
- HDU 5838 (状压DP+容斥)
Problem Mountain 题目大意 给定一张n*m的地图,由 . 和 X 组成.要求给每个点一个1~n*m的数字(每个点不同),使得编号为X的点小于其周围的点,编号为.的点至少大于一个其周围的 ...
- hdu 4628 Pieces 状压dp
题目链接 枚举所有状态, 1表示这个字符还在原来的串中, 0表示已经取出来了. 代码中j = (j+1)|i的用处是枚举所有包含i状态的状态. #include <iostream> #i ...
- HDU 5045 Contest(状压DP)
Problem Description In the ACM International Collegiate Programming Contest, each team consist of th ...
随机推荐
- Linux学习netstat
netstat命令用于显示与IP.TCP.UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况.netstat是在内核中访问网络及相关信息的程序,它能提供TCP连接,TCP和UDP ...
- java 执行linux命令
原文地址: http://blog.csdn.net/xh16319/article/details/17302947 package scut.cs.cwh; import java.io.Inpu ...
- 偷懒的inline-block解决方法
最近这几天看了inline-block的用法,用它来代替浮动实现水平排列很不错,只是要解决一下浏览器兼容和间隙的问题.关于ie6,7的兼容问题 <inline-block 前世今生>(ht ...
- Office 2010简体中文正式版完美终身破解版免费下载
名称:Microsoft Office Professional 2010 正式版(JK破解免序列号安装版) Microsoft Office 2010 专业版 优点:用官方Microsoft off ...
- Delphi Socket Demo
Delphi Socket Demo 转自 http://www.anqn.com/dev/delphi/2010-01-07/a09122531-1.shtml 自己对中间有点修改,下面是代码 ...
- gdb的user-define command
搜索: user-defined例子. # save this file in ~/.gdb or some where easy to find. # then in ~/.gdbinit add ...
- oracle任务计划debug
crontab -eno crontab for root - using an empty onecrontab: installing new crontab"/tmp/crontab. ...
- #include <iostream>
1 static_assert 2 std::nothrow 3 std::ref() 4 std::string 1 static_assert 执行编译时断言检查 语法 static_assert ...
- sql server group by having 之复习篇
where 与 having 之间的差别在于where 是分组前的过滤,而having是分组后的过滤 Group By中Select指定的字段限制 示例3 select 类别, sum(数量) as ...
- 自学xml的几个例子
xml是一种被用来传输和存储数据的语言,下面给出一些学习xml过程一些简单的例子.具体xml语法请转:http://www.w3school.com.cn/xml/xml_intro.asp 例子1: ...