HDU - 5917 - Instability -(Ramsey定理)

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.
题意:给出n个点,n个点之间用m条无向边相连,问其中有多少个子集满足子集中存在3个顶点的完全图(3个点之间两两相连)或者存在两两之间互不相连的3个点,

由Ramsey定理知r(3,3)=6,即当完全图的定点数大于等于6时,用两种颜色给图中的边染色,必定存在全为一种颜色的k3或全为另一种颜色的k3

所以当n>=6时,从n中任取>=6的子集,都满足提努要求

但n<6时,暴力枚举一下

Ramesy定理可参考:Ramsey's Theorem

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<math.h>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 55
#define mod 1000000007
ll c[M][M];
bool ok[M][M];
int n,m;
ll ans;

void init() //预计算组合数
{
    int i,j;
    c[0][0]=c[1][0]=c[1][1]=1;
    for(i=2;i<M;i++)
        for(j=0;j<=i;j++)
    {
        if(j==0||j==i)
            c[i][j]=1;
        else
            c[i][j]=c[i-1][j]+c[i-1][j-1];
        c[i][j]=c[i][j]%mod;
    }
}

bool work(int num,int a[])
{
    int i,j,k;
    for(i=0;i<num;i++)
        for(j=i+1;j<num;j++)
            for(k=j+1;k<num;k++)
    {
        if(ok[a[i]][a[j]]&&ok[a[i]][a[k]]&&ok[a[j]][a[k]]) //三个点之间两两有路
            return true;
        if(!ok[a[i]][a[j]]&&!ok[a[i]][a[k]]&&!ok[a[j]][a[k]]) //三个点之间两两无路
            return true;
    }
    return false;
}


void solve()
{
    int a[5];
    for(a[0]=1;a[0]<=n;a[0]++)
    {
        for(a[1]=a[0]+1;a[1]<=n;a[1]++)
        {
            for(a[2]=a[1]+1;a[2]<=n;a[2]++)
            {
                if(work(3,a))
                    ans++;
                for(a[3]=a[2]+1;a[3]<=n;a[3]++)
                {
                    if(work(4,a))
                        ans++;
                    for(a[4]=a[3]+1;a[4]<=n;a[4]++)
                    {
                        if(work(5,a))
                            ans++;
                    }
                }
            }
        }
    }
    ans=ans%mod;
}

int main()
{
    init();
    int T,x,y,i,cas=0;
    scanf("%d",&T);
    while(T--)
    {
        memset(ok,false,sizeof(ok));
        scanf("%d%d",&n,&m);
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&x,&y);
            ok[x][y]=ok[y][x]=true;
        }
        ans=0;
        if(n>=6)
        {
            for(i=6;i<=n;i++) //从n个点中任取作为子集
            {
                ans+=c[n][i];
                ans=ans%mod;
            }
        }
        solve();

        printf("Case #%d: %I64d\n",++cas,ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值