Description's four-year-old fantasy township election began, the most recent fantasy township is the biggest problem is a lot of unidentified monsters into the Gensokyo, disturbed the former order of Gensokyo. But fantasy Township of the establishment of the Monster (human) Hakurei Ling Dream and eight clouds purple and other people all day high talk about all the monster equality, fantasy Township diversification, and so on, the fantasy township is facing a variety of big problems but can not give the right solution. The fragrance of the wind is one of the rare monsters in Gensokyo who are aware of the seriousness of the problem. This time she bravely stood out to participate in the fantasy village election. A series of proposals, including building a wall at the Gensokyo border (and making human money), vigorously carrying out infrastructure construction to save the unemployment rate and so on, became the unexpected dark Horse of the election year and went on to become the big leader of Gensokyo. After the delicate fragrance came into power, the first measure was to build the road of Gensokyo. Gensokyo has N cities, there is no road between the original. The delicate fragrance promised the voters a tax cut, so she planned to N-1 the cities only by repairing the road. But Gensokyo has just N-1 a construction company, and every construction company wants to get some benefits in the process of repairing roads. Although the construction companies did not give the delicate money before the election, the delicate fragrance still intends to have a good relationship with them, because she also expects them to help her build the wall. So she's going to have each construction company take care of a way to fix it. Each construction company tells the delicate scent of the city in which it is capable of being responsible for building the road. So the fragrance is going to choose N-1 to connect the edges of all the cities in Gensokyo, and then each side is built by a construction company that can take charge of that side, and every construction company just has to fix a side. Fragrance Now you want to know how many possible options are there? Two scenarios are different when and only if they are either trimmed by different sets of edges, or the edges are assigned differently.
The first line of input contains a positive integer N (n<=17) that represents the number of cities. Next N-1 line, where line I represents a list of roads that can be built by the construction company I: Start with a non-negative MI, which means that it can build Mi road, followed by Mi logarithm, each representing two endpoints of an edge. There are no duplicate edges, and no self-loops appear.
Output is a single integer that represents the result of 10^9 + 7 modulo for all possible scenarios.
Sample Input4
2 3 2) 4 2
5 2 1 3 1 3 2 4 1 4 3
4 2 1 3 2 4 1 4 2Sample Output -It's like Zjoi little stars. considering the principle of repulsion, set a (x) to represent the set of solutions that x companies are involved in building, then the answer is equivalent to seeking | A (1) ∩a (2) ∩ ... ∩a (n) |, the tolerance is converted into the form of a set, and then the matrixtree theorem can be calculated. The actual complexity is O (n^3*2^n).
#include <cstdio> #include <cctype> #include <cstring> #include <algorithm> #define REP (i,s,t) for (int i=s;i<=t;i++) #define DWN (I,S,T) for (int. i=s;i>=t;i--) #define REN for (int i=first[x];i;i=next[i]) using namespace Std;const int Buffersize=1<<16;char buffer[buffersize],*head,*tail;inline char Getchar () {if (Head==ta IL) {int l=fread (Buffer,1,buffersize,stdin); Tail= (Head=buffer) +l; } return *head++;} inline int read () {int X=0,f=1;char c=getchar (); for (;! IsDigit (c); C=getchar ()) if (c== '-') f=-1; for (; IsDigit (c); C=getchar ()) x=x*10+c-' 0 '; return x*f;} typedef long LONG ll;const int maxn=20;const int mod=1000000007;typedef ll matrix[maxn][maxn];void gcd (ll a,ll b,ll& x ,ll& y) {if (!b) x=1,y=0; else gcd (b,a%b,y,x), y-=x* (A/b);} ll Getinv (ll a) {ll b=mod,x,y;gcd (a,b,x,y); Return (X+mod)%mod;} Matrix B;ll Gauss (Matrix A,int N) {ll ans=1; Rep (i,0,n-1) {int r=i; Rep (j,i+1,n-1) if (ABS (A[r][i)) ≪abs (A[j][i]) r=j; if (r!=i) Ans*=-1,swap (A[r],a[i]); ll INV=GETINV (A[i][i]); Rep (k,0,n-1) if (i!=k) dwn (j,n-1,i) a[k][j]= (a[k][j]-a[k][i]*a[i][j]%mod*inv%mod+mod)%mod; } Rep (i,0,n-1) (ans*=a[i][i])%=mod; Return (Ans+mod)%mod;} struct company {int M,U[MAXN*MAXN],V[MAXN*MAXN];} A[maxn];int Main () {int n=read (); Rep (i,0,n-2) Dwn (J,a[i].m=read (), 1) a[i].u[j]=read (), A[i].v[j]=read (); ll Ans=0; Rep (s,0, (1<<n-1)-1) {int cnt=0; Rep (i,0,n-1) Rep (j,0,n-1) b[i][j]=0; Rep (i,0,n-2) if (s>>i&1) {cnt++; Rep (j,1,a[i].m) {int u=a[i].u[j]-1,v=a[i].v[j]-1; b[u][u]++; b[v][v]++; b[u][v]--; b[v][u]--; }} Rep (i,0,n-1) Rep (j,0,n-1) (b[i][j]+=mod)%=mod; if (n-1-cnt&1) (Ans-=gauss (b,n-1))%=mod; Else (Ans+=gauss (b,n-1))%=mod; } printf ("%lld\n", (ans+mod)%mod); return 0;}
BZOJ4596: [Shoi2016] Fantasy township before dark