hdu 4305 Lightning count MST

Matrix-Tree Theorem

For a non-directed graph with n vertices G
matrix D: degree of vertex i that du [i], the D [i] [i] = du [i]
adjacency matrix A: If there is an edge between i and j , then a [i] [j] = 1, otherwise 0
Kirchhoff matrix C: C = the DA
the matrix-Tree Theorem: for a no, it is equal to the number of spanning trees to FIG Kirchhoff matrix G n-1 of any order the absolute value of the determinant of the master formula.

example

Link Title: HDU 4305
ideas: press claim FIG built, then Kirchhoff matrix C is obtained, and finally the absolute value of any one of the n-1 order determinant of C

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 310;
const int mod = 10007;
LL x[N],y[N];
LL dis[N][N];
LL a[N][N];
LL inv(LL a,LL b)
{
    LL res=1;
    while(b)
    {
        if(b&1)res=(res*a)%mod;
        b>>=1;
        a=(a*a)%mod;
    }
    return res;
}
int judge(int i,int j,int k)//判断k是否是i和j连线上的点
{
    LL x1=x[j]-x[i],y1=y[j]-y[i];
    LL x2=x[k]-x[i],y2=y[k]-y[i];
    if(x1*y2==x2*y1&&x1*x2>=0&&dis[i][j]>dis[i][k])return 1;
    return 0;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        memset(a,0,sizeof(a));
        memset(dis,0,sizeof(dis));
        int n;
        LL r;
        scanf("%d%lld",&n,&r);
        for(int i=1;i<=n;i++)
            scanf("%lld%lld",&x[i],&y[i]);
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
                dis[i][j]=dis[j][i]=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                if(dis[i][j]>r*r)continue;
                int k;
                for(k=1;k<=n;k++)
                {
                    if(k==i||k==j)continue;
                    if(judge(i,j,k))break;
                }
                if(k>n)
                {
                    a[i][j]=a[j][i]=-1;
                    a[i][i]++;a[j][j]++;
                }
            }
        }
        LL ans=1;
        //化成下三角形
        for(int i=1;i<n;i++)
        {
            LL t=inv(a[i][i],mod-2);
            for(int j=i+1;j<n;j++)
            {
                LL tmp=a[j][i]*t%mod;
                for(int k=1;k<n;k++)
                    a[j][k]=(a[j][k]-a[i][k]*tmp%mod+mod)%mod;
            }
        }
        for(int i=1;i<n;i++)
            ans=ans*a[i][i]%mod;
        if(ans==0)printf("-1\n");
        else printf("%lld\n",ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/HooYing/p/11689473.html
MST
MST