跳到正文

题解

题解:CF574B Bear and Three Musketeers

zhangzirui66约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
获赞
2
题目
CF574B

版本

  1. v1当前

思路

互相认识没有传递关系,所以 AA 认识 BB,BB 认识 CC,但 AA 不一定认识 CC。直接三层循环模拟即可。

C++
#include<bits/stdc++.h>
using namespace std;
int n, m, a[4005], b[4005][4005], ans = 2e9;//a数组是认识的人数,b是邻接矩阵
int main(){
    cin >> n >> m;
    for(int i = 1; i <= m; i ++){
        int x, y;
        cin >> x >> y;
        b[x][y] = 1;
        b[y][x] = 1;//无向图,所以有两次
        a[x] ++;
        a[y] ++;
    }
    for(int i = 1; i <= n; i ++)
        for(int j = i + 1; j <= n; j ++){
            if(b[i][j]){
                for(int k = j + 1; k <= n; k ++){
                    if(b[i][k] && b[j][k]) ans = min(ans, a[i] + a[j] + a[k] - 6);//他们三个互相认识,要减去6
                }
            }
        }
    if(ans != 2e9) printf("%d\n", ans);
    else puts("-1");
    return 0;
}
原文