HDU 5917 Instability(Ramsey定理)

版权声明:转载请注明出处:http://blog.csdn.net/yasola,谢谢 https://blog.csdn.net/Yasola/article/details/78256950

Instability

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

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 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.

Source
2016中国大学生程序设计竞赛(长春)-重现赛

Recommend
wange2014

题目大意

  有一个V(3V50)个点的图,问有多少个集合至少包含一个三元环或三个点之间没有任何边的补三元环。

解题思路

  Ramsey定理:6 个人中至少存在3人相互认识或者相互不认识。
  根据Ramsey定理可以知道我们可以直接算出n>5的集合数。对于3V5的情况,由于V只有50,所以可以暴力求解。

AC代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<set>
#include<vector>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;

const int MAXV=50+3;
const int MOD=1000000007;
int V, E;
bool maze[MAXV][MAXV];

void init()
{
    for(int i=0;i<V;++i)
        for(int j=0;j<V;++j)
            maze[i][j]=false;
}

inline bool judge(int a, int b, int c)//判定三个点是否构成三元环或相互独立
{
    return maze[a][b] && maze[a][c] && maze[b][c] || !maze[a][b] && !maze[a][c] && !maze[b][c];
}

inline void mod_add(int &a, const int &b)
{
    a+=b;
    if(a>=MOD)
        a-=MOD;
}

int main()
{
    int T_T;
    scanf("%d", &T_T);
    for(int cas=1;cas<=T_T;++cas)
    {
        scanf("%d%d", &V, &E);
        init();
        for(int i=0;i<E;++i)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            --u;
            --v;
            maze[u][v]=maze[v][u]=true;
        }
        int ans=V>5?((1ll<<V)-1-V-V*(V-1)/2-V*(V-1)*(V-2)/6-V*(V-1)*(V-2)*(V-3)/24-V*(V-1)*(V-2)*(V-3)*(V-4)/120)%MOD:0;//如果V>5,计算出n>5的所有集合
        for(int i=0;i<V;++i)
            for(int j=i+1;j<V;++j)
                for(int k=j+1;k<V;++k)
                {
                    if(judge(i, j, k))
                    {
                        mod_add(ans, 1+(V-k-1)+(V-k-1)*(V-k-2)/2);
                        continue;
                    }
                    for(int x=k+1;x<V;++x)
                    {
                        if(judge(x, i, j) || judge(x, i, k) || judge(x, j, k))
                        {
                            mod_add(ans, 1+V-x-1);
                            continue;
                        }
                        for(int y=x+1;y<V;++y)
                            if(judge(y, i, j) || judge(y, i, k) || judge(y, i, x) || judge(y, j, k) || judge(y, j, x) || judge(y, k, x))
                                mod_add(ans, 1);
                    }
                }
        printf("Case #%d: %d\n", cas, ans);
    }

    return 0;
}
阅读更多

没有更多推荐了,返回首页