参考[luogu7417],同样求出最短路,得到二元组$(x,y)$并排序,记$tot_{(x,y)}$为$(x,y)$的数量

其中所给的两个条件,即分别要求:

1.$(x,y)$只能和$(x\pm 1,y\pm 1)$连边

2.每一个$(x,y)$都向$(x-1,y\pm 1)$中的一个连边、$(x\pm 1,y-1)$中的一个连边

(另外,注意在$x+1=y$时$(x+1,y-1)$也即为$(x,y)$自身)

从前往后依次dp,假设考虑到二元组为$(x,y)$,将之前二元组分为两类——

(1)对于其中不为$(x,y)$的二元组之后已经不存在能通过连边满足第2个条件的点了,那么就需要保证第2个条件都已经满足,即状态中不需要再记录

(2)对于二元组$(x,y)$,之后也不存在$(x-1,y\pm 1)$的点了,因此$x$的一维必须要已经满足,并记录$y$的一维不满足的数个数即可

由此,即得到一个二维的状态,将其记为$f_{(x,y),i}$(其中$i$的定义参考(2)中)

关于转移,有以下四类本质不同的边:

(1)$(x,y)$与$(x-1,y+1)$中$y$的一维已经满足点的的边

(2)$(x,y)$与$(x-1,y+1)$中$y$的一维仍未满足的点的边

(3)$(x,y)$与$(x-1,y-1)$的边

(4)若$x+1=y$,$(x,y)$和$(x,y)$之间的边

先转移前两类边(避免出现三维状态),枚举后者的点数,转移即
$$
f_{(x,y),i}={tot_{(x,y)}\choose i}\sum_{j=0}^{tot_{(x-1,y+1)}}G(tot_{(x-1,y+1)},j,tot_{(x,y)}-i)f_{(x-1,y+1),j}
$$
(注意此时的$i$的定义并不为最终的定义,仅是一个中间过程)

其中$G(x,z,y)$表示在左边$x$个点和右边$y$个点连边的$2^{xy}$张图中,满足左边特定的$z$个点(如前$z$个点)和右边的$y$​个点度数均非0的方案数,对左边容斥有
$$
G(x,z,y)=\sum_{i=0}^{z}(-1)^{i}{z\choose i}(2^{x-i}-1)^{y}
$$
再转移第3类边,考虑转移的状态$f_{(x,y),j}$,这$j$个点必须都选择(否则$x$这一维不满足),因此转移即
$$
f_{(x,y),i}=\sum_{j=0}^{tot_{(x,y)}}{tot_{(x,y)}-j\choose i}(2^{tot_{(x-1,y-1)}}-1)^{tot_{(x,y)}-i}f_{(x,y),j}
$$
(这里是类似于01背包的,即后者$f_{(x,y),j}$应该是未转移第3类边的结果)

最后转移第4类边,其实这只是一个特例,更完整的情况应该是对于$(x+1,y-1)$不再出现的点(即其之后不再参与转移,之后也没有点可以帮助其满足第2个条件)将其的贡献乘到答案上

具体的,这类状态又分为两类:

1.$x+1\ne y$,这类状态不能通过自环使其满足条件,因此贡献即$f_{(x,y),0}$

2.$x+1=y$,这还可以通过自环使其满足条件

具体的,贡献即$\sum_{i=0}^{tot_{(x,y)}}G(tot_{(x,y)},i)f_{(x,y),i}$,其中$G(x,y)$表示在$x$个点中任意连边的$2^{\frac{x(x+1)}{2}}$张图中(允许自环),满足特定的$y$个点度数均非0的方案数,对这$y$​个点容斥有
$$
G(x,y)=\sum_{i=0}^{y}(-1)^{i}{y\choose i}2^{\frac{(x-i)(x-i+1)}{2}}
$$
不难发现$f$的状态数实际仅为$o(n)$,转移复杂度为$o(n^{2})$,总复杂度即$o(n^{3})$,可以通过

  1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 105
4 #define M 10005
5 #define mod 1000000007
6 #define oo 0x3f3f3f3f
7 #define ll long long
8 struct Edge{
9 int nex,to;
10 }edge[M*3];
11 queue<int>q;
12 int t,n,m,E,x,y,ans,mi[M],Mi[N][N],C[N][N],head[N<<1],d[N<<1],tot[N][N],g[N],f[N];
13 void add(int x,int y){
14 edge[E].nex=head[x];
15 edge[E].to=y;
16 head[x]=E++;
17 }
18 int G(int x,int y){
19 int ans=0;
20 for(int i=0;i<=y;i++){
21 int s=(ll)C[y][i]*mi[(x-i)*(x-i+1)/2]%mod;
22 if (i&1)ans=(ans-s+mod)%mod;
23 else ans=(ans+s)%mod;
24 }
25 return ans;
26 }
27 int G(int x,int z,int y){
28 int ans=0;
29 for(int i=0;i<=z;i++){
30 int s=(ll)C[z][i]*Mi[x-i][y]%mod;
31 if (i&1)ans=(ans-s+mod)%mod;
32 else ans=(ans+s)%mod;
33 }
34 return ans;
35 }
36 int main(){
37 mi[0]=1;
38 for(int i=1;i<M;i++)mi[i]=2*mi[i-1]%mod;
39 for(int i=0;i<N;i++){
40 Mi[i][0]=1;
41 for(int j=1;j<N;j++)Mi[i][j]=(ll)Mi[i][j-1]*(mi[i]-1)%mod;
42 }
43 for(int i=0;i<N;i++){
44 C[i][0]=C[i][i]=1;
45 for(int j=1;j<i;j++)C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
46 }
47 scanf("%d",&t);
48 while (t--){
49 scanf("%d%d",&n,&m);
50 E=0,ans=1;
51 memset(head,-1,sizeof(head));
52 memset(d,oo,sizeof(d));
53 memset(tot,0,sizeof(tot));
54 for(int i=1;i<=m;i++){
55 scanf("%d%d",&x,&y);
56 add(x,y+n),add(y+n,x);
57 add(x+n,y),add(y,x+n);
58 }
59 d[1]=0;
60 q.push(1);
61 while (!q.empty()){
62 int k=q.front();
63 q.pop();
64 for(int i=head[k];i!=-1;i=edge[i].nex)
65 if (d[edge[i].to]==oo){
66 d[edge[i].to]=d[k]+1;
67 q.push(edge[i].to);
68 }
69 }
70 if (d[n+1]==oo){
71 for(int i=1;i<=n;i++)tot[0][min(d[i],d[i+n])]++;
72 for(int i=1;i<=n;i++)ans=(ll)ans*Mi[tot[0][i-1]][tot[0][i]]%mod;
73 printf("%d\n",ans);
74 continue;
75 }
76 for(int i=1;i<=n;i++)tot[d[i]+d[i+n]-d[n+1]>>1][min(d[i],d[i+n])]++;
77 for(int x=0;x<=n;x++){
78 memset(f,0,sizeof(f));
79 f[0]=1;
80 for(int y=x;y<=n;y++){
81 if (!tot[x][y]){
82 if (y-x<=(d[n+1]>>1))ans=(ll)ans*f[0]%mod;
83 else{
84 int s=0;
85 for(int i=0;i<=tot[x][y-1];i++)s=(s+(ll)G(tot[x][y-1],i)*f[i])%mod;
86 ans=(ll)ans*s%mod;
87 }
88 memset(f,0,sizeof(f));
89 f[0]=1;
90 continue;
91 }
92 if ((!x)&&(!y)){
93 f[0]=0,f[1]=1;
94 continue;
95 }
96 memcpy(g,f,sizeof(g));
97 memset(f,0,sizeof(f));
98 for(int i=0;i<=tot[x][y];i++){
99 int s=0;
100 for(int j=0;j<=tot[x][y-1];j++)s=(s+(ll)G(tot[x][y-1],j,tot[x][y]-i)*g[j])%mod;
101 f[i]=(f[i]+(ll)C[tot[x][y]][i]*s)%mod;
102 }
103 memcpy(g,f,sizeof(g));
104 memset(f,0,sizeof(f));
105 if (!x){
106 f[tot[x][y]]=g[0];
107 continue;
108 }
109 for(int i=0;i<=tot[x][y];i++)
110 for(int j=0;j<=tot[x][y];j++)f[i]=(f[i]+(ll)C[tot[x][y]-j][i]*Mi[tot[x-1][y-1]][tot[x][y]-i]%mod*g[j])%mod;
111 }
112 }
113 printf("%d\n",ans);
114 }
115 return 0;
116 }

[luogu7418]Counting Graphs P的更多相关文章

  1. Python Object Graphs — objgraph 1.7.2 documentation

    Python Object Graphs - objgraph 1.7.2 documentation Python Object Graphs¶ objgraph is a module that ...

  2. ARC(Automatic Reference Counting )技术概述

    此文章由Tom翻译,首发于csdn的blog 转自:http://blog.csdn.net/nicktang/article/details/6792972 Automatic Reference ...

  3. 萌新笔记——Cardinality Estimation算法学习(二)(Linear Counting算法、最大似然估计(MLE))

    在上篇,我了解了基数的基本概念,现在进入Linear Counting算法的学习. 理解颇浅,还请大神指点! http://blog.codinglabs.org/articles/algorithm ...

  4. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

  5. ZOJ3944 People Counting ZOJ3939 The Lucky Week (模拟)

    ZOJ3944 People Counting ZOJ3939 The Lucky Week 1.PeopleConting 题意:照片上有很多个人,用矩阵里的字符表示.一个人如下: .O. /|\ ...

  6. find out the neighbouring max D_value by counting sort in stack

    #include <stdio.h> #include <malloc.h> #define MAX_STACK 10 ; // define the node of stac ...

  7. 1004. Counting Leaves (30)

    1004. Counting Leaves (30)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  8. 6.Counting Point Mutations

    Problem Figure 2. The Hamming distance between these two strings is 7. Mismatched symbols are colore ...

  9. 1.Counting DNA Nucleotides

    Problem A string is simply an ordered collection of symbols selected from some alphabet and formed i ...

随机推荐

  1. spark性能优化(一)

    本文内容说明 初始化配置给rdd和dataframe带来的影响 repartition的相关说明 cache&persist的相关说明 性能优化的说明建议以及实例 配置说明 spark:2.4 ...

  2. Great books for learning C++

    Great books for learning C++ Here are three great books for learning C++ – whether you know C++ alre ...

  3. F1英国大奖赛-银石赛道地图及弯道

    背景 今天晚上(2020-08-02)是今年英国大奖赛的正赛.刚好了解了一下赛道地图.记录一下,明年就不用到处找了. 简介 银石赛道(Silverstone Circuit)由一个废弃的空军基地改建, ...

  4. Sequence Model-week1编程题3-用LSTM网络生成爵士乐

    Improvise a Jazz Solo with an LSTM Network 实现使用LSTM生成音乐的模型,你可以在结束时听你自己的音乐,接下来你将会学习到: 使用LSTM生成音乐 使用深度 ...

  5. MarkDown之Typora使用

    Typora:所见即所得 常用快捷键 加粗:ctrl + B 标题:ctrl + 16,对于与16级标题 插入公式:ctrl + Shift + m 插入代码:ctrl + Shift + K 插入图 ...

  6. win10安装git fatal: open /dev/null or dup failed: No such file or directory错误解决方法

    原因看大家意思应该是 非即插即用驱动文件null.sys问题. 网上有很多方案.最后试了一个可行的. 替换  windows/system32/drivers/null.sys为网盘中的文件即可. 链 ...

  7. Beta阶段第七次会议

    Beta阶段第七次会议 时间:2020.5.23 完成工作 姓名 工作 难度 完成度 ltx 1.修改小程序页面无法加载bug2.修改条件语句,使得页面能够正常显示 中 90% xyq 1.根据api ...

  8. 【二食堂】Alpha- 发布声明

    MarkTexting Alpha版本发布声明 今日我们的MarkTexting正式上线了! 功能介绍 我们实现了一个简单的文本标注生成知识图谱的网站,在Alpha阶段,我们实现的功能有: 登陆注册 ...

  9. 用cmd命令行创建vue项目模板

    1.进入cmd命令行 输入存放项目的位置 2.通过vue create 项目名称 创建项目 3.选择Manually select features 4.通过空格选中第1.2.5.6.7.去掉8 4. ...

  10. Qt坐标转换系统的理解

    转 https://blog.csdn.net/hgcprg/article/details/53537106 今天又看了一篇对Qt坐标转换系统以及QTransform的博客,作者讲的非常透彻,链接如 ...