1. 程式人生 > >2018 Multi-University Training Contest 3 hdu 6321 Problem C. Dynamic Graph Matching(狀壓)

2018 Multi-University Training Contest 3 hdu 6321 Problem C. Dynamic Graph Matching(狀壓)

題意

給定一個 n 個點的無向圖,m 次加邊或者刪邊操作。在每次操作後統計有多少個匹配包含 k = 1, 2, ..., n/2 條邊。

 

題解

狀壓表示當前用了那些點,加邊就是dp[i|(1<<x)|(1<<y)]+=dp[i];減邊就是減去dp[i]

 

程式碼

​
#include<bits/stdc++.h>
#define N 100005
#define P pair<int,int>
using namespace std;
typedef long long ll;
const int M=1e9+7;
const int inf=1e9+7;
ll ans[1200],sum[6];
int f[11],n,x,y,d,k;
ll Mod(ll x,ll y){
    if(x>=y)x-=y;
    if(x<0)x+=y;
    return x;
}
void dfs(int p,int m,int num)
{
    if(p>=n){
        k=m|f[x]|f[y];
        sum[num/2+1]=Mod(sum[num/2+1]-ans[k],M);
        ans[k]+=d*ans[m];
        ans[k]=Mod(ans[k],M);
        sum[num/2+1]=sum[num/2+1]+ans[k];
        if(sum[num/2+1]>=M)sum[num/2+1]-=M;
        return;
    }
    dfs(p+1,m,num);
    if(p!=x&&p!=y)dfs(p+1,m|f[p],num+1);
}
int main()
{
    f[0]=1;
    for(int i=1;i<=10;i++)
        f[i]=f[i-1]*2;
    int t,m;
    for(scanf("%d",&t);t;t--)
    {
        scanf("%d%d",&n,&m);
        memset(ans,0,sizeof(ans));
        memset(sum,0,sizeof(sum));
        ans[0]=1;
        char c[5];
        while(m--){
            scanf("%s%d%d",c,&x,&y);
            x--;y--;
            d=c[0]=='+'?1:-1;
            dfs(0,0,0);
            for(int i=1;i<n/2;i++)
                printf("%lld ",sum[i]);
            printf("%lld\n",sum[n/2]);
        }
    }
    return 0;
}

​