[HDU5713]K个联通块
[HDU5713]K个联通块
题目大意:
有一张\(n(n\le14)\)个点,\(m\)条边无重边的无向图,求有多少个边集,使得删掉边集里的边后,图里恰好有\(k\)个连通块。
思路:
一个显然的动态规划是,\(f_{s,i}\)表示点集为\(s\),分成\(i\)个连通块的方案数。
转移什么的都很显然,关键是如何求\(f_{s,1}\)。(万事开头难!)
\(f_{s,1}\)的含义是删去\(s\)中若干条边使得新图仍然连通的方案数。我们可以将其转化为任意删边的方案数-删边使得该图不连通的方案数。
而后者就相对好求。
源代码:
#include<cstdio>
#include<cctype>
#include<cstring>
#include<numeric>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef long long int64;
const int N=14,M=105,mod=1e9+9;
struct Edge {
int u,v;
};
Edge e[M];
int f[1<<N][N+1],cnt[1<<N];
inline int power(int a,int k) {
int ret=1;
for(;k;k>>=1) {
if(k&1) ret=(int64)ret*a%mod;
a=(int64)a*a%mod;
}
return ret;
}
inline int inv(const int &x) {
return power(x,mod-2);
}
inline int lowbit(const int &x) {
return x&-x;
}
int main() {
const int T=getint();
for(register int i=1;i<=T;i++) {
memset(f,0,sizeof f);
memset(cnt,0,sizeof cnt);
const int n=getint(),m=getint(),k=getint();
int tmp=0;
for(register int i=0;i<m;i++) {
e[i]=(Edge){getint()-1,getint()-1};
if(e[i].u==e[i].v) tmp++;
}
for(register int i=0;i<n;i++) f[1<<i][1]=1;
for(register int s=0;s<1<<n;s++) {
if(__builtin_popcount(s)<=1) continue;
for(register int i=0;i<n;i++) {
if((s>>i)&1) {
for(register int j=0;j<m;j++) {
const int &u=e[j].u,&v=e[j].v;
if(u==v) continue;
if(i==u&&((s>>v)&1)) cnt[s]++;
if(i==v&&((s>>u)&1)) cnt[s]++;
}
}
}
cnt[s]>>=1;
const int v=s^lowbit(s);
for(register int t=(v-1)&v;;t=(t-1)&v) {
(f[s][1]+=(int64)f[t^lowbit(s)][1]*power(2,cnt[s^t^lowbit(s)])%mod)%=mod;
if(!t) break;
}
f[s][1]=(power(2,cnt[s])-f[s][1]+mod)%mod;
}
for(register int j=2;j<=k;j++) {
for(register int s=1;s<1<<n;s++) {
for(register int t=(s-1)&s;t;t=(t-1)&s) {
(f[s][j]+=(int64)f[t][j-1]*f[s^t][1]%mod)%=mod;
}
f[s][j]=(int64)f[s][j]*inv(j)%mod;
}
}
printf("Case #%d:\n%lld\n",i,(int64)f[(1<<n)-1][k]*power(2,tmp)%mod);
}
return 0;
}
[HDU5713]K个联通块的更多相关文章
- hdu5713 K个联通块[2016百度之星复赛B题]
dp 代码 #include<cstdio> ; ; int n,m,k,cnt[N]; ]; ][],i,j,l,a,b; int check(int x,int y) { int i; ...
- 树上第k大联通块
题意:求树上第k大联通块 n,k<=1e5 考虑转化为k短路的形式. 也就是要建出一张图是的这条图上每一条S到T的路径都能代表一个联通块. 点分治建图 递归下去,假定每个子树的所有联通块中都可以 ...
- K个联通块
题意: 有一张无重边的无向图, 求有多少个边集,使得删掉边集里的边后,图里恰好有K个联通块. 解法: 考虑dp,$h(i,S)$表示有$i$个联通块,点集为$S$的图的个数,$g(S)$表示点集为S的 ...
- 【HDOJ5713】K个联通块(状压DP,计数)
题意:有一张无重边的无向图, 求有多少个边集,使得删掉边集里的边后,图里恰好有K个连通块. 1≤T≤201≤K≤N≤140≤M≤N∗(N+1)/21≤a,b≤N 思路:From http://blog ...
- Codeforces 731C. Socks 联通块
C. Socks time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard input o ...
- Codeforces Round #369 (Div. 2) D. Directed Roads dfs求某个联通块的在环上的点的数量
D. Directed Roads ZS the Coder and Chris the Baboon has explored Udayland for quite some time. The ...
- Educational Codeforces Round 5 - C. The Labyrinth (dfs联通块操作)
题目链接:http://codeforces.com/contest/616/problem/C 题意就是 给你一个n行m列的图,让你求’*‘这个元素上下左右相连的连续的’.‘有多少(本身也算一个), ...
- [洛谷P1197/BZOJ1015][JSOI2008]星球大战Starwar - 并查集,离线,联通块
Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...
- PAT A1013 Battle Over Cities (25 分)——图遍历,联通块个数
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
随机推荐
- Google Email 帐户泄露
最初爆出来的网站是:https://forum.btcsec.com/index.php?/topic/9426-gmail-meniai-parol/,是一个俄罗斯论坛,然后..就流传开来了... ...
- 流媒体服务器之————EasyDarwin开源流媒体服务器:编译、配置、部署
源码下载地址:https://github.com/EasyDarwin/EasyDarwin/archive/v7.0.5.zip 查看 Ubuntu 的版本号 sudo lsb_release - ...
- Hash::make与Hash::check
调用方法之前要先去引用: use Illuminate\Support\Facades\Hash; 可以调用 Hash 门面上的 make 方法对存储密码进行哈希: $pwd = Hash::make ...
- 第13月第16天 ios keywindow
1. 在弹出层弹出后keywindow已改变 http://www.jianshu.com/p/4695d7efa20b
- linux服务器如何添加sudo用户
1. 编辑 vi /etc/ssh/sshd_config 文件,修改默认端口:默认Port为22,并且已经注释掉了,修改是把注释去掉,并修改成其它的端口. 原来用默认端口:22修改为:8975 (这 ...
- .NET C#错误:所生成项目的处理器框架“MSIL”与引用“wdapi_dotnet1021”的处理器架构“AMD64”不匹配
.NET C#错误:所生成项目的处理器框架“MSIL”与引用“wdapi_dotnet1021”的处理器架构“AMD64”不匹配. 直接在项目右键属性->生成->x64. 即可解决
- ajax与302响应
在ajax请求中,如果服务器端的响应是302 Found,在ajax的回调函数中能够获取这个状态码吗?能够从Response Headers中得到Location的值进行重定向吗?让我们来一起看看实际 ...
- make distclean
清空bin目录make dirclean 清空所有相关的东西,包括下载的软件包,配置文件,feeds内容等make distclean 这个命令会删除feeds目录及其下面的所有的文件,直接结果就是运 ...
- PHP+mysql系统报错:PHP message: PHP Warning: Unknown: Failed to write session data (files)
PHP+mysql系统报错:PHP message: PHP Warning: Unknown: Failed to write session data (files) 故障现象,后台页面点击没有 ...
- poj1292
prim,把每个墙看成一个节点,从起点用prim求最小生成树,直到覆盖到终点为止,输出最小生成树中的最大边 #include <cstdio> #include <cmath> ...