1. 程式人生 > >spoj HIGH(矩陣樹定理)

spoj HIGH(矩陣樹定理)

題意:給定無向圖的邊,求構成生成樹的方案數

這個是個結論,在鄰接矩陣的基礎上將a[i][i]定義為i的度數的相反數,然後對這個矩陣求n-1階子式就可以了。。證明網上一堆,比較長。。

順便get行列式計算姿勢。。

 

 

/*
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神獸保佑,程式碼無bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 15
#define nm 10005 
#define pi 3.1415926535897931
using namespace std;
const ll inf=1e9+7;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}
 
 
 





int n,m,_x,_y;
__int128 a[NM][NM];

void cal(){
    __int128 ans=1;
    inc(i,1,n)
	inc(j,i+1,n){
	for(int x=i,y=j;a[y][i];swap(x,y)){
	    __int128 t=a[x][i]/a[y][i];
	    inc(k,i,n)a[x][k]-=t*a[y][k];
	}
	if(!a[i][i])inc(k,i,n)swap(a[i][k],a[j][k]);
    }
    inc(i,1,n)ans*=a[i][i];
    if(ans<0)ans=-ans;
    printf("%llu\n",(unsigned long long)ans);
}

int main(){
    int _=read();while(_--){
	mem(a);
	n=read()-1;m=read();
	inc(i,1,m){_x=read();_y=read();a[_x][_y]=a[_y][_x]=-1;a[_x][_x]++;a[_y][_y]++;}
	cal();
    }
    return 0;
}

 

 

 

 

 

HIGH - Highways

no tags 

 

In some countries building highways takes a lot of time... Maybe that's because there are many possiblities to construct a network of highways and engineers can't make up their minds which one to choose. Suppose we have a list of cities that can be connected directly. Your task is to count how many ways there are to build such a network that between every two cities there exists exactly one path. Two networks differ if there are two cities that are connected directly in the first case and aren't in the second case. At most one highway connects two cities. No highway connects a city to itself. Highways are two-way.

Input

The input begins with the integer t, the number of test cases (equal to about 1000). Then t test cases follow. The first line of each test case contains two integers, the number of cities (1<=n<=12) and the number of direct connections between them. Each next line contains two integers a and b, which are numbers of cities that can be connected. Cities are numbered from 1 to n. Consecutive test cases are separated with one blank line.

Output

The number of ways to build the network, for every test case in a separate line. Assume that when there is only one city, the answer should be 1. The answer will fit in a signed 64-bit integer.

Example

Sample input:
4
4 5
3 4
4 2
2 3
1 2
1 3

2 1
2 1

1 0

3 3
1 2
2 3
3 1

Sample output:
8
1
1
3