CF div2 318 B

B. Bear and Three Musketeers

Do you know a story about the three musketeers? Anyway, you will learn about its origins now.

Richelimakieu is a cardinal in the city of Bearis. He is tired of dealing with crime by himself. He needs three brave warriors to help him to fight against bad guys.

There are n warriors. Richelimakieu wants to choose three of them to become musketeers but it‘s not that easy. The most important condition is that musketeers must know each other to cooperate efficiently. And they shouldn‘t be too well known because they could be betrayed by old friends. For each musketeer his recognition is the number of warriors he knows, excluding other two musketeers.

Help Richelimakieu! Find if it is possible to choose three musketeers knowing each other, and what is minimum possible sum of their recognitions.

Input

The first line contains two space-separated integers, n and m (3 ≤ n ≤ 4000, 0 ≤ m ≤ 4000) — respectively number of warriors and number of pairs of warriors knowing each other.

i-th of the following m lines contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi). Warriors ai and bi know each other. Each pair of warriors will be listed at most once.

Output

If Richelimakieu can choose three musketeers, print the minimum possible sum of their recognitions. Otherwise, print "-1" (without the quotes).

Sample test(s)

Input

5 61 21 32 32 43 44 5

Output

2

Input

7 42 13 65 11 7

Output

-1

Note

In the first sample Richelimakieu should choose a triple 1, 2, 3. The first musketeer doesn‘t know anyone except other two musketeers so his recognition is 0. The second musketeer has recognition 1 because he knows warrior number 4. The third musketeer also has recognition 1 because he knows warrior 4. Sum of recognitions is 0 + 1 + 1 = 2.

The other possible triple is 2, 3, 4 but it has greater sum of recognitions, equal to 1 + 1 + 1 = 3.

In the second sample there is no triple of warriors knowing each other.

题目大意是让找一个由三个点组成的环并且三个点的度数加起来“最小”,这里的“最小”是指成环的三个顶点之间的边所形成的度数不算,也就是三个顶点成环的最小度数-6 就是要求的答案,如果没有这样的环输出 -1.

看了一下顶点数才4000,边也才4000 用邻接矩阵存图,然后再用一个数组记录一下各个顶点的度数,然后直接枚举每个顶点取总度数最小的。代码如下

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>

using namespace std;

const int M = 10005;
const int maxn = 5000;
typedef long long ll;
const int inf = 9999999;

vector<int>G[maxn];
queue<int>Q;
stack<int>st;

int a[maxn][maxn];
int vis[maxn];

int main()
{

    int n,m;

    scanf("%d%d",&n,&m);
    int u,v;
    memset(vis,0,sizeof(vis));
    memset(a,0,sizeof(a));
    for(int i=1;i<=m;i++){
        scanf("%d%d",&u,&v);
        a[u][v] = a[v][u] = 1;
        vis[u]++;
        vis[v]++;
    }
   int  ans = inf;
    for(int i=1;i<=n;i++){
        for(int j=i+1;j<=n;j++){
            if(a[i][j]){
                for(int k=j+1;k<=n;k++){
                    if(a[i][k]&&a[k][j]){
                        ans = min(ans,vis[i]+vis[j]+vis[k]);
                    }
                }
            }
        }
    }
    if(ans == inf ) printf("-1\n");
    else printf("%d\n",ans-6);

    return 0;
}

时间: 2024-09-30 06:13:29

CF div2 318 B的相关文章

CF div2 318 C

C. Bear and Poker Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollar

CF div2 318 D

D. Bear and Blocks Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of hi identical blocks. For clarification see picture for the first sample. Limak will rep

CF div2 318 A

A. Bear and Elections Limak is a grizzly bear who desires power and adoration. He wants to win in upcoming elections and rule over the Bearland. There are n candidates, including Limak. We know how many citizens are going to vote for each candidate.

Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)

以后每做完一场CF,解题报告都写在一起吧 暴力||二分 A - Bear and Elections 题意:有n个候选人,第一个候选人可以贿赂其他人拿到他们的票,问最少要贿赂多少张票第一个人才能赢 分析:正解竟然是暴力!没敢写暴力,卡了很久,导致这场比赛差点爆零!二分的话可以优化,但对于这题来说好像不需要... 收获:以后CF div2的A题果断暴力 代码(暴力): /************************************************ * Author :Runni

CF1287 div2题解

前言 昨夜打CF div2,思涨分之事.然脑未上线,BC题皆挂,仅A两道. 特写此篇,以记此耻. 所有题题面:https://codeforces.com/contest/1287/problems A. Angry Students 题面:https://codeforces.com/contest/1287/problem/A 题解:直接扫一遍,记录\(A\)后面最长的一段\(P\)即可. 时间复杂度:\(O(n)\). 代码:略 B. Hyperset 题面:https://codefor

HDU-SupportOrNot训练实录

菜鸡队训练实录. 现场赛记录: 2016:[名称:奖项/排名] ZJPSC:Gold/1 CCPC中南邀请赛:Gold/1 ICPC Dalian:Gold/24 ICPC Beijing:??? CCPC Final:??? ICPC China-Final:??? To do List: 所有人需要提高效率 减小罚时 三人组队训练时必须用指定Ubuntu电脑敲题,其他两台电脑只能读题.读代码 为提升代码能力,poursoul和_ilovelife尽量做到每天solo一套简单GYM,也可以视情

我的ACM 之大一

当我开始写文章时,已经快12点了,为什么不睡觉?因为还有半小时有CF div2 . 趁着现在有空,有感而发,回顾我的ACM之大一.我第一次接触ACM是在叶老师的宣讲会上,那时就被忽悠了,我单纯地认为参加了ACM才能追上国内高校顶尖学子,不进ACM大学平平淡淡,我就平平庸庸了.于是,那天开始我就开始刷oj水题200道,想赶快加入ACM,结果我是第一个刷到200题的,然而这并没有什么卵用!校赛被虐,杭电比赛也一题惨淡收场,感觉还有很多人比我厉害,我还是个平庸的人:( 后来我参加寒假集训,学了几个使用

我是逗比!!!!!!

set和map都不怎么会用的逗比就这样完挂了第一次cf div2 D #include <cstdio> #include <cstring> #include <set> #include <iostream> #include <algorithm> #include <cstdlib> using namespace std; set<int> a,b,c; int aa[400001],bb[400001]; in

牛客小白月赛2 总结

随便找了牛客网上的一个比赛打,看了一会题目,很快就嘴巴AK了,突然有点小激动,最后发现牛客小白月赛就是手速场,题目难度在CF DIV2 A~C ,总共10题,一不小心又做了一波水题.(可惜嘴巴选手实际只敲了3题的代码,逃) A 数字方阵 构造题,打表找规律,或者随机,详见http://www.cnblogs.com/Surrender/p/8969880.html B 小马过河 解方程 C 真真假假 用map D 虚虚实实 欧拉路径 E 是是非非 nim游戏 F 黑黑白白 树上博弈,用最大.最小