社交网络
题目背景:
分析:矩阵树定理
求有向图的有根树形图,显然对于有根的树形图是可以直接用矩阵树定理的,那么直接建出入度矩阵和邻接表矩阵,入度矩阵 - 邻接表矩阵获得基尔霍夫矩阵。直接去掉1号点所在行列直接求就可以了。复杂度O(n3)
Source:
/*
created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
inline char read() {
static const int IN_LEN = 1024 * 1024;
static char buf[IN_LEN], *s, *t;
if (s == t) {
t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
if (s == t) return -1;
}
return *s++;
}
///*
template<class T>
inline void R(T &x) {
static char c;
static bool iosig;
for (c = read(), iosig = false; !isdigit(c); c = read()) {
if (c == -1) return ;
if (c == '-') iosig = true;
}
for (x = 0; isdigit(c); c = read())
x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/
const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
*oh++ = c;
}
template<class T>
inline void W(T x) {
static int buf[30], cnt;
if (x == 0) write_char('0');
else {
if (x < 0) write_char('-'), x = -x;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
while (cnt) write_char(buf[cnt--]);
}
}
inline void flush() {
fwrite(obuf, 1, oh - obuf, stdout);
}
/*
template<class T>
inline void R(T &x) {
static char c;
static bool iosig;
for (c = getchar(), iosig = false; !isdigit(c); c = getchar())
if (c == '-') iosig = true;
for (x = 0; isdigit(c); c = getchar())
x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/
const int MAXN = 250 + 10;
const int mod = 10007;
int n, m, x, y;
int a[MAXN][MAXN];
inline int mod_pow(int a, int b) {
int ans = 1;
for (; b; b >>= 1, a = (long long)a * a % mod)
if (b & 1) ans = (long long)ans * a % mod;
return ans;
}
inline void read_in() {
R(n), R(m);
for (int i = 1; i <= m; ++i) {
R(x), R(y), x--, y--;
if (x == y) continue ;
a[x][x]++, a[y][x]--;
}
}
inline void gauss() {
int sign = 1;
int ans = 1;
for (int i = 1; i < n; ++i) {
int pos = -1;
for (int j = i; j < n; ++j)
if (a[j][i] != 0) {
pos = j;
break ;
}
if (pos != i) {
sign = -sign;
for (int k = i; k < n; ++k) std::swap(a[i][k], a[pos][k]);
}
int inv = mod_pow(a[i][i], mod - 2);
for (int j = i + 1; j < n; ++j) {
int t = (long long)a[j][i] * inv % mod;
for (int k = i; k < n; ++k)
a[j][k] = (a[j][k] - t * a[i][k] % mod + mod) % mod;
}
ans = ans * a[i][i] % mod;
}
std::cout << (sign * ans + mod) % mod;
}
int main() {
// freopen("sns.in", "r", stdin);
// freopen("sns.out", "w", stdout);
read_in();
gauss();
return 0;
}