# BZOJ 3659: Which Dreamed It 基尔霍夫矩阵 best theorem

## 3659: Which Dreamed It

Time Limit: 20 Sec  Memory Limit: 1024 MB
Submit: 172  Solved: 49
[Submit][Status][Discuss]

有多组数据。

1
0
2
1 1
1 2
0

1
0

## HINT

Best Theorem

#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>

using namespace std;

typedef long long ll;

inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}

const int N=110,mod=1000003;

int a[N][N],d[N],fac[mod];

void initial()
{memset(a,0,sizeof(a));memset(d,0,sizeof(d));}

int det(int n)
{
register int i,j,k,res=1,x,y,tmp,f=1;
for(i=1;i<=n;++i)for(j=1;j<=n;++j)a[i][j]=(a[i][j]+mod)%mod;
for(i=1;i<=n;++i)
{
for(j=i+1;j<=n;++j)
{
x=a[i][i],y=a[j][i];
while(y)
{
tmp=x/y;x%=y;swap(x,y);
for(k=i;k<=n;++k)
a[i][k]=((a[i][k]-1ll*tmp*a[j][k])%mod+mod)%mod;
for(k=i;k<=n;++k)swap(a[i][k],a[j][k]);
f=-f;
}
}
if(!a[i][i])return 0;
res=1ll*res*a[i][i]%mod;
}
if(f==-1)res=mod-res;
return (res%mod+mod)%mod;
}

int main()
{

register int i,j,v,n=read(),ans;
fac[0]=1;for(i=1;i<mod;++i)fac[i]=1ll*fac[i-1]*i%mod;//cout<<n<<endl;
while(n)
{
ans=1;
for(i=1;i<=n;++i)
{
d[i]=read();
for(j=1;j<=d[i];++j){v=read();if(i!=v)a[i][i]++,a[i][v]--;}
}
if(n==1&&!d[1]){puts("1");n=read();continue;}
for(i=1;i<=n;++i)ans=1ll*ans*fac[d[i]-1]%mod;
ans=1ll*ans*det(n-1)%mod;ans=1ll*ans*d[1]%mod;
print(ans);puts("");
n=read();initial();
}
return 0;
}
/*
1
0
2
1 1
1 2
0

1
0
*/