Problem C. Dynamic Graph Matching
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 376 Accepted Submission(s): 145
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
Source
2018 Multi-University Training Contest 3
壮压。
令mask表示点的匹配状态
当+ u v的边的时候
令mask1=(1<<(u-1))+(1<<(v-1))
dp(mask|mask1)+=dp(mask) (mask&mask1==0)
- u v的时候类似,只不过把+换成-即可
#include <bits/stdc++.h>
#define fir first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
using namespace std;
const int maxn=1e5+10;
const ll mod=1e9+7;
const double eps=1e-7;
const int maxm=1e6+10;
const int inf=0x3f3f3f3f;
int g[15][15];
ll dp[1100];
int num[1100];
int n,m;
char s[2];
int u,v;
ll ans[10];
int main(){
int t;
scanf("%d",&t);
num[1]=1;
for (int i=2;i<=1030;i++){
num[i]=num[i/2]+i%2;
}
/*for (int i=2;i<=30000;i++){
inv[i]=(mod-mod/i)*inv[mod%i]%mod;
}*/
while (t--){
scanf("%d %d",&n,&m);
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++) g[i][j]=0;
for (int i=0;i<(1<<n);i++) dp[i]=0;
dp[0]=1;
while (m--){
scanf("%s %d %d",s,&u,&v);
if (s[0]=='+'){
g[u][v]++;
int mask=(1<<(u-1))+(1<<(v-1));
for (int i=0;i<(1<<n);i++){
if (!(i&mask)){
dp[i|mask]=(dp[i|mask]+dp[i]%mod)%mod;
//dp[i]=(dp[i]*inv[g[u][v]-1])%mod*g[u][v]%mod;
}
}
}
else{
g[u][v]--;
int mask=(1<<(u-1))+(1<<(v-1));
for (int i=0;i<(1<<n);i++){
if (!(i&mask)){
dp[i|mask]=(dp[i|mask]-dp[i])%mod;
dp[i|mask]=(dp[i|mask]+mod)%mod;
}
}
}
/* for (int i=1;i<(1<<n);i++){
printf("%lld ",dp[i]);
}
printf("\n");*/
for (int i=1;i<=n;i++) ans[i]=0;
for (int i=1;i<(1<<n);i++){
ans[num[i]]=(ans[num[i]]+dp[i])%mod;
}
printf("%lld",ans[2]);
for (int i=2;i<=n/2;i++) printf(" %lld",ans[i*2]);
printf("\n");
}
}
return 0;
}