Submission #3272509
Source Code Expand
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>
#include <time.h>
#include <tuple>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;
#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e16;
const ll MOD = 1e9 + 7;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,N) for(int i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
ll sq(ll num) { return num*num; }
ll mod_pow(ll x, ll n) {
if (n == 0)return 1;
if (n == 1)return x%MOD;
ll res = sq(mod_pow(x, n / 2));
res %= MOD;
if (n % 2 == 1) {
res *= x;
res %= MOD;
}
return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a*b % MOD; }
int n;
ll num[1001][1001];
void make_pascal(ll n) {
num[0][0] = 1;
rep(i, n+1) {
rep(j, n+1) {
if (i != 0)num[i][j] += num[i - 1][j];
num[i][j] %= MOD;
if (j != 0)num[i][j] += num[i][j - 1];
num[i][j] %= MOD;
}
}
}
ll nCk_pas(ll n, ll k) {
return num[n - k][k];
}
vector<int> G[1000];
ll dp[1000][1000];
ll en[1000][1000];
void solve(int now,int to){
if(dp[now][to] != -1)return;
dp[now][to] = 1;
rep(i,G[to].size()){
int tto = G[to][i];
if(tto == now)continue;
solve(to,tto);
en[now][to] += (en[to][tto]+1);
}
ll sum = en[now][to];
rep(i,G[to].size()){
int tto = G[to][i];
if(tto == now)continue;
dp[now][to] *= dp[to][tto];
dp[now][to] %= MOD;
dp[now][to] *= (ll)(nCk_pas(sum,en[to][tto]+1));
dp[now][to] %= MOD;
// cout << "!" << sum << " " << en[to][tto] << endl;
// cout << now << " " << to << " " << dp[now][to] << endl;
sum -= (en[to][tto]+1);
}
}
bool f[1000][1000];
int main(){
cin >> n;
rep(i,n-1){
int a,b;
cin >> a >> b;
a--; b--;
G[a].push_back(b);
G[b].push_back(a);
f[a][b] = true;
f[b][a] = true;
}
make_pascal(1000);
rep(i,1000)rep(j,1000)dp[i][j] = -1;
rep(i,n){
rep(j,G[i].size()){
int to = G[i][j];
if(f[i][to])solve(i,to);
}
}
ll ans = 0;
rep(i,n){
rep(j,G[i].size()){
int to = G[i][j];
if(i < to){
ll num = (dp[i][to] * dp[to][i])%MOD;
num = (num * (ll)(nCk_pas(en[i][to]+en[to][i],en[i][to])))%MOD;
ans = (ans + num)%MOD;
}
}
}
cout << ans << endl;
}
Submission Info
Submission Time |
|
Task |
N - 木 |
User |
jimmy |
Language |
C++14 (GCC 5.4.1) |
Score |
5 |
Code Size |
2915 Byte |
Status |
AC |
Exec Time |
28 ms |
Memory |
24448 KB |
Judge Result
Set Name |
All |
Score / Max Score |
5 / 5 |
Status |
|
Set Name |
Test Cases |
All |
00, 01, 02, 03, 04, 05, 06, 90, 91 |
Case Name |
Status |
Exec Time |
Memory |
00 |
AC |
14 ms |
24320 KB |
01 |
AC |
28 ms |
24192 KB |
02 |
AC |
14 ms |
24448 KB |
03 |
AC |
14 ms |
24448 KB |
04 |
AC |
14 ms |
24448 KB |
05 |
AC |
14 ms |
24448 KB |
06 |
AC |
14 ms |
24448 KB |
90 |
AC |
11 ms |
16512 KB |
91 |
AC |
11 ms |
16512 KB |