[生成樹計數] BZOJ1002 輪狀病毒 生成樹計數+JAVA

Step:

1.求基爾霍夫矩陣(=度數矩陣-圖的鄰接矩陣)

2.高斯消元求n-1階主子式的行列式,答案就是生成樹個數。

n-1階主子式就是n階方陣去掉任意一個元素所在的行和列的所有元素,就變成了一個n-1階的方陣。我把中心的那個點給去掉了。

兢兢業業寫完之後發現這個要用高精度,默默改用JAVA但是高斯消元的時候用BigDecimal要不就精度不夠WA要不就T。後來找了一個用BigInteger的高斯消元。還不會高斯消元。氣哭。


import java.io.BufferedInputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

import org.omg.CosNaming.BindingIteratorPOA;

public class Main{
	static final double eps=1e-3;
    public static void main(String[] args) {  
    	int[][] mp=new int[105][105];
    	int n;
    	BigInteger[][] mat=new BigInteger[105][105];
    	Scanner input=new Scanner(new BufferedInputStream(System.in));
    	n=input.nextInt();
    	for (int i = 0; i < n + 1; i++){
            for (int j = 0; j < n + 1; j++)
            	mat[i][j] = BigInteger.ZERO;
        }
        if(n==1){
        	System.out.println("1");
        	System.exit(0);
        }else if(n==2){
            System.out.println("5");System.exit(0);
        }
        for(int i=0;i<n;i++){
            mp[i][n]=mp[n][i]=1;
        }
        for(int i=0;i<n;i++){
            mp[i][(i+1)%n]=mp[(i+1)%n][i]=1;
        }
        for(int i=0;i<=n;i++){
            if(i<n) mat[i][i]=BigInteger.valueOf(3-mp[i][i]);
            else mat[i][i]=BigInteger.valueOf(n-mp[i][i]);
            for(int j=0;j<=n;j++){
                if(i!=j){
                    mat[i][j]=BigInteger.valueOf(-mp[i][j]);
                }
            }
        }
        n++;
        BigInteger tot=Gauss(n-1,n-1,mat);
        BigInteger ans = BigInteger.ONE;
        for (int i = 0; i < n; i++) ans = ans.multiply(mat[i][i]);
        System.out.println(ans.divide(tot).abs().divide(BigInteger.valueOf(n-1)));
       
    }  
    static boolean zero(BigInteger a){
        return a.compareTo(BigInteger.ZERO)==0;
    }
    static BigInteger lcm(BigInteger x,BigInteger y){
        return x.divide(x.gcd(y)).multiply(y);
    }
    static BigInteger Gauss(int equ,int var,BigInteger[][] a){ 
    	int i,j,k; 
    	int max_r; 
    	int col; 
    	BigInteger ta,tb; 
    	BigInteger LCM; 
    	BigInteger tot = BigInteger.ONE;
    	int temp; col = 0; 
    	for (k = 0; k < equ && col < var; k++ ,col ++){ 
    		max_r = k; for (i = k + 1; i < equ; i++){ 
    			if (a[i][col].abs().compareTo(a[max_r][col].abs()) > 0) max_r = i; 
    		} 
    		if (max_r != k){ 
    			for (j = k; j < var + 1; j++) { 
    				BigInteger t; 
    				t = a[k][j]; 
    				a[k][j] = a[max_r][j]; 
    				a[max_r][j] = t; 
    				} 
    			} 
    		if (a[k][col].equals(BigInteger.ZERO)) { 
    			k--; continue; 
    		} 
    		for (i = k + 1; i < equ; i++){ 
    			if (!a[i][col].equals(BigInteger.ZERO)) { 
    				LCM = lcm(a[i][col].abs(),a[k][col].abs()); 
    				ta = LCM.divide(a[i][col].abs()); 
    				tb = LCM.divide(a[k][col].abs()); 
    				tot = tot.multiply(ta); 
    				if (a[i][col].multiply(a[k][col]).compareTo(BigInteger.ZERO) < 0) 
    					tb = tb.multiply(BigInteger.valueOf(-1)); 
    				for (j = col; j < var + 1; j++){ 
    					a[i][j] = a[i][j].multiply(ta).subtract(a[k][j].multiply(tb)); 
    					} 
    				} 
    			} 
    		} 
    	return tot;
    	}

}  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章