题目大意:
  一个有向无环图上有n个结点,
  现在告诉你n-1个条件(x,y),表示x和y的先后关系。
  问原图共有几种可能的拓扑序?

思路:
  树形DP。
  f[i][j]表示对于第i个结点,有j个点在它前面的方案数。
  设当前结点为x,后面有一个结点为y,原本x前有i个结点,y前有j个结点,我们可以得到状态转移方程:
  f[x][size[x]-i+size[y]-j]+=f[x][size[x]-i]*c[i+j][i]*c[size[x]-i+size[y]-j][size[y]-j]*((sum[y][size[y]]-sum[y][size[y]-j]+mod)%mod);
  其中c是预处理好的组合数,sum是f数组的前缀和。
  同样对于y在x前面的情况,状态转移方程如下:
  f[x][i+j]+=f[x][i]*c[i+j][i]*c[size[x]-i+size[y]-j][size[y]-j]*sum[y][j];
  最后就是求f[root][0]~f[root][n-1]的和,也就是sum[root][n]。

 #include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
typedef long long int64;
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=,mod=1e9+;
struct Edge {
int to;
bool type;
};
std::vector<Edge> e[N];
inline void add_edge(const int &u,const int &v,const bool &type) {
e[u].push_back((Edge){v,type});
e[v].push_back((Edge){u,!type});
}
int c[N][N];
inline void prep() {
for(register int i=;i<N;i++) {
c[i][]=;
for(register int j=;j<=i;j++) {
c[i][j]=(c[i-][j-]+c[i-][j])%mod;
}
}
}
int f[N][N],sum[N][N],size[N];
inline void init() {
memset(f,,sizeof f);
for(register int i=;i<N;i++) {
e[i].clear();
}
}
void dp(const int &x,const int &par) {
size[x]=;
f[x][]=;
for(unsigned i=;i<e[x].size();i++) {
const int &y=e[x][i].to;
if(y==par) continue;
dp(y,x);
static int g[N];
memset(g,,sizeof g);
if(e[x][i].type) {
for(register int i=;i<=size[x];i++) {
for(register int j=;j<=size[y];j++) {
g[size[x]-i+size[y]-j]
+=(int64)f[x][size[x]-i]
*c[i+j][i]%mod
*c[size[x]-i+size[y]-j][size[y]-j]%mod
*((sum[y][size[y]]-sum[y][size[y]-j]+mod)%mod)%mod;
g[size[x]-i+size[y]-j]%=mod;
}
}
} else {
for(register int i=;i<=size[x];i++) {
for(register int j=;j<=size[y];j++) {
g[i+j]
+=(int64)f[x][i]
*c[i+j][i]%mod
*c[size[x]-i+size[y]-j][size[y]-j]%mod
*sum[y][j]%mod;
g[i+j]%=mod;
}
}
}
size[x]+=size[y];
memcpy(f[x],g,sizeof g);
}
size[x]++;
for(register int i=;i<=size[x];i++) {
sum[x][i]=(sum[x][i-]+f[x][i-])%mod;
}
}
int main() {
prep();
for(register int T=getint();T;T--) {
init();
const int n=getint();
for(register int i=;i<n;i++) {
const int u=getint(),sign=getchar(),v=getint();
add_edge(u,v,sign=='<');
}
dp(,-);
printf("%d\n",sum[][n]);
}
return ;
}

[HEOI2013]SAO的更多相关文章

  1. 3167: [Heoi2013]Sao [树形DP]

    3167: [Heoi2013]Sao 题意: n个点的"有向"树,求拓扑排序方案数 Welcome to Sword Art Online!!! 一开始想错了...没有考虑一个点 ...

  2. 【BZOJ3167】[HEOI2013]SAO(动态规划)

    [BZOJ3167][HEOI2013]SAO(动态规划) 题面 BZOJ 洛谷 题解 显然限制条件是一个\(DAG\)(不考虑边的方向的话就是一棵树了). 那么考虑树型\(dp\),设\(f[i][ ...

  3. P4099 [HEOI2013]SAO

    P4099 [HEOI2013]SAO 贼板子有意思的一个题---我()竟然没看题解 有一张连成树的有向图,球拓扑序数量. 树形dp,设\(f[i][j]\)表示\(i\)在子树中\(i\)拓扑序上排 ...

  4. BZOJ 3167: [Heoi2013]Sao

    3167: [Heoi2013]Sao Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 96  Solved: 36[Submit][Status][D ...

  5. P4099 [HEOI2013]SAO(树形dp)

    P4099 [HEOI2013]SAO 我们设$f[u][k]$表示以拓扑序编号为$k$的点$u$,以$u$为根的子树中的元素所组成的序列方案数 蓝后我们在找一个以$v$为根的子树. 我们的任务就是在 ...

  6. 【BZOJ3167/4824】[Heoi2013]Sao/[Cqoi2017]老C的键盘

    [BZOJ3167][Heoi2013]Sao Description WelcometoSAO(StrangeandAbnormalOnline).这是一个VRMMORPG,含有n个关卡.但是,挑战 ...

  7. [HEOI2013]SAO(树上dp,计数)

    [HEOI2013]SAO (这写了一个晚上QAQ,可能是我太蠢了吧.) 题目说只有\(n-1\)条边,然而每个点又相互联系.说明它的结构是一个类似树的结构,但是是有向边连接的,题目问的是方案个数,那 ...

  8. 【做题记录】 [HEOI2013]SAO

    P4099 [HEOI2013]SAO 类型:树形 \(\text{DP}\) 这里主要补充一下 \(O(n^3)\) 的 \(\text{DP}\) 优化的过程,基础转移方程推导可以参考其他巨佬的博 ...

  9. [HEOI2013]SAO ——计数问题

    题目大意: Welcome to SAO ( Strange and Abnormal Online).这是一个 VR MMORPG, 含有 n 个关卡.但是,挑战不同关卡的顺序是一个很大的问题. 有 ...

  10. [BZOJ3167][P4099][HEOI2013]SAO(树形DP)

    题目描述 Welcome to SAO ( Strange and Abnormal Online).这是一个 VR MMORPG, 含有 n 个关卡.但是,挑战不同关卡的顺序是一个很大的问题. 有 ...

随机推荐

  1. SQL server(到主机的TCPIPl连接失败的问题)

    1 首先要做的是在sql新建查询里输入 exec sys.sp_readerrorlog 0, 1, 'listening' 运行后 会显示你的sql 正在运行的tcp/ip接口 看看是否和你java ...

  2. 执行impdp时出现的各种问题

    1.不同的表空间,不同的用户,不同的表名 impdp ODS_YYJC_BUF_ZB/ODS_YYJC_BUF_ZB job_name=bs3 directory=EXPDMP exclude=OBJ ...

  3. PHP对象3: public / private / protected

    <?php /* public 可继承, 内外可访问 private 不可, 只内部访问 protected 可继承, 只内部 */ class A{ protected $name; priv ...

  4. python并发编程之进程、线程、协程的调度原理(六)

    进程.线程和协程的调度和运行原理总结. 系列文章 python并发编程之threading线程(一) python并发编程之multiprocessing进程(二) python并发编程之asynci ...

  5. 76.ZYNQ-用PS控制DDR3内存读写

    本编文章的目的主要用简明的方法对DDR3进行读写,当然这种方式每次读写都需要CPU干预,效率是比较低的,但是这是学习的过程吧. 本系列文章尽可能的让每一个实验都相对独立,过程尽可能保证完整性,保证实验 ...

  6. Nginx部署部分https与部分http【转】

    转自 Nginx部署部分https与部分http - na_tion的专栏 - 博客频道 - CSDN.NEThttp://blog.csdn.net/na_tion/article/details/ ...

  7. js实现图片上传预览

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. 84.Largest Rectangle in histogram---stack

    题目链接:https://leetcode.com/problems/largest-rectangle-in-histogram/description/ 题目大意:在直方图中找出最大的矩形面积.例 ...

  9. <mvc:annotation-driven/>都做了那些事情

    mvc:annotation-driven是一种简写的配置方式,那么mvc:annotation-driven到底做了哪些工作呢?如何替换掉mvc:annotation-driven呢? <mv ...

  10. 关于angular导入第三方库的问题

    angular-cli使用webpack来将模块打包,在这里配置的scripts和styles会被打包成script.bundle.js和styles.bundle.js文件加载到前台页面. 这样就可 ...