SP104 HIGH - Highways

vjudge
luogu

题意

就是要你求无向图的生成树个数。\(n\le 12\),保证答案不爆\(long long\)

sol

矩阵树定理直接上。
如果怕掉精可以写整数意义下的高斯消元,需要辗转相除,复杂度多个\(\log\)

code

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
int gi()
{
	int x=0,w=1;char ch=getchar();
	while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
	if (ch=='-') w=0,ch=getchar();
	while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
	return w?x:-x;
}
int n,m;ll a[20][20],ans;
int main()
{
	int T=gi();
	while (T--)
	{
		n=gi();m=gi();ans=1;
		memset(a,0,sizeof(a));
		while (m--)
		{
			int u=gi(),v=gi();
			a[u][u]++;a[v][v]++;
			a[u][v]--;a[v][u]--;
		}
		for (int i=2;i<=n;++i)
		{
			for (int j=i+1;j<=n;++j)
				while (a[j][i])
				{
					ll t=a[i][i]/a[j][i];
					for (int k=i;k<=n;++k) a[i][k]-=t*a[j][k],swap(a[i][k],a[j][k]);
					ans=-ans;
				}
			ans*=a[i][i];
		}
		printf("%lld\n",ans);
	}
	return 0;
}
posted @ 2018-04-21 10:15  租酥雨  阅读(204)  评论(0编辑  收藏  举报