結果

問題 No.310 2文字しりとり
ユーザー skyleeskylee
提出日時 2018-12-21 11:55:04
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 656 ms / 6,000 ms
コード長 2,451 bytes
コンパイル時間 1,356 ms
コンパイル使用メモリ 45,532 KB
実行使用メモリ 65,748 KB
最終ジャッジ日時 2023-10-26 01:20:18
合計ジャッジ時間 5,406 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 1 ms
4,372 KB
testcase_02 AC 1 ms
4,372 KB
testcase_03 AC 1 ms
4,372 KB
testcase_04 AC 1 ms
4,372 KB
testcase_05 AC 1 ms
4,372 KB
testcase_06 AC 1 ms
4,372 KB
testcase_07 AC 1 ms
4,372 KB
testcase_08 AC 1 ms
4,372 KB
testcase_09 AC 1 ms
4,372 KB
testcase_10 AC 1 ms
4,372 KB
testcase_11 AC 2 ms
4,372 KB
testcase_12 AC 2 ms
4,372 KB
testcase_13 AC 1 ms
4,372 KB
testcase_14 AC 1 ms
4,372 KB
testcase_15 AC 2 ms
4,372 KB
testcase_16 AC 2 ms
4,372 KB
testcase_17 AC 1 ms
4,372 KB
testcase_18 AC 2 ms
5,372 KB
testcase_19 AC 2 ms
5,360 KB
testcase_20 AC 3 ms
5,372 KB
testcase_21 AC 347 ms
65,748 KB
testcase_22 AC 432 ms
65,748 KB
testcase_23 AC 656 ms
65,748 KB
testcase_24 AC 624 ms
65,748 KB
testcase_25 AC 380 ms
65,748 KB
testcase_26 AC 270 ms
28,152 KB
testcase_27 AC 303 ms
34,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<ctime>
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstdlib>
#include<algorithm>
inline int getint() {
	register char ch;
	while(!isdigit(ch=getchar()));
	register int x=ch^'0';
	while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
	return x;
}
const int N=4001,mod=1e9+7;
int mat[N][N],fac[N],node[N],in[N],out[N];
inline void exgcd(const int &a,const int &b,int &x,int &y) {
	if(!b) {
		x=1,y=0;
		return;
	}
	exgcd(b,a%b,y,x);
	y-=a/b*x;
}
inline int inv(const int &x) {
	int ret,tmp;
	exgcd(x,mod,ret,tmp);
	return (ret%mod+mod)%mod;
}
inline void fail() {
	puts("0");
	exit(0);
}
inline int matrix_tree(const int &n) {
	for(register int i=1;i<=n;i++) {
		for(register int j=1;j<=n;j++) {
			mat[i][j]=(mat[i][j]%mod+mod)%mod;
		}
	}
	int ret=1;
	for(register int i=1;i<=n;i++) {
		std::vector<int> v;
		for(register int j=i;j<=n;j++) {
			if(mat[j][i]) v.push_back(j);
		}
		if(v.empty()) return 0;
		const int p=v[rand()%v.size()];
		if(i!=p) {
			ret=(mod-ret)%mod;
			for(register int j=1;j<=n;j++) {
				std::swap(mat[i][j],mat[p][j]);
			}
		}
		ret=1ll*ret*mat[i][i]%mod;
		const int t=inv(mat[i][i]);
		for(register int j=i;j<=n;j++) {
			mat[i][j]=1ll*mat[i][j]*t%mod;
		}
		for(register int j=i+1;j<=n;j++) {
			if(mat[j][i]==0) continue;
			const int t=mat[j][i];
			for(register int k=i;k<=n;k++) {
				mat[j][k]=(mat[j][k]-1ll*mat[i][k]*t%mod+mod)%mod;
			}
		}
	}
	return ret;
}
int main() {
	srand(time(NULL));
	const int n=getint(),m=getint();
	for(register int i=fac[0]=1;i<=n;i++) {
		fac[i]=1ll*fac[i-1]*i%mod;
	}
	for(register int i=1;i<=n;i++) {
		in[i]=out[i]=mat[i][i]=n;
		for(register int j=1;j<=n;j++) {
			mat[i][j]--;
		}
	}
	for(register int i=0;i<m;i++) {
		const int x=getint(),y=getint();
		in[y]--;
		out[x]--;
		mat[x][y]++;
		mat[y][y]--;
	}
	int st=-1,en=-1,x=0;
	for(register int i=1;i<=n;i++) {
		if(in[i]==0&&out[i]==0) continue;
		node[++x]=i;
		if(std::abs(in[i]-out[i])>1) fail();
		if(out[i]==in[i]+1) {
			if(st!=-1) fail();
			st=i;
		}
		if(out[i]==in[i]-1) {
			if(en!=-1) fail();
			en=i;
		}
	}
	if(x==0) {
		puts("1");
		return 0;
	}
	int ans=1;
	if(st!=-1&&en!=-1) {
		in[st]++;
		out[en]++;
		mat[en][st]--;
		mat[st][st]++;
	} else {
		ans=n*n-m;
	}
	for(register int i=1;i<=x;i++) {
		mat[i][i]=mat[node[i]][node[i]];
	}
	for(register int i=1;i<=x;i++) {
		ans=1ll*ans*fac[out[node[i]]-1]%mod;
	}
	ans=1ll*ans*matrix_tree(x-1)%mod;
	printf("%d\n",ans);
	return 0;
}
0