UVA 10766 - Organising the Organisation(生成树计数)

UVA10766 生成树计数裸题! 纯套模板了。 不会生成树计数可以去看看Matrix-Tree定理。 本题要用long double才能AC。

/* ***
Author :kuangbin
Created Time :2015/1/27 22:43:02
File Name :I.cpp
************************************************ */

#include <stdio.h>

#include <string.h>

#include

#include

#include

#include

#include

#include

#include

#include <math.h>

#include <stdlib.h>

#include <time.h>
using namespace std;

const double eps = 1e-8;
const int MAXN = 110;
int sgn(long double x){
if(fabs(x) < eps)return 0;
if(x < 0)return -1;
else return 1;
}
long double b[MAXN][MAXN];
long double det(long double a[][MAXN],int n){
int i, j, k, sign = 0;
long double ret = 1;
for(i = 0;i < n;i++)
for(j = 0;j < n;j++)
b[i][j] = a[i][j];
for(i = 0;i < n;i++){
if(sgn(b[i][i]) == 0){
for(j = i + 1; j < n;j++)
if(sgn(b[j][i]) != 0)
break;
if(j == n)return 0;
for(k = i;k < n;k++)
swap(b[i][k],b[j][k]);
sign++;
}
ret = b[i][i];
for(k = i + 1;k < n;k++)
b[i][k]/=b[i][i];
for(j = i+1;j < n;j++)
for(k = i+1;k < n;k++)
b[j][k] -= b[j][i]
b[i][k];
}
if(sign & 1)ret = -ret;
return ret;
}
long double a[MAXN][MAXN];
int g[MAXN][MAXN];
int main(){
int n,m;
int u,v;
int k;
while(scanf(“%d%d%d”,&n,&m,&k) == 3){
memset(g,0,sizeof(g));
while(m–){
scanf(“%d%d”,&u,&v);
u–;v–;
g[u][v] = g[v][u] = 1;
}
memset(a,0,sizeof(a));
for(int i = 0;i < n;i++)
for(int j = 0;j < n;j++)
if(i != j && g[i][j] == 0){
a[i][i]++;
a[i][j] = -1;
}
double ans = det(a,n-1);
printf(“%.0lf\n”,ans);
}
return 0;
}

------ 本文结束------
  • 本文作者: kuangbin
  • 本文链接: 568.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
0%