結果

問題 No.827 総神童数
ユーザー okok
提出日時 2019-05-04 00:00:37
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 131 ms / 2,000 ms
コード長 2,233 bytes
コンパイル時間 857 ms
コンパイル使用メモリ 80,044 KB
実行使用メモリ 21,748 KB
最終ジャッジ日時 2023-08-30 07:06:06
合計ジャッジ時間 4,847 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
13,572 KB
testcase_01 AC 5 ms
13,612 KB
testcase_02 AC 6 ms
13,572 KB
testcase_03 AC 5 ms
13,632 KB
testcase_04 AC 5 ms
13,888 KB
testcase_05 AC 5 ms
13,560 KB
testcase_06 AC 5 ms
13,684 KB
testcase_07 AC 5 ms
13,620 KB
testcase_08 AC 6 ms
13,632 KB
testcase_09 AC 107 ms
20,904 KB
testcase_10 AC 34 ms
15,896 KB
testcase_11 AC 6 ms
13,664 KB
testcase_12 AC 17 ms
14,560 KB
testcase_13 AC 81 ms
19,420 KB
testcase_14 AC 7 ms
13,820 KB
testcase_15 AC 31 ms
15,664 KB
testcase_16 AC 82 ms
19,552 KB
testcase_17 AC 94 ms
20,504 KB
testcase_18 AC 40 ms
16,392 KB
testcase_19 AC 126 ms
21,748 KB
testcase_20 AC 82 ms
19,808 KB
testcase_21 AC 53 ms
17,916 KB
testcase_22 AC 94 ms
20,108 KB
testcase_23 AC 5 ms
13,664 KB
testcase_24 AC 103 ms
20,868 KB
testcase_25 AC 72 ms
19,060 KB
testcase_26 AC 110 ms
21,160 KB
testcase_27 AC 64 ms
18,364 KB
testcase_28 AC 63 ms
18,232 KB
testcase_29 AC 52 ms
17,316 KB
testcase_30 AC 17 ms
14,752 KB
testcase_31 AC 39 ms
16,348 KB
testcase_32 AC 36 ms
16,472 KB
testcase_33 AC 131 ms
21,604 KB
testcase_34 AC 112 ms
20,880 KB
testcase_35 AC 37 ms
16,556 KB
testcase_36 AC 52 ms
17,720 KB
testcase_37 AC 73 ms
18,996 KB
testcase_38 AC 114 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++){
		// cout<<i<<" "<<temp[i]<<" "<<ans<<endl;
		if(!temp[i]) break;
		ans += temp[i]*combination(N,i+1)%MOD*fac[i]%MOD*fac[N-i-1]%MOD;
		ans %= MOD;
		// cout<<combination(N,i+1)*fac[i]%MOD*fac[N-i-1]<<endl;;
	}
	
	cout<<ans<<endl;

	return 0;
}
0