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的自己,所以我們只需要預處理出組合數,即可Cni(i6)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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章