HDU - 5917-Instability · znzryb
## 思路讲解
> 根据 Ramsey定理,大于等于6个的集合,肯定存在一个子集的边都是红色或者都是蓝色,即为团还是为孤立点;
> 即6 个人中至少存在3人相互认识或者相互不认识。
```cpp
FOR(i,6,N){ // 根据Ramsey定理的结论
ans+=C(N,i);
ans%=mod;
}
```
然后3-5个点的枚举就可以了
## AC代码
[https://vjudge.net/solution/60814540](https://vjudge.net/solution/60814540)
::: 源代码
```cpp
// Problem: Instability
// Contest: Virtual Judge - HDU
// URL: https://vjudge.net/problem/HDU-5917
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define all(vec) vec.begin(),vec.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int) a.size())
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define getFro(vec) (vec.empty()?0:vec.front())
#define getBac(vec) (vec.empty()?0:vec.front())
#define debug(var) cerr << #var <<":"<<var<<"\n";
#define DEBUG(variable) \
do { \
std::cerr << #variable << ":"; \
for (const auto& elem : variable) { \
std::cerr << elem << " "; \
} \
std::cerr << "\n"; \
} while (0)
#define uniVec(var) \
do { \
sort(var.begin(),var.end());\
var.resize(unique(var.begin(),var.end())-var.begin());\
} while (0)
#define debugN(var,N) \
do{ \
std::cerr<<#var<<":"; \
FOR(i,0,N){ \
std::cerr<<var[i]<<" "; \
} \
std::cerr<<"\n"; \
}while(0)
#define debugMap(variable) \
do { \
std::cerr << #variable << ":\n"; \
for (const auto& pair : variable) { \
std::cerr << " " << pair.first << " => " << pair.second << "\n"; \
} \
std::cerr << std::endl; \
} while (0)
#define lson(var) (var<<1)
#define rson(var) ((var<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
typedef double DB;
typedef long double LD;
typedef pair<DB,DB> pdd;
typedef pair<ll,bool> plb;
constexpr ll MAXN=static_cast<ll>(1e6)+10,INF=static_cast<ll>(1e18)+3;
constexpr ll mod=static_cast<ll>(1e9)+7;
constexpr double eps=1e-8;
ll N,M,K,T,oT;
bool G[55][55];
/*
*/
ll frac[60],infrac[60];
inline ll C(ll n,ll k){
ll res=frac[n]*infrac[n-k];
res%=mod;
res*=infrac[k];
res%=mod;
return res;
}
inline ll binpow(ll a,ll k){
ll res=1;
while(k){
if(k&1) res*=a;
a*=a;
a%=mod;
res%=mod;
k>>=1;
}
return res;
}
inline void solve(){
cin>>N>>M;
FOR(i,0,N+3){
FOR(j,0,N+3){
G[i][j]=false;
}
}
FOR(i,1,M){
ll u,v;
cin>>u>>v;
G[u][v]=true;
G[v][u]=true;
}
ll ans=0;
FOR(i,6,N){ // 根据Ramsey定理的结论
ans+=C(N,i);
ans%=mod;
}
FOR(i,1,N){
FOR(j,i+1,N){
FOR(k,j+1,N){
bool alTrue=false;
// if(G[i][j]==false && G[i][k]==false && G[k][j]==false){
// ans+=1;
// alTrue=true;
// }else if(G[i][j] && G[i][k] && G[j][k]){
// ans+=1;
// alTrue=true;
// }
auto cal=[&](ll i,ll j,ll k){
if(G[i][j]==false && G[i][k]==false && G[k][j]==false){
alTrue=true;
}
if(G[i][j] && G[i][k] && G[j][k]){
alTrue=true;
}
};
cal(i,j,k);
if(alTrue){
ans+=1;
}
FOR(i4,k+1,N){
alTrue=false;
cal(i,j,k);
cal(i,j,i4);
cal(i,i4,k);
cal(i4,j,k);
if(alTrue){
ans+=1;
}
FOR(i5,i4+1,N){
alTrue=false;
cal(i,j,k);
cal(i,j,i4);
cal(i,i4,k);
cal(i4,j,k);
cal(i,j,i5);
cal(i,i5,k);
cal(i5,j,k);
cal(i5,j,i4);
cal(i5,i4,k);
cal(i5,i,i4);
if(alTrue){
ans+=1;
}
}
}
}
}
}
ans%=mod;
if(ans<0) ans+=mod;
ll Case=oT-T;
cout<<"Case #"<<Case<<": "<< ans <<"\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>T;
oT=T;
frac[0]=1;
FOR(i,1,50){
frac[i]=frac[i-1]*i;
frac[i]%=mod;
}
infrac[0]=1;
FOR(i,1,50){
infrac[i]=binpow(frac[i],mod-2);
}
#ifdef LOCAL
debugN(frac,50);
cerr<<"\n";
debugN(infrac,50);
debug(C(4,2));
#endif
while(T--){
solve();
}
// solve();
return 0;
}
/*
AC https://vjudge.net/solution/60814540
1
7 8
1 2
2 3
3 4
4 5
5 6
6 7
1 7
1 4
ans:Case #1: 53
1
5 6
1 2
2 3
3 4
2 5
4 5
1 5
ans: Case #1: 4
*/
```
:::
## 心路历程(WA,TLE,MLE……)
小心 infrac[0]=0;
```cpp
infrac[0]=1;
FOR(i,1,50){
infrac[i]=binpow(frac[i],mod-2);
}
```