【BZOJ3162】独钓寒江雪 树同构+DP
【BZOJ3162】独钓寒江雪
题解:先进行树hash,方法是找重心,如果重心有两个,则新建一个虚点将两个重心连起来,新点即为新树的重心。将重心当做根进行hash,hash函数不能太简单,我的方法是:将x的所有儿子的hash值排序,然后将这些hash值立方合在一起作为x的hash值。
进行完树hash后,我们考虑DP。首先不考虑同构,设f[0/1][x]表示选(不选)x时,在x的子树中选出独立集的方案数,则有
$f[0][x]=\prod f[1][y]+f[0][y]\\f[1][x]=\prod f[0][y]$
考虑同构,如果x有m个儿子是相同的,它们的f值都是s,那么可以转化成如下问题:给m个相同球染s种颜色有多少种方案,显然答案=$C_{m+s-1}^m$。
如果根是虚点,那么最后统计答案的时候需要特判。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn=500010;
const ll mod=1000000007;
int n,rt,rt1,rt2,cnt;
int head[maxn],next[maxn<<1],to[maxn<<1],siz[maxn];
bool vis[maxn];
ull hs[maxn];
ll f[2][maxn],ine[maxn],jcc[maxn];
vector<int> ch[maxn];
bool cmp(int a,int b)
{
return hs[a]<hs[b];
}
inline int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-')f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret*f;
}
void findr(int x,int fa)
{
siz[x]=1;
int flag=0;
for(int i=head[x];i!=-1;i=next[i]) if(to[i]!=fa) findr(to[i],x),siz[x]+=siz[to[i]],flag|=(siz[to[i]]>(n/2));
flag|=(n-siz[x]>(n/2));
if(!flag&&rt1) rt2=x;
if(!flag&&!rt1) rt1=x;
}
void add(int a,int b)
{
to[cnt]=b,next[cnt]=head[a],head[a]=cnt++;
}
void gethash(int x)
{
for(int i=head[x];i!=-1;i=next[i]) if(!vis[to[i]]) vis[to[i]]=1,ch[x].push_back(to[i]);
hs[x]=ch[x].size()+1;
if(!ch[x].size()) return ;
for(int i=0;i<(int)ch[x].size();i++) gethash(ch[x][i]);
sort(ch[x].begin(),ch[x].end(),cmp);
for(int i=0;i<(int)ch[x].size();i++) hs[x]=hs[x]*131+hs[ch[x][i]]*hs[ch[x][i]]*hs[ch[x][i]];
}
ll calc(ll a,ll b)
{
ll ret=1;
for(ll i=a;i>a-b;i--) ret=ret*i%mod;
return ret*jcc[b]%mod;
}
void dfs(int x)
{
f[0][x]=1,f[1][x]=1;
ll now=0;
for(int i=0,j;i<(int)ch[x].size();i++)
{
j=ch[x][i],dfs(j);
now++;
if(i==(int)ch[x].size()-1||hs[j]!=hs[ch[x][i+1]])
{
f[0][x]=f[0][x]*calc(now+f[1][j]+f[0][j]-1,now)%mod;
f[1][x]=f[1][x]*calc(now+f[0][j]-1,now)%mod;
now=0;
}
}
}
int main()
{
n=rd();
int i,a,b;
memset(head,-1,sizeof(head));
ine[1]=jcc[1]=1;
for(i=2;i<=n;i++) ine[i]=(mod-(mod/i)*ine[mod%i]%mod)%mod,jcc[i]=jcc[i-1]*ine[i]%mod;
for(i=1;i<n;i++) a=rd(),b=rd(),add(a,b),add(b,a);
findr(1,0);
if(rt2) add(++n,rt1),add(n,rt2),rt=n;
else rt=rt1;
vis[rt]=1,gethash(rt);
dfs(rt);
if(rt2)
{
if(hs[rt1]==hs[rt2]) printf("%lld",(f[0][rt1]*(f[0][rt1]+1)/2+f[0][rt1]*f[1][rt2])%mod);
else printf("%lld",(f[0][rt]-f[1][rt1]*f[1][rt2]%mod+mod)%mod);
}
else printf("%lld",(f[0][rt]+f[1][rt])%mod);
return 0;
}
【BZOJ3162】独钓寒江雪 树同构+DP的更多相关文章
- BZOJ 3162 / Luogu P4895: 独钓寒江雪 树hash+DP
题意 给出一棵无根树,求本质不同的独立集数模100000000710000000071000000007的值. n≤500000n\le 500000n≤500000 题解 如果是有根树就好做多了.然 ...
- 『Andrew and Chemistry 树同构』
Andrew and Chemistry Description During the chemistry lesson Andrew learned that the saturated hydro ...
- 【BZOJ-3572】世界树 虚树 + 树形DP
3572: [Hnoi2014]世界树 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1084 Solved: 611[Submit][Status ...
- 【BZOJ-2286】消耗战 虚树 + 树形DP
2286: [Sdoi2011消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2120 Solved: 752[Submit][Status] ...
- 树状DP (poj 2342)
题目:Anniversary party 题意:给出N各节点的快乐指数,以及父子关系,求最大快乐指数和(没人职员愿意跟直接上司一起玩): 思路:从底向上的树状DP: 第一种情况:第i个员工不参与,F[ ...
- POJ3659 Cell Phone Network(树上最小支配集:树型DP)
题目求一棵树的最小支配数. 支配集,即把图的点分成两个集合,所有非支配集内的点都和支配集内的某一点相邻. 听说即使是二分图,最小支配集的求解也是还没多项式算法的.而树上求最小支配集树型DP就OK了. ...
- bzoj 2286 [Sdoi2011]消耗战(虚树+树上DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2286 [题意] 给定一棵树,切断一条树边代价为ci,有m个询问,每次问使得1号点与查询 ...
- uva12489 Combating cancer(树同构)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud https://uva.onlinejudge.org/index.php?opt ...
- poj3659树状DP
Cell Phone Network Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6273 Accepted: 225 ...
随机推荐
- Linux System Programming 学习笔记(一) 介绍
1. Linux系统编程的三大基石:系统调用.C语言库.C编译器 系统调用:内核向用户级程序提供服务的唯一接口.在i386中,用户级程序执行软件中断指令 INT n 之后切换至内核空间 用户程序通过寄 ...
- Codeforces989E. A Trance of Nightfall
$n \leq 200$个平面上的点,$q \leq 200$次询问:重复操作$m \leq 10000$次,到达点$x$的概率最大是多少.操作:一开始选点$P$,不一定要是给定点,可以是平面上任一点 ...
- Solr5.2.1+Zookeeper3.4.9分布式集群搭建
1.选取三台服务器 由于机器比较少,现将zookeeper和solr都部署在以下三台机器上.(以下操作都是在172.16.20.101主节点上进行的哦) 172.16.20.101 主节点 172.1 ...
- es6总结(十)--class
- Android组件实例化问题
对于Application. Activity. Notification. BroadCast. Service 这些组件的使用,对象的实例化问题各有不同,如何实例化以及在什么时候实例化也所有不同. ...
- vue 权限控制按钮3种样式、内容、以及跳转事件
最近碰到一个因为要根据权限来给一个按钮变成不同功能, 简单写出3个按钮然后用v-if也能实现这个功能,但是在加载页面时,如果延迟过高则会把按钮按照DOM顺序加载出来,这是个很不好的效果 思索了下,把三 ...
- va_list 简介
原文:http://blog.sina.com.cn/s/blog_590be5290100qhxr.html va_list是一个宏,由va_start和va_end界定. typedef char ...
- 小程序使用wxParse插件解析html标签图片间距问题
转自:https://www.cnblogs.com/likun123/p/9543376.html 小程序解析html标签,就需要用到wxParse啦.但是在解析连续图片的时候,会发现图片之间会有间 ...
- 快速比较两个uiimage是否相等防止使用原始dsdata造成界面卡顿问题
UIImage *imageLater = image1; UIImage *imagePre = image2; if (imageLater == imagePre){....}
- 3.【nuxt起步】-下面以一个SPA单页程序为例子
spa:single page applcation 1.components目录新建header.vue,footer.vue Header.vue Footer.vue Pages/index.v ...