Friend-Graph(数学,思维)

该博客介绍了一个团队友谊评估问题,其中定义了好团队的条件:不存在三人互为朋友或互为非朋友的情况。给出团队人数和成员间的关系,需要判断团队是否符合好团队的标准。博主提供了两种解决方案:一种基于拉姆齐定理,另一种通过查找三元环。代码实现包括两种方法,分别用C++编写,以解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Problem Description
It is well known that small groups are not conducive of the development of a team. Therefore, there shouldn’t be any small groups in a good team.
In a team with n members,if there are three or more members are not friends with each other or there are three or more members who are friends with each other. The team meeting the above conditions can be called a bad team.Otherwise,the team is a good team.
A company is going to make an assessment of each team in this company. We have known the team with n members and all the friend relationship among these n individuals. Please judge whether it is a good team.
 

Input
The first line of the input gives the number of test cases T; T test cases follow.(T<=15)
The first line od each case should contain one integers n, representing the number of people of the team.( n3000 )

Then there are n-1 rows. The i th row should contain n-i numbers, in which number aij represents the relationship between member i and member j+i. 0 means these two individuals are not friends. 1 means these two individuals are friends.
 

Output
Please output ”Great Team!” if this team is a good team, otherwise please output “Bad Team!”.
 

Sample Input
  
  
1 4 1 1 0 0 0 1
 

Sample Output
  
  
Great Team!
 

Source
 题目思路:
这题可以用两种方法来写,第一种因为实际上是一个完全图,所以根据拉姆齐定理,大于等于6的直接输出no,剩下的暴力找三元环。
第二种根据题目意思很容易知道是找三元环了,对于每一个点我们去找符合题意的三元环,就是找一个有边的,找一个没有边的,最后去下重,然后判断是不是等于全部三元环的个数就行了。
第一种:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstring>
#include<iostream>
#include<sstream>
#include<cmath>
#include<vector>
#define LL long long
#define INF 0x3f3f3f3f
#define eps 1e-6
using namespace std;
using namespace std;
const LL mod = 1e9 + 7;
int mp[10][10];
int n;
int T;
int main()
{
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        memset(mp,0,sizeof(mp));
        if(n>=6){
        for(int i = 1;i<=n;i++){
            for(int j = 1;j<=n-i;j++){
                int x;
                scanf("%d",&x);
            }
        }
        }
        else{
            for(int i = 1;i<=n;i++){
            for(int j = 1;j<=n-i;j++){
                int x;
                scanf("%d",&x);
                mp[i][i+j] =x;
                mp[i+j][i] = x;
            }
        }
        }
        if(n>=6){
            puts("Bad Team!");
        }
        else{
                int falg = 1;
            for(int i = 1;i<=n;i++){
                for(int j = 1;j<=n;j++){
                    for(int k = 1;k<=n;k++){
                        if(i==j||j==k||i==k)
                            continue;
                        if(mp[i][j]==1&&mp[i][k]==1&&mp[j][k]==1){
                            falg= 0;
                            //cout<<"fuck1"<<endl;
                        }
                        if(mp[i][j]==0&&mp[i][k]==0&&mp[j][k]==0){
                            //cout<<i<<' '<<j<<' '<<k<<endl;
                            //cout<<"fuck2"<<endl;
                            falg = 0;
                        }
                    }
                }
            }
            if(falg)
                puts("Great Team!");
            else
                puts("Bad Team!");
        }
    }
}
第二种:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<memory.h>
#include<iostream>
#define eps 1e-6
#define LL long long
using namespace std;
int cnt[3005];
int n;
int T;
int main()
{
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        memset(cnt,0,sizeof(cnt));
        for(int i = 1;i<=n;i++){
            for(int j = 1;j<=n-i;j++){
                int x;
                scanf("%d",&x);
                if(x){
                    cnt[i]++;
                    cnt[i+j]++;
                }
            }
        }
        LL sum = (LL)n*(n-1)*(n-2)/6;
        LL sum2 = 0;
        for(int i = 1;i<=n;i++){
            sum2+=(LL)(cnt[i])*(n-cnt[i]-1);
        }
        if(sum2/2==sum)
            puts("Great Team!");
        else
            puts("Bad Team!");
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值