题意:

现在有一个$n$个点的树形图被拆开,现在你知道其中$m$条边,已经$q$对点的$LCA$,试求原先的树有多少种可能。

解法:

考虑$dp$,$f(x,S)$表示$x$的子树内的点集为$S$(不包括$x$的方案数)

$S$被拆成$S_0 ,S_1, S_2 ... S_m$,每个集合

这样考虑$LCA(a,b) =c$,与$<x,y> ∈ E$对$dp$的影响。

前者相当于$a,b$分属于两个$S_i$,

假设$x$连向的点为$y_0,y_1...$,

后者相当于不存在$S_i$中含有两个$y_i$ 且 当$S_i$中含有一个$y_i$时,必须要将$y_i$作为$S_i - \{ y_i \}$的父节点。

这样,由于每一层要背包,还要状态压缩,代码十分的复杂。

考虑类比转二叉树的方法,$f(x,S)$ 由 $f(x,S \oplus S0) \cdot f(p, S0 \oplus p)$ 转移过来。

这样代码会简化许多。

枚举子集的时候应用 $ S_0 = S \& (S_0-1)$ 的技巧。

这样,总复杂度 $O(n*3^n + q*n*2^n )$。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector> #define N 14
#define LL long long
#define bit(x) (1<<(x)) using namespace std; int n,m,q;
int g[N];
LL f[N][<<N];
vector<int> LCA[N]; int get_bit(int S)
{
for(int i=;i<n;i++)
if(S&bit(i)) return i;
return -;
} int cnt_bit(int S)
{
int ans=;
for(;S;S>>=) if(S&) ans++;
return ans;
} bool check(int x,int S)
{
for(int i=;i<(int)LCA[x].size();i++)
if((S&LCA[x][i]) != LCA[x][i]) return ;
return ;
} LL dp(int x,int S)
{
if(f[x][S]!=-) return f[x][S];
if(!S) return f[x][S] = ;
f[x][S]=;
int t=get_bit(S);
for(int S0=S;S0;S0=(S0-)&S)
if(S0&bit(t))
{
bool flag=;
for(int i=;i<(int)LCA[x].size();i++)
if((S0&LCA[x][i]) == LCA[x][i]){flag=; break;}
if(flag || cnt_bit(g[x]&S0)>) continue;
int tmp=get_bit(g[x]&S0);
if(tmp!=-)
{
if(check(tmp,S0) && ( (S0|bit(x)) & g[tmp] ) == g[tmp])
f[x][S] += dp(x,S^S0)*dp(tmp,S0^bit(tmp));
}
else
{
for(int i=;i<n;i++)
if(S0&bit(i))
{
if(check(i,S0) && (S0&g[i]) == g[i])
f[x][S] += dp(x,S^S0)*dp(i,S0^bit(i));
}
}
}
return f[x][S];
} int main()
{
while(~scanf("%d%d%d",&n,&m,&q))
{
for(int i=;i<n;i++)
{
for(int j=;j<(<<n);j++)
f[i][j]=-;
g[i]=;
LCA[i].clear();
}
for(int i=,x,y;i<=m;i++)
{
scanf("%d%d",&x,&y);
x--,y--;
g[x]|=bit(y);
g[y]|=bit(x);
}
for(int i=,x,y,z;i<=q;i++)
{
scanf("%d%d%d",&x,&y,&z);
x--,y--,z--;
LCA[z].push_back(bit(x)|bit(y));
}
cout << dp(,((<<n)-)^) << endl;
}
return ;
}

Sandy and Nuts的更多相关文章

  1. Codeforces 599E Sandy and Nuts(状压DP)

    题目链接 Sandy and Nuts 题意大概就是给出限制条件求出在该限制条件下树的种数. #include <bits/stdc++.h> using namespace std; # ...

  2. CodeForces 599E Sandy and Nuts 状压DP

    题意: 有一棵\(n(1 \leq n \leq 13)\)个节点的树,节点的标号为\(1 \sim n\),它的根节点是\(1\). 现在已知它的\(m(0 \leq m < n)\)条边,和 ...

  3. Solution -「CF 599E」Sandy and Nuts

    \(\mathcal{Description}\)   Link.   指定一棵大小为 \(n\),以 \(1\) 为根的有根树的 \(m\) 对邻接关系与 \(q\) 组 \(\text{LCA}\ ...

  4. 「算法笔记」状压 DP

    一.关于状压 dp 为了规避不确定性,我们将需要枚举的东西放入状态.当不确定性太多的时候,我们就需要将它们压进较少的维数内. 常见的状态: 天生二进制(开关.选与不选.是否出现--) 爆搜出状态,给它 ...

  5. 【BZOJ-4698】Sandy的卡片 后缀数组

    4698: Sdoi2008 Sandy的卡片 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 140  Solved: 55[Submit][Stat ...

  6. BZOJ 4698: Sdoi2008 Sandy的卡片

    4698: Sdoi2008 Sandy的卡片 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 106  Solved: 40[Submit][Stat ...

  7. Timus 2068. Game of Nuts 解题报告

    1.题目描述: 2068. Game of Nuts Time limit: 1.0 secondMemory limit: 64 MB The war for Westeros is still i ...

  8. sandy bridge

      SANDY BRIDGE SPANS GENERATIONS Intel Focuses on Graphics, Multimedia in New Processor Design By Li ...

  9. ural 2068. Game of Nuts

    2068. Game of Nuts Time limit: 1.0 secondMemory limit: 64 MB The war for Westeros is still in proces ...

随机推荐

  1. mysql查询处理顺序

    http://zhangzhaoaaa.iteye.com/blog/1689412参考:<MYSQL技术内幕SQL编程> select distinct <selectlist&g ...

  2. 【转】Linux上的free命令详解

    解释一下Linux上free命令的输出.默认输出是KB,可以用free -m则输出是MB 下面是free的运行结果,一共有4行.为了方便说明,我加上了列号.这样可以把free的输出看成一个二维数组FO ...

  3. Android摄像头採集的视频数据流怎样通过Socket实时发送到目标服务端

    分两块: 1.取得摄像头採集的视频流 2.发送到server端 protected MediaRecorder mMediaRecorder; private LocalServerSocket mL ...

  4. YUV格式

    http://blog.csdn.net/u011270282/article/details/50696616 http://blog.csdn.net/acs713/article/details ...

  5. PHP对称加密类

    <?php /** * Created by PhpStorm. * User: zongbinghuang * Date: 2017/7/31 * Time: 15:13 */ namespa ...

  6. commons.cli.jar 作用

    对命令行进行处理的jar包.处理的步骤主要包括定义.分析和询问.(There are three stages to command line processing. They are the def ...

  7. 怎么样获得泛型T的Class对象?

    public class GenClass<T> { private Class<T> entityClass; } public class Test { public st ...

  8. react遇到的各种坑

    标签里用到<label for>的,for 要写成htmlFor 标签里的class要写成className 组件首字母一定要大写 单标签最后一定要闭合 如果html里要空格转义, 注意不 ...

  9. 对于js里的闭包的理解

    Ali的回答: 当function里嵌套function时,内部的function可以访问外部function里的变量. function foo(x) {     var tmp = 3;      ...

  10. Javascript学习之Date对象详解

    1.定义 创建 Date 实例用来处理日期和时间.Date 对象基于1970年1月1日世界协调时起的毫秒数 2.语法 构造函数 new Date() new Date(value) value代表自世 ...