Description
Input
Output
Sample Input
Sample Output
HINT
Source
树Hash+树形DP
考虑树的重心一定不会变,以重心为根Hash
同时如果有两个重心,就加一个虚点
然后考虑有u的儿子有x个Hash值相同的,方案数为y
它们的组合方案
相当于有y种物品,每种无限个,选出x个的方案数,这个用组合数统计一下即可
然后相当于统计独立集的方案数,稍微改一改
#include <bits/stdc++.h>
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define fill( x, y ) memset( x, y, sizeof x )
using namespace std;
typedef long long LL;
typedef pair < int, int > pa;
const int MAXN = 500005;
const int mod = 1e9 + 7;
inline int read()
{
int sc = 0; char ch = getchar();
while( ch < '0' || ch > '9' ) ch = getchar();
while( ch >= '0' && ch <= '9' ) sc = sc * 10 + ch - '0', ch = getchar();
return sc;
}
struct edge { int to, nxt; }e[MAXN << 1];
int f[MAXN], g[MAXN], n, head[MAXN], e_cnt = 1, cg[2], p[MAXN];
int st[MAXN], top, rt, ans, size[MAXN];
unsigned long long H[MAXN];
inline void Add(int x, int y) { e[ ++e_cnt ] = { y, head[ x ] }; head[ x ] = e_cnt; }
inline void Addedge(int x, int y) { Add( x, y ); Add( y, x ); }
inline void Findroot(int x, int fa = 0)
{
size[ x ] = 1;
for( int i = head[ x ] ; i ; i = e[ i ].nxt )
if( e[ i ].to ^ fa )
Findroot( e[ i ].to, x ), p[ x ] = max( p[ x ], size[ e[ i ].to ] ), size[ x ] += size[ e[ i ].to ];
p[ x ] = max( p[ x ], n - size[ x ] );
if( p[ x ] < p[ cg[ 0 ] ] ) cg[ 0 ] = x, cg[ 1 ] = 0;
else if( p[ x ] == p[ cg[ 0 ] ] ) cg[ 1 ] = x;
}
inline int qpow(int x, int y) { int ret = 1; for( ; y ; y >>= 1, x = 1LL * x * x % mod ) if( y & 1 ) ret = 1LL * ret * x % mod; return ret; }
inline int inv(int x) { return qpow( x, mod - 2 ); }
inline int C(int n, int m) { int p = 1, q = 1; for( int i = 1 ; i <= m ; i++ ) p = 1LL * p * ( n - i + 1 ) % mod, q = 1LL * q * i % mod; return 1LL * p * inv( q ) % mod; }
inline int Cal(int n, int m) { return C( n % mod + m - 1, n ); }
inline bool cmp(int x, int y) { return H[ x ] > H[ y ]; }
inline void Dfs(int x, int fa = 0)
{
for( int i = head[ x ] ; i ; i = e[ i ].nxt )
if( e[ i ].to ^ fa )
Dfs( e[ i ].to, x );
top = 0; H[ x ] = 19; f[ x ] = g[ x ] = 1;
for( int i = head[ x ] ; i ; i = e[ i ].nxt )
if( e[ i ].to ^ fa )
st[ ++top ] = e[ i ].to;
sort( st + 1, st + top + 1, cmp );
for( int l = 1, r = 2 ; l <= top ; l = r )
{
while( r <= top && H[ st[ r ] ] == H[ st[ l ] ] ) r++;
f[ x ] = 1LL * f[ x ] * Cal( r - l, f[ st[ l ] ] + g[ st[ l ] ] ) % mod;
g[ x ] = 1LL * g[ x ] * Cal( r - l, f[ st[ l ] ] ) % mod;
}
for( int i = 1 ; i <= top ; i++ ) H[ x ] = H[ x ] * 9875321 + H[ st[ i ] ] * 17;
}
int main()
{
#ifdef wxh010910
freopen( "data.in", "r", stdin );
#endif
n = read(); p[ 0 ] = mod;
for( int i = 1 ; i < n ; i++ ) Addedge( read(), read() );
Findroot( 1 );
if( p[ cg[ 1 ] ] == p[ cg[ 0 ] ] )
{
for( int i = head[ cg[ 0 ] ] ; i ; i = e[ i ].nxt )
if( e[ i ].to == cg[ 1 ] )
{
e[ i ].to = e[ i ^ 1 ].to = rt = n + 1;
break;
}
Add( n + 1, cg[ 0 ] ); Add( n + 1, cg[ 1 ] );
}
else rt = cg[ 0 ];
Dfs( rt );
if( p[ cg[ 1 ] ] == p[ cg[ 0 ] ] )
{
if( H[ cg[ 1 ] ] == H[ cg[ 0 ] ] ) ans = ( 1LL * f[ cg[ 0 ] ] * g[ cg[ 1 ] ] % mod + Cal( 2, f[ cg[ 0 ] ] ) ) % mod;
else ans = ( 1LL * f[ cg[ 0 ] ] * g[ cg[ 1 ] ] % mod + 1LL * f[ cg[ 1 ] ] * g[ cg[ 0 ] ] % mod + 1LL * f[ cg[ 0 ] ] * f[ cg[ 1 ] ] % mod ) % mod;
}
else ans = ( f[ rt ] + g[ rt ] ) % mod;
cout << ans << endl;
}