結果

問題 No.758 VVVVV
ユーザー 0w1
提出日時 2018-12-10 02:22:01
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 1,138 bytes
コンパイル時間 1,823 ms
コンパイル使用メモリ 195,792 KB
最終ジャッジ日時 2025-01-06 18:49:20
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int M = int(1e9 + 7);

signed main() {
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  if (N == 1) cout << 1 << endl, exit(0);

  vector<int> deg(N);
  for (int i = 0; i + 1 < N; ++i) {
    int X, Y;
    cin >> X >> Y;
    --X, --Y;
    ++deg[X], ++deg[Y];
  }

  int lcnt = count(deg.begin(), deg.end(), 1) - (deg[0] == 1);

  vector<int> fac(N);
  for (int i = fac[0] = 1; i < N; ++i) fac[i] = 1LL * fac[i - 1] * i % M;

  auto int_pow = [](int x, int e) {
    int r = 1;
    for (int i = e; i; i /= 2) {
      if (i & 1) r = 1LL * r * x % M;
      x = 1LL * x * x % M;
    }
    return r;
  };

  vector<int> ifac(N);
  ifac[N - 1] = int_pow(fac[N - 1], M - 2);
  for (int i = N - 1; i; --i) ifac[i - 1] = 1LL * ifac[i] * i % M;
  vector<int> inv(N);
  for (int i = 1; i < N; ++i) inv[i] = 1LL * ifac[i] * fac[i - 1] % M;

  int a = 1LL * fac[N - 1] * ifac[lcnt - 1] % M * ifac[N - lcnt] % M;
  int b = 1LL * fac[N - 2] * ifac[lcnt - 1] % M * ifac[N - lcnt - 1] % M;
  int ans = 1LL * a * b % M * inv[lcnt] % M;
  if (ans < 0) ans += M;
  cout << ans << endl;

  return 0;
}
0