P4455 [CQOI2018]社交网络(矩阵树定理)

时间:2022-02-03 17:30:48

题目

P4455 [CQOI2018]社交网络

\(CQOI\)的题都这么裸的吗??

做法

有向图,指向叶子方向 \(D^{out}(G)-A(G)\)

至于证明嘛,反正也就四个定理,先挖个坑,省选后再来补

My complete code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<ctime>
using namespace std;
typedef int LL;
const int p=10007;
const int maxn=300;
inline LL Read(){
LL x(0),f(1); char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') f=-1; c=getchar();
}
while(c>='0'&&c<='9')
x=(x<<3)+(x<<1)+c-'0',c=getchar();
return x*f;
}
LL n,m;
LL D[maxn][maxn];
inline LL Pow(LL base,LL b){
LL ret(1);
while(b){
if(b&1)
ret=ret*base%p;
base=base*base%p,
b>>=1;
}return ret;
}
inline LL Solve(){
LL N(n),tr(0),ans(1);
for(LL i=2;i<=N;++i){
LL mx(i);
for(LL j=i+1;j<=N;++j)
if(D[mx][i]<D[j][i])
mx=j;
if(D[mx][i]==0)
return 0;
if(mx!=i){
swap(D[mx],D[i]),
tr^=1;
}
for(LL j=i+1;j<=N;++j){
LL tmp=D[j][i]*Pow(D[i][i],p-2)%p;
for(LL k=1;k<=N;++k)
D[j][k]=(D[j][k]-tmp*D[i][k]%p+p)%p;
}
ans=ans*D[i][i]%p;
}
if(tr)
ans=p-ans;
return ans;
}
int main(){
n=Read(),m=Read();
while(m--){
LL v(Read()),u(Read());
++D[v][v],D[u][v]=(D[u][v]+p-1)%p;
}
printf("%d\n",Solve());
return 0;
}/*
*/