Instability

Instability

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 463    Accepted Submission(s): 173


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
  
  
2 4 3 1 2 2 3 1 3 3 0
 

Sample Output
  
  
Case #1: 2 Case #2: 1
Hint
• In the first example, {1,2,3} and {1,2,3,4} , containing the subset {1,2,3} which is connected directly, are considered unstable. • In the second example, {1,2,3} is considered unstable because they are not pairwise connected directly.
一个集合里面只要有>=3个点两两相连或两两都不相连 就认为这个集合是稳定的,,求这样集合的总和 里面有个重要的定理 ramsey定理(百度吧) 世界上任意的6个人中 总有三个相互认识或都不认识 所以此题只有>=6个点都满足----------就有c(n,0)+c(n,1)+c(n,2)+..........+c(n,6)+c(n,7)+......c(n,n); 这个式子的总和就等于2^n(高中知识了) 大于5的都成立 所以只要判断3到5个点的 减去就就行了
Ramsey定理:6个人中至少3个人相互认识或相互不认识。
所以这对于这题,6以上的点就不用考虑了,直接暴力求3,4,5个点的情况。先求出所有的子集为 2^n 个(组合公式得来),然后减去不满足的即可。

#include<cstring>
#include<algorithm>
#include<cstdio>
int road[55][55];
const int Mod=1000000007;
long long fun(long long x,long long n)
{
    long long res=1;
    while(n>0)
    {
        if(n&1)
            res=(res*x)%Mod;
        x=(x*x)%Mod;
        n>>=1;
    }
    return res;
}
int check(int i,int j,int k)//判断是否存在三个点  全相连  或者  全不相连
{
	if(road[i][j]+road[i][k]+road[j][k]==0)
		return 1;
	if(road[i][j]+road[i][k]+road[j][k]==3)
		return 1;
	return 0;
}
int main()
{
	int cas;
	scanf("%d",&cas);
	int nn=0;
	while(cas--)
	{
		memset(road,0,sizeof(road));
		int n,m;
		scanf("%d%d",&n,&m);
		int u,v;
		for(int i=0;i<m;i++)
		{
			scanf("%d%d",&u,&v);
			road[u][v]=1;//预处理一下,便于判断
			road[v][u]=1;
		}
		long long ans=fun(2,n);
		ans=(ans-1)%Mod;//减去多加的空集,即C(n,0)=1;
		ans=(ans-n)%Mod;//减去集合里面只有1个元素的情况,即C(n,1)=n;
		ans=(ans-n*(n-1)/2)%Mod;//减去集合里面是2个的情况,即C(n,2)=n*(n-1)/2;
		for(int i=1;i<=n-2;i++)//减去不满足的
		{
			for(int j=i+1;j<=n-1;j++)
			{
				for(int k=j+1;k<=n;k++)
				{
					if(check(i,j,k)==0)
						ans=(ans-1)%Mod;
				}
			}
		}
		for(int i=1;i<=n-3;i++)//减去不满足的
		{
			for(int j=i+1;j<=n-2;j++)
			{
				for(int k=j+1;k<=n-1;k++)
				{
					for(int ll=k+1;ll<=n;ll++)
					{
                       //所有的情况都要考虑,比如i,j,k,ll这四个点就有(i,j,k)(i,j,ll)(j,k,ll)(i,k,ll)这四种
					   if(check(i,j,k)==0&&check(i,j,ll)==0&&check(i,k,ll)==0&&check(ll,j,k)==0)
						ans=(ans-1)%Mod;
					}
				}
			}
		}
		for(int i=1;i<=n-4;i++)//减去不满足的
		{
			for(int j=i+1;j<=n-3;j++)
			{
				for(int k=j+1;k<=n-2;k++)
				{
					for(int ll=k+1;ll<=n-1;ll++)
					{
						for(int lll=ll+1;lll<=n;lll++)
						{
						    //同上
							if(check(i,j,k)==0&&check(i,j,ll)==0&&check(i,j,lll)==0&&
								check(i,k,ll)==0&&check(i,k,lll)==0&&check(i,ll,lll)==0
								&&check(ll,j,k)==0&&check(lll,j,k)==0&&check(k,ll,lll)==0&&check(j,ll,lll)==0)
						      ans=(ans-1)%Mod;
						}
					}
				}
			}
		}
		printf("Case #%d: %lld\n",++nn,ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值