Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1341 Accepted Submission(s): 561
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
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
题意:在一个无向图中给出 n 个点, n <= 10 且 n 为偶数,有m个操作,有两种操作,一种是在两个点之间增加一条边,一种是从图中去除两个点之间的一条边,可以有重边
每次操作都要求 n / 2 个答案 记为询问 X (1...n/2) , X = 2时表示:从图中取两条边,两条边中没有边共点的方案数,以此类推
思路:因为 n 只有十,考虑状态压缩, 1 << 10 = 1024 ,即最多需要 1024个十进制的数可以表示出所有点集的情况
例如样例中有 4 个点
插入 + 1 2 边的时候用二进制 _ _ 1 1 表示,即 3,而四个点的图中,包含了这两个点的点集且只包含偶数个点的情况只有两种
1. _ _ 1 1 = 3 2. 1 1 1 1 = 15 (本题点集含义:点数 = 2 (例如 点 1 和 点 2) 的点集 dp[3] 表示,有这两个在内,且符合题目要求的没有两边共点的方案数)
注意预处理的时候不可最后再取模,会爆int,所以最好一边算一边取,且用 long long 保证不丢失
代码:
#include
using namespace std;
#define ll long long
#define mem(a,x) memset(a,x,sizeof(a))
const int mod = 1e9 + 7;
const int maxn = 1 << 10 | 1;
int dp[2][maxn];
int bits[maxn],eve[maxn],ans[10];
int cnt;
void init(){
int len = 1 << 10;
cnt = 0;
for(int i = 0;i < len;i++){
bits[i] = bits[i >> 1] + (i & 1); // bits[i] 表示 i 的二进制有多少个1
if(bits[i] % 2 == 0){
eve[cnt++] = i; // eve[i] 记录的是 1 的个数为偶数的二进制数
}
}
}
int main(){
int T;
int n,m,u,v;
char opt[5];
init();
scanf("%d",&T);
while(T--){
mem(dp,0);
dp[0][0] = 1;
int flag = 0; // 用 flag ^ 1 做滚动数组
scanf("%d %d",&n,&m);
int len = 1 << n;
while(m--){
mem(ans,0);
scanf("%s %d %d",opt,&u,&v);
u--,v--;
int state = (1 << u) | (1 << v); // state 表示当前插入的边用二进制表示的状态
if(opt[0] == '+'){
for(int i = 0;i < cnt;i++){
if(eve[i] >= len){
break;
}
int sta = eve[i]; //
dp[flag ^ 1][sta] = dp[flag][sta];
if((sta & state) == state){
dp[flag ^ 1][sta] = (dp[flag ^ 1][sta] + dp[flag][sta ^ state]) % mod;
}
ans[bits[sta] / 2] = (ans[bits[sta] / 2] + dp[flag ^ 1][sta]) % mod;
}
}else{
for(int i = 0;i < cnt;i++){
if(eve[i] >= len){
break;
}
int sta = eve[i];
dp[flag ^ 1][sta] = dp[flag][sta];
if((sta & state) == state){
dp[flag ^ 1][sta] -= dp[flag][sta ^ state];
if(dp[flag ^ 1][sta] < 0){
dp[flag ^ 1][sta] += mod;
}
}
ans[bits[sta] / 2] = (ans[bits[sta] / 2] + dp[flag ^ 1][sta]) % mod;
}
}
flag ^= 1;
for(int i = 1;i <= n / 2;i++){
if(i != 1)
printf(" ");
printf("%d",ans[i]);
}
puts("");
}
}
return 0;
}