Hdu 6321 Dynamic Graph Matching 状压DP

Problem C. Dynamic Graph Matching

Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1226 Accepted Submission(s): 509

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


状压DP。把每个点是否在匹配中被选取用10位2进制压缩,每次修改时,枚举所有二进制状态。若增加一条从x到y的边,目前的状态为now,则有DP方程:
dp[now]+=(-=)dp[now^(2^x+2^y)];

此时需要注意,DP必须从点数多的状态向点数少的状态DP,否则会将当前的修改也带入DP中。

#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#include <assert.h>
#define pb push_back 
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef pair<int,int> pp;
const int maxn=2005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f,mod=1e9+7;
const ld pi=acos(-1.0L);
ll dp[maxn],ans[10];
char t[maxn];
int m;

struct node {
    int cnt,c;
};
node s[maxn];

bool cmp(node a,node b) {
    return a.cnt>b.cnt;
}

void init(int n) {
    for (int i=0;i<(1<<10);i++) {
        int cnt=0;
        for (int j=0;j<10;j++) {
            if (i&(1<<j)) cnt++;
        }
        if (cnt%2==0) s[++m].c=i,s[m].cnt=cnt;
    }
}

int main() {
    int cas;
    m=0;
    init(10);
    sort(s+1,s+m+1,cmp);
    scanf("%d",&cas);
    while (cas--) {
        int n,q,x,y;
        mem0(dp);
        for (int i=0;i<=10;i++) ans[i]=0;
        dp[0]=1;
        scanf("%d%d",&n,&q);
        for (int i=1;i<=q;i++) {
            scanf("%s%d%d",t,&x,&y);
            x--;y--;
            int now=(1<<x)+(1<<y);
            for (int j=1;j<=m;j++) {
    //          if (j==now) continue; 
                if ((s[j].c&now)==now) {
                    ll p=dp[s[j].c];
                    if (t[0]=='+') dp[s[j].c]+=dp[s[j].c^now]; else dp[s[j].c]-=dp[s[j].c^now];
                    dp[s[j].c]=(dp[s[j].c]+mod)%mod;
                    ans[s[j].cnt]+=(dp[s[j].c]-p+mod);
                    ans[s[j].cnt]%=mod;
                }
            }
    //      if (t[0]=='+') dp[now]++,ans[now]=(ans[now]+1)%mod; else dp[now]--,ans[now]=(ans[now]-1+mod)%mod;
            for (int j=2;j<=n;j+=2) {
                printf("%lld",ans[j]); 
                if (j!=n) printf(" ");
            }
            printf("\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值