比较想吐血。。。。。
题目:
Problem C. Dynamic Graph Matching
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1128 Accepted Submission(s): 463
Problem Description
In the mathematical discipline of graph theory, a matching in a graph is a set of edges without common vertices.
You are given an undirected graph with n vertices, labeled by 1,2,...,n. Initially the graph has no edges.
There are 2 kinds of operations :
+ u v, add an edge (u,v) into the graph, multiple edges between same pair of vertices are allowed.
- u v, remove an edge (u,v), it is guaranteed that there are at least one such edge in the graph.
Your task is to compute the number of matchings with exactly k edges after each operation for k=1,2,3,...,n2. Note that multiple edges between same pair of vertices are considered different.
Input
The first line of the input contains an integer T(1≤T≤10), denoting the number of test cases.
In each test case, there are 2 integers n,m(2≤n≤10,nmod2=0,1≤m≤30000), denoting the number of vertices and operations.
For the next m lines, each line describes an operation, and it is guaranteed that 1≤u<v≤n.
Output
For each operation, print a single line containing n2 integers, denoting the answer for k=1,2,3,...,n2. Since the answer may be very large, please print the answer modulo 109+7.
Sample Input
1
4 8
+ 1 2
+ 3 4
+ 1 3
+ 2 4
- 1 2
- 3 4
+ 1 2
+ 3 4
Sample Output
1 0
2 1
3 1
4 2
3 1
2 1
3 1
4 2
题意:给一个最多10个点的空图,之后有m个添加删除边的操作,要求每次操作后输出在图中 匹配数为1,2……k/2 的方案数
然后图的匹配指的是在图中选n条边,任意两条边都没有共用的点,匹配数就为n
匹配的具体讲解:https://blog.csdn.net/u012116229/article/details/44205295
【概念完全不熟离散数学白学了真的orz】
解法:
一看图中最多10个点所以考虑状压
f[i][s]表示在第 i 次操作后 ,点集s中所有点都匹配了的方案数
那么我们每添加一条边u,v 对于所有s中已经包含了u或v的没有影响【因为这时候u,v都匹配了,再添加边就不满足不相邻的条件了】
所以从大到小 f[i][s]+=f[i-1][s-u-v] 当然[i]这一维可以省略;
然后考虑删除操作,因为边的添加顺序对于答案没有影响所以我们把添加该边的操作看成在删除前一步,这样删除操作就看成复原上一步操作,把加边时候的操作反着来一遍就OJBK
还有一点求余要写成 x=(x%mod+mod)%mod 消除负数。。。。。。不过不知道为什么要这样但不写成这样就会WAorz
代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int mod=1e9+7;
ll ans[11];
ll dp[1<<11];
ll t,n,m,v,u;
char op[3];
void adjust(ll &x){x=(x%mod+mod)%mod;}
int main(){
scanf("%lld",&t);
while(t--){
memset(dp,0,sizeof(dp));
memset(ans,0,sizeof(ans));
dp[0]=1;
scanf("%lld%lld",&n,&m);
for (ll i=1;i<=m;i++){
scanf("%s%lld%lld",op,&v,&u);
v--;u--;
if (op[0]=='+'){
for (ll j=(1<<n)-1;j;j--)
if ( ((1<<v)&j) && ((1<<u)&j) )
{ dp[j]+=dp[j-(1<<u)-(1<<v)];
ans[__builtin_popcount(j)]+=dp[j-(1<<u)-(1<<v)];
adjust(dp[j]);
adjust(ans[__builtin_popcount(j)]);
}
}else{
for (ll j=1;j<1<<n;j++)
if ( ((1<<v)&j) && ((1<<u)&j) )
{ dp[j]-=dp[j-(1<<u)-(1<<v)];
ans[__builtin_popcount(j)]-=dp[j-(1<<u)-(1<<v)];
adjust(dp[j]);
adjust(ans[__builtin_popcount(j)]);
}
}
for (int j=2;j<=n;j+=2)
printf("%lld%c",ans[j],j==n?'\n':' ');
}
}
return 0;
}