BNUOJ 52305 Around the World 树形dp
生活随笔
收集整理的這篇文章主要介紹了
BNUOJ 52305 Around the World 树形dp
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目鏈接:
https://www.bnuoj.com/v3/problem_show.php?pid=52305
Around the World
題意
給你一顆樹(shù),相鄰兩點(diǎn)間有2c條邊,問(wèn)以1為起點(diǎn)和終點(diǎn)的歐拉回路有多少種。
題解
樹(shù)形dp
兄弟之間考慮可重集的排序,父子之間則考慮下插板法。
dp[u]表示以u(píng)為根的子樹(shù)能跑的所有歐拉回路。
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printftypedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);//start----------------------------------------------------------------------const int maxn=1e5+10;
const int maxm=2e6+100;
const int mod=1e9+7;VPII G[maxn];int n;LL dp[maxn];
int cntv[maxn];
LL inv[maxm],invfac[maxm],fac[maxm];LL get_C(int n,int m){return fac[n]*invfac[m]%mod*invfac[n-m]%mod;
}void dfs(int u,int fa){LL &res=dp[u]=1;cntv[u]=0;rep(i,0,G[u].sz()){int v=G[u][i].X,c=G[u][i].Y;if(v==fa) continue;dfs(v,u);cntv[u]+=c;//兒子的兄弟間的可重集排列res=res*invfac[c]%mod;//上下要插板res=res*get_C(cntv[v]+c-1,c-1)%mod;//內(nèi)部階乘res=res*fac[2*c]%mod;//分步乘法res=res*dp[v]%mod;}//兒子的兄弟間的可重集排列res=res*fac[cntv[u]]%mod;
}void pre(){fac[0]=fac[1]=1;invfac[0]=invfac[1]=1;inv[1]=1;rep(i,2,maxm){fac[i]=fac[i-1]*i%mod;inv[i]=(mod-mod/i)*inv[mod%i]%mod;invfac[i]=invfac[i-1]*inv[i]%mod;}
}int main() {pre();scf("%d",&n);for(int i=0;i<n-1;i++){int u,v,c;scf("%d%d%d",&u,&v,&c);G[u].pb(mkp(v,c));G[v].pb(mkp(u,c));}dfs(1,-1);prf("%lld\n",dp[1]);return 0;
}//end-----------------------------------------------------------------------
轉(zhuǎn)載于:https://www.cnblogs.com/fenice/p/5930281.html
總結(jié)
以上是生活随笔為你收集整理的BNUOJ 52305 Around the World 树形dp的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: “夜妒燕双栖”下一句是什么
- 下一篇: 乌镇哪个季节去比较好