結果

問題 No.827 総神童数
ユーザー MarcusAureliusAntoninusMarcusAureliusAntoninus
提出日時 2019-05-03 22:24:39
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 185 ms / 2,000 ms
コード長 1,311 bytes
コンパイル時間 2,275 ms
コンパイル使用メモリ 205,172 KB
実行使用メモリ 50,728 KB
最終ジャッジ日時 2023-08-30 06:12:24
合計ジャッジ時間 7,851 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
26,940 KB
testcase_01 AC 32 ms
26,936 KB
testcase_02 AC 32 ms
27,004 KB
testcase_03 AC 31 ms
26,984 KB
testcase_04 AC 33 ms
26,912 KB
testcase_05 AC 31 ms
27,112 KB
testcase_06 AC 33 ms
26,928 KB
testcase_07 AC 32 ms
26,928 KB
testcase_08 AC 31 ms
26,988 KB
testcase_09 AC 161 ms
50,728 KB
testcase_10 AC 65 ms
34,876 KB
testcase_11 AC 35 ms
27,444 KB
testcase_12 AC 46 ms
30,444 KB
testcase_13 AC 128 ms
46,344 KB
testcase_14 AC 33 ms
27,440 KB
testcase_15 AC 61 ms
34,496 KB
testcase_16 AC 133 ms
44,720 KB
testcase_17 AC 151 ms
48,648 KB
testcase_18 AC 80 ms
36,448 KB
testcase_19 AC 184 ms
46,188 KB
testcase_20 AC 127 ms
41,068 KB
testcase_21 AC 100 ms
37,396 KB
testcase_22 AC 144 ms
42,364 KB
testcase_23 AC 32 ms
27,072 KB
testcase_24 AC 169 ms
43,928 KB
testcase_25 AC 122 ms
39,656 KB
testcase_26 AC 162 ms
44,236 KB
testcase_27 AC 111 ms
38,208 KB
testcase_28 AC 112 ms
38,400 KB
testcase_29 AC 87 ms
35,808 KB
testcase_30 AC 46 ms
29,396 KB
testcase_31 AC 77 ms
33,772 KB
testcase_32 AC 68 ms
33,560 KB
testcase_33 AC 179 ms
45,536 KB
testcase_34 AC 165 ms
43,880 KB
testcase_35 AC 70 ms
34,024 KB
testcase_36 AC 95 ms
36,956 KB
testcase_37 AC 121 ms
39,824 KB
testcase_38 AC 185 ms
45,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

struct Node {
	int depth{-1};
	std::list<int> edge;
	bool visited{};
};

constexpr long long mod{1'000'000'007};
constexpr long long max{1000'000};

std::vector<Node> graph;
long long fac[max + 1], finv[max + 1], inv[max + 1];

void dfs(int, int);

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i <= max; i++){
        fac[i] = fac[i - 1] * i % mod;
        inv[i] = mod - inv[mod%i] * (mod / i) % mod;
        finv[i] = finv[i - 1] * inv[i] % mod;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

int main()
{
	COMinit();
	int N;
	scanf("%d", &N);
	graph.resize(N);
	for (int i{}; i < N - 1; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		u--; v--;
		graph[u].edge.push_back(v);
		graph[v].edge.push_back(u);
	}
	dfs(0, 0);
	long long ans{};
	for (auto& e: graph)
		ans = (ans + COM(N, e.depth + 1) * fac[e.depth] % mod * fac[N - e.depth - 1] % mod) % mod;
	printf("%lld\n", ans);

	return 0;
}

void dfs(int index, int depth)
{
	if (graph[index].depth >= 0) return;
	graph[index].depth = depth;
	for (auto& e: graph[index].edge)
		dfs(e, depth + 1);
	return;
}
0