Uva10766 Organising the Organisation

题目链接戳这里
基尔霍夫矩阵裸题。构建基尔霍夫矩阵(度数矩阵-邻接矩阵),求他的任意\(n-1\)阶主子式的绝对值即为答案。
这题开始用java写,结果BigInteger太慢Tle了。
后来用c++写了个crt,不知道为什么wa了。
最后用long double强上最后输出转long long,ac了。
注意:给出的边可能有重复的。

#include<cmath>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;

typedef long double ld;
typedef long long ll;
#define maxn (60)
#define eps (1e-7)
int N,M,K; ld A[maxn][maxn]; ll ans;

inline ld guass()
{
	ld ret = 1; bool sign = false;
	for (int i = 1,j;i < N;++i)
	{
		for (j = i;j < N;++j) if (fabs(A[j][i]) > eps) break;
		if (j == N) { sign = true; break; }
		for (int k = 1;k < N;++k) swap(A[i][k],A[j][k]);
		ld inv = A[i][i]; ret *= inv;
		for (j = i;j < N;++j) A[i][j] /= inv;
		for (j = i+1;j < N;++j)
		{
			ld t = A[j][i];
			for (int k = i;k < N;++k) A[j][k] -= t*A[i][k];
		}
	}
	if (sign) ret = 0;
	return fabs(ret);
}

int main()
{
	freopen("10766.in","r",stdin);
	freopen("10766.out","w",stdout);
	while (scanf("%d %d %d",&N,&M,&K) != EOF)
	{
		for (int i = 1;i <= N;++i)
			for (int j = 1;j <= N;++j)
			{
				if (i != j) A[i][j] = 1;
				else A[i][i] = 0;
			}
		while (M--)
		{
			int a,b; scanf("%d %d",&a,&b);
			A[a][b] = A[b][a] = 0;
		}
		for (int i = 1;i <= N;++i)
			for (int j = 1;j <= N;++j)
				if (i != j&&A[i][j] > eps) A[i][i]--;
		printf("%lld\n",(ll)(guass()+0.5));
	}
	fclose(stdin); fclose(stdout);
	return 0;
}
posted @ 2016-12-13 19:33  lmxyy  阅读(255)  评论(0编辑  收藏  举报