結果
| 問題 |
No.827 総神童数
|
| コンテスト | |
| ユーザー |
MarcusAureliusAntoninus
|
| 提出日時 | 2019-05-03 22:24:39 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 250 ms / 2,000 ms |
| コード長 | 1,311 bytes |
| コンパイル時間 | 2,231 ms |
| コンパイル使用メモリ | 198,964 KB |
| 最終ジャッジ日時 | 2025-01-07 03:27:16 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 36 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:40:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
40 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
main.cpp:45:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
45 | scanf("%d%d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~
ソースコード
#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;
}
MarcusAureliusAntoninus