C - Friend-Graph HDU - 6152 三元环 & 拉姆齐定理
原题链接
题意:判断图和补图 是否含有三元环
拉姆齐定理
拉姆齐定理:
在一个大于等于6个点的 完全图中,用红蓝两色染色,一定存在一个红色或者蓝色的三角形。
或者说:在总人数大于等于6的时候,一定存在这样的三个人,他们相互认识,或者相互不认识。
所以
点数n大于等于6:直接输出bad team,否则进行暴力即可。
代码
#include <bits/stdc++.h>
using namespace std;
int n;
int g[6][6];
int check()
{
for ( int a = 1; a <= n; a++ )
for ( int b = a + 1; b <= n; b++ )
for ( int c = b + 1; c <= n; c++ )
if ( (g[a][b] && g[a][c] && g[b][c]) ||
(!g[a][b] && !g[a][c] && !g[b][c]) )
return true;
return false;
}
void solve()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
for (int j = i + 1; j <= n; j++)
{
int t;cin >> t;
if(n<6) g[i][j]=g[j][i]=t;
}
}
if (n >= 6)
cout<<"Bad Team!"<<endl;
else if (check())
cout<<"Bad Team!"<<endl;
else
cout<<"Great Team!"<<endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--)
solve();
return 0;
}
暴力题解
注意及时剪枝结束,不然会超时。
代码
#include <bits/stdc++.h>
using namespace std;
// #define int long long
// #define pii pair<int, int>
//***********************必不可少,不然内存超限
#define int short int
const int N = 3010;
const int mod = 1e9 + 7;
int n, m;
vector<int> g[N];
int vis[N];
int get_sum()
{
for (int i = 1; i <= n; i++)
{
for (int j : g[i])
if(g[i].size()>g[j].size() || (g[i].size()==g[j].size() && i>j))
vis[j] = 1;
for (int j : g[i])
if(g[i].size()>g[j].size() || (g[i].size()==g[j].size() && i>j))
for (int k : g[j])
if(g[j].size()>g[k].size() || (g[j].size()==g[k].size() && j>k))
if (vis[k])
return 1;
for (int j : g[i])
if(g[i].size()>g[j].size() || (g[i].size()==g[j].size() && i>j))
vis[j] = 0;
}
return 0;
}
void init()
{
m = 0;
for (int i = 1; i <= n; ++i)
g[i].clear();
}
void solve()
{
cin >> n;
init();
for (int i = 1; i <= n - 1; i++){
for (int j = i + 1; j <= n; j++){
int t;cin >> t;
if (t){
m++;
g[i].push_back(j);
g[j].push_back(i);
}
}
}
int ans1 = 0, ans2 = 0;
ans1 = get_sum();
m = n * (n - 1) / 2 - m;
//反向建边
for (int i = 1; i <= n; ++i)
{
memset(vis, 0, sizeof vis);
for (int j : g[i])
{
vis[j] = 1;
}
g[i].clear();
for (int j = 1; j <= n; j++)
{
if (vis[j] == 0)
g[i].push_back(j);
}
}
ans2 = get_sum();
if (ans1 == 0 && ans2 == 0)
cout << "Great Team!" << endl;
else
cout << "Bad Team!" << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--)
solve();
return 0;
}