[CSP-S Simulation Test]: giant magic (like pressure DP)

Title Description

Oberthur Leask giant magic is very fond of a directed graph, one day he found a directed graph $ n $ points of $ m $ edges.
Oberoi Leask did not believe a directed graph ring is beautiful, I ask how many sub-graphs (ie a selected set of edges) have this picture is beautiful? The answer to $ 1,000,000,007 $ modulo.


Input Format

The first line of two integers n-$ $ and $ m $.
The next two rows each row represents an integer of $ m $ a directed edges. To ensure that no re-Loop-free edges.


Output Format

A row of integer answer, of $ 1,000,000,007 $ modulo.


Sample

Sample input:

3 6
1 2
2 1
1 3
3 1
2 3
3 2

Sample output:

25


Data range and tips

For $ 40 \% $ data $ n \ leqslant 5, m \ leqslant 20 $;
for $ 60 \% $ data $ n \ leqslant 10 $;
for $ 80 \% $ data $ n \ leqslant 15 $;
for $ 100 \ % $ data $ n \ leqslant 17 $.


answer

See the data range is $ 17 $, I think I can test this $ AK $; however, when I see it's time to set the edge, I found that after half an hour in the room to hear a little keyboard sound ......

Similarly, we first consider the $ 60 \% $ of practice.

Give hierarchical directed acyclic graph, the degree of the first layer is a $ 0 $ point, the second layer is the first layer of the deletion of degree $ 0 $ point, similar to $ topsort $?

Then set $ dp [i] [j] $ indicates the currently selected set of nodes is $ i $, the final layer of the set of nodes $ J $ program number.

S_1 may assume $ $ indicates the currently selected set of nodes, S_2 $ $ represents the last layer of the set of nodes, a subset of $ S_3 $ $ $ complement of S_1, S_2 and $ $ $ and $ S_3 a linked side, $ s_1 \ oplus s_2 $ and $ s_3 $ connected side is $ cnt_1 $ bar, $ s_2 $ $ s_3 $ connected with the side of a $ cnt_2 $ strips.

Then, the state transition equation is:

$$dp[s_1|s_3][s_3]=\sum dp[s_1][s_2]\times \prod 2^{cnt_1}\times (2^{cnt_2}-1)$$

Such time complexity is $ \ Theta (4 ^ n \ times m) $, either space or time is not enough.

Therefore, we consider subtract the second dimension, direct enumeration of the currently selected node set $ s_1 $, then $ s_2 $ enumerate a subset of its complement, and even set up with between $ s_1 $ $ $ s_2 $ sides have cnt $ strips.

Well, you may be listed in such a state of transition equation: $ dp [s_1 | s_2] = dp [s_1] \ times 2 ^ {cnt} $.

But you will find that the answer will count much as $ s_1 | s_2 $ be a lot of $ s_1 $ and $ s_2 $ composed, so we also need inclusion and exclusion, with coefficients $ (- 1) ^ {sz [s] +1 } $.

The time complexity of this time for $ \ Theta (3 ^ nm) $, can only get $ 80 $ points, also need to be optimized, there is a lot of optimization, I optimize it to $ \ Theta (2 ^ n \ times n ^ 2 ) $, so that you can $ AC $ this question is a.

Time complexity: $ \ Theta (2 ^ n \ times n ^ 2) $.

Expectations score: $ 100 $ points.

Actual score: $ 100 $ points.


Code time

#include<bits/stdc++.h>
using namespace std;
const int mod=1000000007;
int n,m;
bool Map[20][20];
int sz[131073];
int du[65537];
int sum[131073];
int qpow[1000];
int dp[131073];
int main()
{
	scanf("%d%d",&n,&m);
	int maxn=(1<<n);
	dp[0]=qpow[0]=1;
	for(int i=1;i<=m;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		Map[x-1][y-1]=1;
		qpow[i]=(qpow[i-1]<<1)%mod;
	}
	sz[0]=-1;
	for(int i=1;i<maxn;i++)sz[i]=sz[i>>1]*(i&1?-1:1);
	for(int i=0;i<maxn-1;i++)
	{
		if(!dp[i])continue;
		int t=maxn-1-i;
		memset(du,0,sizeof(du));
		for(int j=0;j<n;j++)
			if(i&(1<<j))
				for(int k=0;k<n;k++)
					du[1<<k]+=Map[j][k];
		sum[0]=0;
		for(int s=(t-1)&t;;s=(s-1)&t)
		{
			int now=t^s,lst=now&-now;
			sum[now]=sum[now-lst]+du[lst];
			dp[i+now]=(dp[i+now]+1LL*sz[now]*qpow[sum[now]]*dp[i])%mod;
			if(!s)break;
		}
	}
	printf("%lld",dp[maxn-1]);
	return 0;
}

rp++

Guess you like

Origin www.cnblogs.com/wzc521/p/11619687.html