HDU6321:Problem C. Dynamic Graph Matching(状压dp)

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

题意:给一个图有N个点和若干个询问,每个询问增或者删一条边,每个询问输出选择1~N/2条不相交的边的方案数。

思路:因为点的个数很少,所以dp[i]表示i状态点集的选边方案数,因为X个点限定选X/2条边,所以很方便转移。

/*dp[i][j]表示j状态点集的方案数*/

# include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 1e9+7;
int dp[2][1025], num[1025], sta[1025], ans[6];
void add(int &x, int y)
{
    x += y;
    if(x >= mod) x -= mod;
}
void sub(int &x, int y)
{
    x -= y;
    if(x < 0) x += mod;
}
int main()
{
    char c;
    int n, q, T, u, v, cnt=0;
    for(int i=0; i<1024; ++i)
    {
        num[i]= num[i>>1]+(i&1);//i的二进制有几个1
        if(~num[i]&1) sta[cnt++] = i;//sta保存偶数个1的二进制数
    }
    for(scanf("%d",&T);T;--T)
    {
        memset(dp, 0, sizeof(dp));
        scanf("%d%d",&n,&q);
        int now = 0;
        dp[0][0] = 1;
        while(q--)
        {
            getchar();
            c = getchar();
            scanf("%d%d",&u,&v);
            --u,--v;
            memset(ans, 0, sizeof(ans));
            int tmp = (1<<u) | (1<<v);
            if(c == '+')
            {
                for(int i=0; i<cnt&&sta[i]<(1<<n); ++i)
                {
                    int cur = sta[i];
                    dp[now^1][cur] = dp[now][cur];
                    if((cur&tmp) == tmp) add(dp[now^1][cur],dp[now][cur^tmp]);
                    add(ans[num[cur]/2],dp[now^1][cur]);
                }
            }
            else
            {

                for(int i=0; i<cnt&&sta[i]<(1<<n); ++i)
                {
                    int cur = sta[i];
                    dp[now^1][cur] = dp[now][cur];
                    if((cur&tmp) == tmp) sub(dp[now^1][cur],dp[now][cur^tmp]);
                    add(ans[num[cur]/2],dp[now^1][cur]);
                }
            }
            now ^= 1;
            for(int i=1; i<=n/2; ++i) printf("%d%c",ans[i],i==n/2?'\n':' ');
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值