結果

問題 No.758 VVVVV
ユーザー pekempeypekempey
提出日時 2018-12-06 00:46:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 1,136 bytes
コンパイル時間 814 ms
コンパイル使用メモリ 71,924 KB
実行使用メモリ 11,332 KB
最終ジャッジ日時 2024-04-22 02:59:32
合計ジャッジ時間 2,241 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,888 KB
testcase_01 AC 3 ms
5,760 KB
testcase_02 AC 3 ms
5,888 KB
testcase_03 AC 73 ms
11,136 KB
testcase_04 AC 33 ms
8,192 KB
testcase_05 AC 24 ms
7,552 KB
testcase_06 AC 21 ms
7,424 KB
testcase_07 AC 71 ms
11,008 KB
testcase_08 AC 37 ms
8,704 KB
testcase_09 AC 13 ms
6,912 KB
testcase_10 AC 24 ms
7,808 KB
testcase_11 AC 74 ms
11,264 KB
testcase_12 AC 19 ms
7,296 KB
testcase_13 AC 23 ms
7,552 KB
testcase_14 AC 13 ms
6,912 KB
testcase_15 AC 9 ms
6,400 KB
testcase_16 AC 11 ms
6,656 KB
testcase_17 AC 64 ms
10,496 KB
testcase_18 AC 32 ms
8,192 KB
testcase_19 AC 72 ms
11,136 KB
testcase_20 AC 20 ms
7,296 KB
testcase_21 AC 21 ms
7,552 KB
testcase_22 AC 72 ms
11,332 KB
testcase_23 AC 2 ms
5,888 KB
testcase_24 AC 3 ms
5,760 KB
testcase_25 AC 3 ms
5,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

// https://www.geeksforgeeks.org/combinatorics-ordered-trees/

using namespace std;

const long long mod = 1000000007;

vector<int> g[100000];
long long F[100001];
long long IF[100001];
long long inv[100001];
int L;

void dfs(int u, int p) {
    bool l = true;
    for (int v : g[u]) if (v != p) {
        dfs(v, u);
        l = false;
    }
    L += l;
}

long long C(int n, int r) {
    if (n < 0 || r < 0 || n < r) return 0;
    return F[n] * IF[r] % mod * IF[n - r] % mod;
}

int main() {
    int n;
    cin >> n;
    if (n == 1) {
        cout << 1 << endl;
        return 0;
    }
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(0, -1);
    inv[1] = 1;
    for (int i = 2; i <= n; i++) {
        inv[i] = inv[mod % i] * (mod - mod / i) % mod;
    }
    F[0] = 1;
    IF[0] = 1;
    for (int i = 1; i <= n; i++) {
        F[i] = F[i - 1] * i % mod;
        IF[i] = IF[i - 1] * inv[i] % mod;
    }
    cout << C(n - 1, L) * C(n - 1, L - 1) % mod * inv[n - 1] % mod << endl;
}
0