[CQOI 2018] Social Network

Description

Question bank link

Find the number of directed spanning trees with \(n\) points rooted at \(1\) .

\ (1 \ leq n \ leq 250 \)

Solution

I will finally be \(\texttt{Matrix-Tree}\) spicy! !

It is impossible to write a detailed explanation, just drop the link directly .

Note that the directed graph degree matrix is ​​in-degree.

Code

#include <bits/stdc++.h>
using namespace std;
const int N = 250+5, yzh = 10007;

int n, m, u, v, a[N][N];

int gauss() {
    int ans = 1;
    for (int i = 2; i <= n; i++) {
        for (int j = i+1; j <= n; j++)
            while (a[j][i]) {
                int t = a[i][i]/a[j][i];
                for (int k = i; k <= n; k++) (a[i][k] -= a[j][k]*t%yzh) %= yzh;
                swap(a[i], a[j]); ans *= -1;
            }
        (ans *= a[i][i]) %= yzh;
    }
    return (ans+yzh)%yzh;
}
void work() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++) {
        scanf("%d%d", &v, &u);
        ++a[v][v], --a[u][v];
    }
    printf("%d\n", gauss());
}
int main() {work(); return 0; }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325184925&siteId=291194637
Recommended