原题链接
题意:判断图和补图 是否含有三元环
拉姆齐定理
拉姆齐定理:
在>=6个点的完全图中,用红蓝两色染色,一定存在一个红色或者蓝色的三角形。
所有n>=6的话直接输出bad team,否则进行暴力即可。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 20;
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 <= min(10, 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 && i < 10 && j < 10)
{
m++;
g[i].push_back(j);
g[j].push_back(i);
}
}
}
if (n > 6)
printf("Bad Team!\n");
else if (get_sum())
printf("Bad Team!\n");
else
printf("Great Team!\n");
}
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;
}
标签:HDU,return,int,Graph,init,vis,6152,&&,size
From: https://www.cnblogs.com/kingwz/p/16666511.html