結果

問題 No.827 総神童数
ユーザー okok
提出日時 2019-05-04 00:06:00
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 121 ms / 2,000 ms
コード長 2,129 bytes
コンパイル時間 1,110 ms
コンパイル使用メモリ 106,132 KB
実行使用メモリ 21,952 KB
最終ジャッジ日時 2023-08-20 12:22:54
合計ジャッジ時間 4,472 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
12,972 KB
testcase_01 AC 5 ms
14,728 KB
testcase_02 AC 5 ms
13,284 KB
testcase_03 AC 4 ms
13,708 KB
testcase_04 AC 5 ms
12,956 KB
testcase_05 AC 5 ms
13,240 KB
testcase_06 AC 5 ms
14,356 KB
testcase_07 AC 5 ms
12,940 KB
testcase_08 AC 5 ms
13,680 KB
testcase_09 AC 96 ms
20,964 KB
testcase_10 AC 30 ms
16,008 KB
testcase_11 AC 6 ms
13,056 KB
testcase_12 AC 15 ms
14,376 KB
testcase_13 AC 78 ms
19,404 KB
testcase_14 AC 7 ms
14,820 KB
testcase_15 AC 29 ms
15,868 KB
testcase_16 AC 76 ms
19,916 KB
testcase_17 AC 93 ms
20,708 KB
testcase_18 AC 35 ms
16,508 KB
testcase_19 AC 121 ms
21,952 KB
testcase_20 AC 79 ms
20,064 KB
testcase_21 AC 54 ms
17,952 KB
testcase_22 AC 93 ms
20,576 KB
testcase_23 AC 5 ms
13,576 KB
testcase_24 AC 104 ms
20,836 KB
testcase_25 AC 72 ms
19,644 KB
testcase_26 AC 107 ms
21,148 KB
testcase_27 AC 59 ms
19,036 KB
testcase_28 AC 60 ms
18,724 KB
testcase_29 AC 43 ms
17,376 KB
testcase_30 AC 15 ms
14,556 KB
testcase_31 AC 31 ms
16,456 KB
testcase_32 AC 30 ms
16,976 KB
testcase_33 AC 114 ms
21,760 KB
testcase_34 AC 106 ms
20,884 KB
testcase_35 AC 33 ms
17,580 KB
testcase_36 AC 48 ms
17,832 KB
testcase_37 AC 72 ms
19,112 KB
testcase_38 AC 113 ms
21,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<queue>

using namespace std;

#define int long long
#define rep(i,n) for(int i = 0; i < (n); i++)
#define INF ((long long)1e18)
#define MOD ((int)1e9+7)
#define endl "\n"

#define yn(f) ((f)?"Yes":"No")
#define YN(f) ((f)?"YES":"NO")

#define MAX 210000


long long fac[MAX], mmi[MAX];
int used[MAX], temp[MAX];

int N;
const int MAX_VAL = MAX;

vector<int> G[MAX];

void factorial_mod(int Z){
	 fac[0]=fac[1]=1;
	for(long long i = 2; i < Z; fac[i]%=MOD,i++)
		fac[i] = fac[i-1]*(i%MOD);
}

long long power_mod(long long x, long long n){
	long long ans = 1;
	for(;n;n>>=1,x*=x,ans%=MOD,x%=MOD)
		if(n&1)ans*=x;
	return ans%MOD;
}

void exgcd(int a, int b, int &x, int &y){
	if(b == 0){
		x = 1;
		y = 0;
		return ;
	}
	exgcd(b,a%b,y,x);
	y -= a/b * x;
}

void modular_multiplicatibe_inverse(int Z){
	int x, y;  
	exgcd(fac[Z-1],MOD,x,y);
	mmi[Z-1] = x; 
	for(long long i = Z-2; i >= 0; mmi[i]%=MOD,i--)
		mmi[i] = mmi[i+1]*((i+1)%MOD);
}

long long combination(long long n, long long r){
	return fac[n]*(mmi[r]*mmi[n-r]%MOD)%MOD;
}

void dfs(int depth = 0, int num = 1){
	if(used[num]) return;
	used[num] = true;
	
	temp[depth]++;
	
	for(int i = 0; i < G[num].size(); i++){
		if(used[G[num][i]]) continue;
		dfs(depth+1, G[num][i]);
	}
}

void bfs(){
	queue<int> Q;
	
	Q.push(1);
	used[1] = 1;
	
	while(!Q.empty()){
		int num = Q.front(); Q.pop();
		
		temp[used[num]-1]++;
		
		for(int i = 0; i < G[num].size(); i++){
			if(used[G[num][i]]) continue;
			if(!used[G[num][i]]){
				Q.push(G[num][i]);
				used[G[num][i]] = used[num] + 1;
			}
		}
	}
}
signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	
	int u, v;
	int ans = 0;
	
	cin>>N;
	
	factorial_mod(N+10);
	modular_multiplicatibe_inverse(N+10);
	
	for(int i = 0; i < N-1; i++){
		cin>>u>>v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	
	bfs();
	
	for(int i = 0; i < N; i++){
		if(!temp[i]) break;
		ans += temp[i]*combination(N,i+1)%MOD*fac[i]%MOD*fac[N-i-1]%MOD;
		ans %= MOD;
	}
	
	cout<<ans<<endl;

	return 0;
}
0