Submission #2138492


Source Code Expand

/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-2-26 20:57:11
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>   // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set>
#include <random> // random_device rd; mt19937 mt(rd());
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

const int MAX_SIZE = 1000010;
const long long MOD = 1000000007;

long long inv[MAX_SIZE];
long long fact[MAX_SIZE];
long long factinv[MAX_SIZE];

void init() {
  inv[1] = 1;
  for (int i=2; i<MAX_SIZE; i++) {
    inv[i] = ((MOD - inv[MOD%i]) * (MOD/i))%MOD;
  }
  fact[0] = factinv[0] = 1;
  for (int i=1; i<MAX_SIZE; i++) {
    fact[i] = (i * fact[i-1])%MOD;
    factinv[i] = (inv[i] * factinv[i-1])%MOD;
  }
}

long long C(int n, int k) {
  if (n >=0 && k >= 0 && n-k >= 0) {
    return ((fact[n] * factinv[k])%MOD * factinv[n-k])%MOD;
  }
  return 0;
}

long long power(long long x, long long n) {
  if (n == 0) {
    return 1;
  } else if (n%2 == 1) {
    return (x * power(x, n-1)) % MOD;
  } else {
    long long half = power(x, n/2);
    return (half * half) % MOD;
  }
}

long long gcm(long long a, long long b) {
  if (a < b) {
    return gcm(b, a);
  }
  if (b == 0) return a;
  return gcm(b, a%b);
}

int N;
vector<int> V[5010];
vector<int> children[5010];
int child_num[5010];
int parent[5010];
ll DP[5010][5010];

int calc_child_num(int n)
{
  if (child_num[n] >= 0)
    return child_num[n];
  child_num[n] = 1;
  for (auto x : children[n])
  {
    child_num[n] += calc_child_num(x);
  }
  return child_num[n];
}

int main()
{
  init();
  cin >> N;
  for (auto i = 0; i < N-1; i++)
  {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    V[x].push_back(y);
    V[y].push_back(x);
  }
  queue<int> Q;
  Q.push(0);
  fill(child_num, child_num + 5010, -1);
  fill(parent, parent + 5010, -1);
  parent[0] = -2;
  while (!Q.empty())
  {
    int now = Q.front();
    Q.pop();
    for (auto x : V[now])
    {
      if (parent[x] == -1)
      {
        parent[x] = now;
        children[now].push_back(x);
        Q.push(x);
      }
    }
  }
  calc_child_num(0);
  bool is_there_two_center = false;
  int center = 0;
  while (true)
  {
    bool found_center = true;
#if DEBUG == 1
    cerr << "center = " << center << endl;
#endif
    for (auto x : children[center])
    {
      if (N % 2 == 0 && child_num[x] == N / 2)
      {
        is_there_two_center = true;
        center = x;
        break;
      }
      else if (child_num[x] > N/2)
      {
        center = x;
        found_center = false;
        break;
      }
    }
    if (found_center)
      break;
  }
  if (is_there_two_center)
  {
    ll sq = fact[N / 2];
    cout << (sq * sq) % MOD << endl;
    return 0;
  }
  vector<ll> T;
  T.push_back(0);
  ll nokori = N-1;
  for (auto x : children[center])
  {
    ll n = child_num[x];
    T.push_back(n);
    nokori -= n;
  }
  if (nokori > 0)
    T.push_back(nokori);
  int X = T.size();
  X--;
#if DEBUG == 1
  cerr << "T : ";
  for (auto x : T)
  {
    cerr << x << " ";
  }
  cerr << endl;
#endif
  fill(&DP[0][0], &DP[0][0] + 5010 * 5010, 0);
  DP[0][0] = 1;
  for (auto x = 1; x <= X; x++)
  {
    for (auto i = 0; i <= T[x]; i++)
    {
      for (auto k = 0; k <= N-i; k++)
      {
        ll c = C(T[x], i);
        ll plus = (DP[x - 1][k] * fact[i]) % MOD;
        plus = (plus * ((c * c) % MOD)) % MOD;
#if DEBUG == 1
        cerr << "x = " << x << ", i = " << i << ", k = " << k << endl;
        cerr << "DP[" << k + i << "][" << x << "] += " << plus << endl;
#endif
        DP[x][k + i] += plus;
        DP[x][k + i] %= MOD;
      }
    }
  }
  ll ans = 0;
#if DEBUG == 1
  for (auto x = 1; x <= X; x++)
  {
    for (auto k = 0; k <= N; k++)
    {
      cerr << "DP[" << x << "][" << k << "] = " << DP[x][k] << endl;
    }
  }
#endif

  for (auto k = 0; k <= N; k++)
  {
    if (k%2 == 0)
    {
      ans += (fact[N - k] * DP[X][k]) % MOD;
      ans %= MOD;
    }
    else
    {
      ans += MOD - (fact[N - k] * DP[X][k]) % MOD;
      ans %= MOD;
    }
  }
  cout << ans << endl;
}

Submission Info

Submission Time
Task F - Squirrel Migration
User kazunetakahashi
Language C++14 (Clang 3.8.0)
Score 800
Code Size 4801 Byte
Status AC
Exec Time 716 ms
Memory 220416 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 4
AC × 41
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 1_30.txt, 1_31.txt, 1_32.txt, 1_33.txt, 1_34.txt, 1_35.txt, 1_36.txt
Case Name Status Exec Time Memory
0_00.txt AC 74 ms 220032 KB
0_01.txt AC 74 ms 220032 KB
0_02.txt AC 26 ms 24832 KB
0_03.txt AC 73 ms 220032 KB
1_00.txt AC 319 ms 220416 KB
1_01.txt AC 34 ms 25216 KB
1_02.txt AC 240 ms 220288 KB
1_03.txt AC 342 ms 220288 KB
1_04.txt AC 319 ms 220288 KB
1_05.txt AC 346 ms 220288 KB
1_06.txt AC 397 ms 220288 KB
1_07.txt AC 398 ms 220288 KB
1_08.txt AC 558 ms 220416 KB
1_09.txt AC 716 ms 220416 KB
1_10.txt AC 518 ms 220416 KB
1_11.txt AC 518 ms 220288 KB
1_12.txt AC 397 ms 220288 KB
1_13.txt AC 316 ms 220288 KB
1_14.txt AC 333 ms 220288 KB
1_15.txt AC 341 ms 220288 KB
1_16.txt AC 358 ms 220288 KB
1_17.txt AC 373 ms 220288 KB
1_18.txt AC 381 ms 220288 KB
1_19.txt AC 388 ms 220288 KB
1_20.txt AC 389 ms 220288 KB
1_21.txt AC 399 ms 220288 KB
1_22.txt AC 401 ms 220288 KB
1_23.txt AC 409 ms 220288 KB
1_24.txt AC 425 ms 220288 KB
1_25.txt AC 461 ms 220288 KB
1_26.txt AC 462 ms 220288 KB
1_27.txt AC 501 ms 220288 KB
1_28.txt AC 555 ms 220416 KB
1_29.txt AC 26 ms 24832 KB
1_30.txt AC 26 ms 24832 KB
1_31.txt AC 34 ms 25088 KB
1_32.txt AC 35 ms 25088 KB
1_33.txt AC 34 ms 25088 KB
1_34.txt AC 34 ms 25088 KB
1_35.txt AC 34 ms 25088 KB
1_36.txt AC 34 ms 25088 KB