Instability HDU - 5917(Ramsey定理)

Instability HDU - 5917

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 A⊆S(|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 (3≤n≤50) and m (1≤m≤n(n−1)/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个(或更多的)人中,或者有3个人,他们中的每两个人都互相认识,或者有3个人,他们中的每两个人都彼此不认识。

那么这个定理就是解题的关键,即如果我们在n个点的全集中,任意选取6个点或者6个以上的点,那么这个选取的子集一定是一个unstable的自己,所以我们只需要预处理出组合数,即可 C n i ( i 6 ) C_n^i(i \ge 6)

那么只剩下选取3个点(至少为3个点),4个点,5个点作为子集的时候了,这个时候直接暴力就行,看是能有三个点互相两两相连或者互相两两均不相连。

code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 50 + 10;
const int MOD = 1e9+7;
int n,m,w[N][N];
bool ok3(int a,int b,int c){
    if(w[a][b] && w[a][c] && w[b][c]) return true;//三个人中每两个人都相互认识
    if(!w[a][b] && !w[a][c] && !w[b][c]) return true;//三个人中每两个人都相互不认识
    return false;
}
bool ok4(int a,int b,int c,int d){
    if(ok3(a,b,c) || ok3(a,b,d) || ok3(a,c,d) || ok3(b,c,d)) return true;
    return false;
}
bool ok5(int a,int b,int c,int d,int e){
    if(ok4(a,b,c,d) || ok4(a,b,c,e) || ok4(a,b,e,d) || ok4(a,c,e,d) || ok4(e,b,c,d)) return true;
    return false;
}
ll C[55][55];
void init(){
    memset(C,0,sizeof(C));
    C[0][0] = 1;
    for(int i = 1; i <= 50; i++){
        C[i][0] = 1;
        for(int j = 1; j <= 50; j++){
            C[i][j] = (C[i-1][j] + C[i-1][j-1]) % MOD;
        }
    }
}
int main(){
    init();
    int T,cas = 0;
    scanf("%d",&T);
    while(T--){
        int n,m;
        scanf("%d%d",&n,&m);
        memset(w,0,sizeof(w));
        int u,v;
        for(int i = 1; i <= m;  i++){
            scanf("%d%d",&u,&v);
            w[u][v] = w[v][u] = 1;
        }
        ll ans = 0;
        for(int i = 1; i <= n; i++){//暴力枚举3个点的子集
            for(int j = i+1; j <= n; j++){
                for(int k = j+1; k <= n; k++){
                    if(ok3(i,j,k)) ans++;
                }
            }
        }
        for(int i = 1; i <= n; i++){//暴力枚举4个点的子集
            for(int j = i+1; j <= n; j++){
                for(int k = j+1; k <= n; k++){
                    for(int k1 = k+1; k1 <= n; k1++){
                        if(ok4(i,j,k,k1)) ans++;
                    }
                }
            }
        }
        for(int i = 1; i <= n; i++){//暴力枚举5个点的子集
            for(int j = i+1; j <= n; j++){
                for(int k = j+1; k <= n; k++){
                    for(int k1 = k+1; k1 <= n; k1++){
                        for(int k2 = k1+1; k2 <= n; k2++){
                            if(ok5(i,j,k,k1,k2)) ans++;
                        }
                    }
                }
            }
        }
        for(int i = 6; i <= n; i++){//6个点及其以上直接组合数算种类数
            ans = (ans + C[n][i]) % MOD;
        }
        printf("Case #%d: %lld\n",++cas,ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/codeswarrior/article/details/82841995