题解 P2144 【[FJOI2007]轮状病毒】

题目链接

Solution [FJOI2007]轮状病毒

题目大意:给定一张图,求生成树数量

分析:矩阵树定理裸题,唯一恶心人的地方就是要写高精度

但是作为一个Python爱好者(其实就是懒),我们怎能就此束手就擒,乖乖按照毒瘤出题人的意愿写高精?Python 30行解决战斗

Life is short,I use Python

n = int(input())
maxn = 128
deg = [[0 for i in range(maxn)] for j in range(maxn)]
G = [[0 for i in range(maxn)] for j in range(maxn)]
lap = [[0 for i in range(maxn)] for j in range(maxn)]//列表生成式生成二维数组
def addedge(x,y):
    G[x][y] += 1
    deg[y][y] += 1
def gauss(idx)://高斯消元求解
    res = 1
    for i in range(1,idx + 1):
        for j in range(i + 1,idx + 1):
            while lap[j][i] != 0:
                r = lap[i][i] // lap[j][i]
                res *= -1
                for k in range(i,idx + 1):
                    lap[i][k] -= r * lap[j][k]
                    lap[i][k],lap[j][k] = lap[j][k],lap[i][k]
        res *= lap[i][i]
    return res
for i in range(1,n):
    addedge(i,i + 1),addedge(i + 1,i)
addedge(n,1),addedge(1,n)
for i in range(1,n + 1):
    addedge(i,n + 1),addedge(n + 1,i)
n += 1
for i in range(1,n + 1):
    for j in range(1,n + 1):
        lap[i][j] = deg[i][j] - G[i][j]
print(gauss(n - 1))

转载于:https://www.cnblogs.com/colazcy/p/11515145.html

你可能感兴趣的:(题解 P2144 【[FJOI2007]轮状病毒】)