HDU

来源:互联网 发布:杭州整站优化公司 编辑:程序博客网 时间:2024/06/16 11:44

Problem Description
Long long ago, there was a prosperous kingdom which consisted of n cities and every two cites were connected by an undirected road.

However, one day a big monster attacked the kingdom and some roads were destroyed. In order to evaluate the influence brought by the catastrophe, the king wanted to know the instability of his kingdom. Instability is defined as the number of the unstable subset of {1, 2,,n}.

A set S is unstable if and only if there exists a set A such that AS(|A|3) and A is a clique or an independent set, namely that cites in A are pairwise connected directly or they are pairwise disconnected.

Archaeologist has already restored themroads that were not destroyed by the monster. And they want you to figure out the instability.

Since the answer may be tremendously huge, you are only required to write a program that prints the answer modulo 1000000007.
 

Input
The first line contains only one integer T, which indicates the number of test cases.

For each test case, the first line contains two integers n (3n50) and m (1mn(n1)/2), indicating the number of cities and the number of roads.

Then the following are m lines, each of which contains two integers x and y, indicating there is a road between the city x and the city y.

It is guarenteed that there does not exist a road connecting the same city and there does not exist two same roads.
 

Output
For each test case, print a line “Case #x: y”, where x is the case number (starting from 1) and y is an integer indicating the instability modulo 1000000007.
 

Sample Input
24 31 22 31 33 0
 

Sample Output
Case #1: 2Case #2: 1
Hint
• In the first example, {1,2,3} and {1,2,3,4} , containing the subset {1,2,3} which is connecteddirectly, are considered unstable.• In the second example, {1,2,3} is considered unstable because they are not pairwise connecteddirectly.


思路

题目大致意思是给定一个无向图(点数>=3),求这个集合有多少子集满足这个子集中存在K3完全图或者三个互相孤立的点

这里要用到Ramsey定理进行优化        具体可以参见    Ramsey定理简介

定理中红色的Km或者蓝色的Kn在本题中就可以看成完全图或者互相孤立的点

于是对于n>=6的集合,C(n,6)即为数量,因为r(3,3)=6 (  r(m,n)意义参见上面Ramsey链接  )。

对于点的数量是3,4,5的子集,枚举即可


代码示例

#include<bits/stdc++.h>#define clr(a,b) memset(a,b,sizeof(a))using namespace std;const int maxn=55;const int mod=1000000007;typedef long long ll;ll c[maxn][maxn];int mat[maxn][maxn];void init(){    c[0][0]=1;    c[1][0]=c[1][1]=1;    for(int i=2;i<maxn;++i){        for(int j=0;j<=i;++j){            if(j==0||j==i) c[i][j]=1;            else c[i][j]=c[i-1][j-1]+c[i-1][j];        }    }}int fun(int n){    ll ans=0;    for(int i=6;i<=n;++i){        ans+=c[n][i];    }    ans%=mod;    return ans;}bool check(int i,int j,int k){    ll ans=0;    int tmp[3];    tmp[0]=i;    tmp[1]=j;    tmp[2]=k;    int sum=0;    for(int p=0;p<=2;++p){        for(int q=0;q<p;++q){            sum+=mat[tmp[p]][tmp[q]];        }    }    if(sum==0||sum==3) return 1;    else return 0;}int check3(int n){    ll ans=0;    for(int i=1;i<=n;++i){        for(int j=1;j<i;++j){            for(int k=1;k<j;++k){                if(check(i,j,k)) ans++;            }        }    }    return ans;}int check4(int n){    ll ans=0;    for(int i=1;i<=n;++i){        for(int j=1;j<i;++j){            for(int k=1;k<j;++k){                for(int l=1;l<k;++l){                    if(check(i,j,k)||check(i,j,l)||check(i,k,l)||check(j,k,l)) ans++;                }            }        }    }    return ans;}int check5(int n){    ll ans=0;    for(int i=1;i<=n;++i){        for(int j=1;j<i;++j){            for(int k=1;k<j;++k){                for(int l=1;l<k;++l){                    for(int m=1;m<l;++m)                    if(check(i,j,k)||check(i,j,l)||check(i,k,l)||check(j,k,l)||check(i,j,m)||                       check(i,k,m)||check(i,l,m)||check(j,k,m)||check(j,l,m)||check(k,l,m)) ans++;                }            }        }    }    return ans;}int main(){    //freopen("read.txt","r",stdin);    ios::sync_with_stdio(false);    init();    int t,n,m;    int Case=0;    cin>>t;    ll ans;    while(t--)    {        //cout<<c[50][25]<<endl;        clr(mat,0);        cin>>n>>m;        for(int i=0;i<m;++i){            int x,y;            cin>>x>>y;            mat[x][y]=1;            mat[y][x]=1;        }        ans=0;        if(n>=6){            ans+=fun(n);            ans+=check3(n);            ans+=check4(n);            ans+=check5(n);        }        if(n==5){            ans+=check3(n);            ans+=check4(n);            ans+=check5(n);        }        if(n==4){            ans+=check3(n);            //cout<<ans<<endl;            ans+=check4(n);            //cout<<ans<<endl;        }        if(n==3) ans+=check3(n);        cout<<"Case #"<<++Case<<": "<<ans%mod<<endl;    }    return 0;}




原创粉丝点击