BZOJ5297 CQOI2018社交网络(矩阵树定理)

时间:2023-01-27 17:32:02

  板子题。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 256
#define P 10007
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<''||c>'')) c=getchar();return c;}
int gcd(int n,int m){return m==?n:gcd(m,n%m);}
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
int n,m,degree[N],inv[P];
struct matrix
{
int a[N][N];
int value()
{
int s=;
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
a[i][j]=(a[i][j]%P+P)%P;
for (int i=;i<=n;i++)
{
if (!a[i][i])
for (int j=i+;j<=n;j++)
if (a[j][i]) {swap(a[i],a[j]);s=-s;break;}
for (int j=i+;j<=n;j++)
{
int t=1ll*a[j][i]*inv[a[i][i]]%P;
for (int k=i;k<=n;k++)
a[j][k]=((a[j][k]-a[i][k]*t)%P+P)%P;
}
}
for (int i=;i<=n;i++) s=1ll*s*a[i][i]%P;
return (s+P)%P;
}
}a;
int main()
{
#ifndef ONLINE_JUDGE
freopen("bzoj5297.in","r",stdin);
freopen("bzoj5297.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
n=read(),m=read();
for (int i=;i<=m;i++)
{
int x=read(),y=read();
a.a[y][x]--;degree[x]++;
}
for (int i=;i<=n;i++) a.a[i][i]+=degree[i];
inv[]=;for (int i=;i<P;i++) inv[i]=P-(P/i)*inv[P%i]%P;
cout<<a.value();
return ;
}