結果

問題 No.827 総神童数
ユーザー yakkiyakki
提出日時 2020-08-01 07:54:41
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 222 ms / 2,000 ms
コード長 1,815 bytes
コンパイル時間 1,160 ms
コンパイル使用メモリ 124,696 KB
実行使用メモリ 29,292 KB
最終ジャッジ日時 2023-09-21 19:48:37
合計ジャッジ時間 8,206 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
10,072 KB
testcase_01 AC 10 ms
9,972 KB
testcase_02 AC 10 ms
10,096 KB
testcase_03 AC 11 ms
9,868 KB
testcase_04 AC 11 ms
10,048 KB
testcase_05 AC 11 ms
10,044 KB
testcase_06 AC 10 ms
10,140 KB
testcase_07 AC 11 ms
10,100 KB
testcase_08 AC 10 ms
10,056 KB
testcase_09 AC 176 ms
29,292 KB
testcase_10 AC 65 ms
16,464 KB
testcase_11 AC 13 ms
10,656 KB
testcase_12 AC 32 ms
13,056 KB
testcase_13 AC 140 ms
25,924 KB
testcase_14 AC 15 ms
10,708 KB
testcase_15 AC 61 ms
16,592 KB
testcase_16 AC 140 ms
22,816 KB
testcase_17 AC 168 ms
25,888 KB
testcase_18 AC 76 ms
17,788 KB
testcase_19 AC 222 ms
20,848 KB
testcase_20 AC 140 ms
18,144 KB
testcase_21 AC 99 ms
16,156 KB
testcase_22 AC 153 ms
18,536 KB
testcase_23 AC 11 ms
9,996 KB
testcase_24 AC 169 ms
19,612 KB
testcase_25 AC 133 ms
17,248 KB
testcase_26 AC 177 ms
19,592 KB
testcase_27 AC 110 ms
16,512 KB
testcase_28 AC 110 ms
16,456 KB
testcase_29 AC 86 ms
15,132 KB
testcase_30 AC 31 ms
11,424 KB
testcase_31 AC 67 ms
14,044 KB
testcase_32 AC 64 ms
13,804 KB
testcase_33 AC 201 ms
20,384 KB
testcase_34 AC 180 ms
19,592 KB
testcase_35 AC 70 ms
14,060 KB
testcase_36 AC 95 ms
15,560 KB
testcase_37 AC 135 ms
17,224 KB
testcase_38 AC 195 ms
20,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<bitset>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<functional>
#include<cstdio>
#include<cstdlib>
using namespace std;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
#define MOD 1000000007
//#define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592

const double EPS = 1e-10;

using ll = long long;
using Pi = pair<int,int>;
using Pl = pair<ll,ll>;

struct Combination{
    vector<long long> _fac,_finv,_inv;
    const int m = MOD;

    Combination(int sz) : _fac(sz),_finv(sz),_inv(sz){
        _fac[0] = _fac[1] = 1;
        _finv[0] = _finv[1] = 1;
        _inv[1] = 1;
        for(int i = 2; i <= sz; i++){
            _fac[i] = _fac[i-1]*i%m;
            _inv[i] = m-_inv[m%i]*(m/i)%m;
            _finv[i] = _finv[i-1]*_inv[i]%m;
        }
    }

    inline long long fac(int n){return _fac[n];}
    inline long long inv(int n){return _inv[n];}
    inline long long finv(int n){return _finv[n];}

    long long comb(int n,int k){
        if(n < k) return 0;
        if(n < 0 || k < 0) return 0;
        return _fac[n]*(_finv[k]*_finv[n-k]%m)%m;
    }
};

int n;
vector<vector<int>> G;
ll ans = 0;
Combination C(300000);

void dfs(int u,int p,int depth){
    ans += C.comb(n,depth+1)*C.fac(depth)%MOD*C.fac(n-depth-1)%MOD;
    ans %= MOD;
    for(int v : G[u]){
        if(v == p) continue;
        dfs(v,u,depth+1);
    }
}

int main(){
    cin >> n;
    G.resize(n);
    rep(i,n-1){
        int u,v; cin >> u >> v;
        u--; v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(0,-1,0);
    cout << ans << endl;
}
0