题目连接:http://codeforces.com/contest/574/problem/B

B. Bear and Three Musketeers
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 ≤ nai ≠ 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).

Examples
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 1because 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.

题目大意:有一堆火枪手,从1-n编号,输入告诉某m对火枪手相互认识,要求找出一在这些火枪手中找出一个符合下述条件的三人小队。

条件:1.三人必须互相认识

   2.三人认识的人个数总和最小(不包括队中认识的二人)

解题思路:

对于每一个火枪手抽象出三个属性:1.自己的编号。2.认识的人的编号。3.认识的人的个数。

思考以后发现可以抽象成图,每一个火枪手为节点,认识关系为边,就可以形成一个无向图。

而题目就转化成了给定一个无向图求三元环的个数。

因为数据范围较小,所以暴力枚举就可过。

代码如下:

#include<bits/stdc++.h>

using namespace std;

][]={};

int main()
{
    int  n,m,a,b;
    ;
    ]={};
    scanf("%d%d",&n,&m);
    ;i<m;i++)
    {
        scanf("%d%d",&a,&b);
        g[a][b]=g[b][a]=;
        num[a]++;
        num[b]++;
    }
    ;i<=n;i++)
    {
        ;j<=n;j++)
        {
            )
            {
                ;k<=n;k++)
                {
                    &&k!=i&&g[k][i]!=)
                    {
                        ;
                        res=min(res,sum);
                    }
                }
            }
        }
    }
    )
        res=-;
    cout<<res<<endl;
}

codeforces-574B的更多相关文章

  1. codeForces 574b Bear and Three Musketeers

    一种巧妙到暴力方式,这题到抽象化:在一个无向图中,找一个度数和最小到三阶到圈. 首先对边进行枚举,这样确定了两个顶点,然后在对点进行枚举,找到一个可以构成三元圈到点,则计算他们到度数和. 最后保存最小 ...

  2. 【CodeForces 574B】Bear and Three Musketeers

    [链接] 我是链接,点我呀:) [题意] [题解] 枚举每一条边(x,y) 然后再枚举y的出度z 看看g[x][z]是否等于1(表示联通) 如果等于1就说明找到了一个三元环,则尝试用它们的出度和-6更 ...

  3. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  6. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  7. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  8. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  9. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  10. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

随机推荐

  1. 孤荷凌寒自学python第三十二天python的代码块中的异常的捕获

    孤荷凌寒自学python第三十二天python的代码块中的异常的捕获 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天简单了解了Python的错误陷阱,了解到其与过去学过的其它语言非常类似 ...

  2. mongodb 部署

    安装mongodb-3.4 1)将安装包上传至服务器 2)对压缩文件进行解压 tar -zxvf mongodb-linux-x86_64-suse12-v3.4-latest.tar.gz 3)把解 ...

  3. java中newInstance和new(转)

    在Java开发特别是数据库开发中,经常会用到Class.forName( )这个方法.通过查询Java Documentation我们会发现使用Class.forName( )静态方法的目的是为了动态 ...

  4. HDU 4665 Unshuffle DFS找一个可行解

    每层找一对相等的整数,分别放在两个不同的串中. 参考了学弟的解法,果断觉得自己老了…… #include <cstdio> #include <cstring> #includ ...

  5. 第二阶段团队冲刺-three

    昨天: 修复博客作业查询功能. 今天: 绘制logo. 遇到的问题: 无.

  6. 贪吃蛇StringBuilder 和 定时器

    ConsoleKeyInfo info = Console.ReadKey(); while (true) { if (info.Key == ConsoleKey.UpArrow)//只能输入一次但 ...

  7. SQL查询oracle的nclob字段

    使用CONTAINS关键字查询NCLOB字段 SELECT  FORMATTED_MESSAGE    FROM     TBL_LOG WHERE     CONTAINS(FORMATTED_ME ...

  8. warning MSB3162: 所选的“Microsoft Report Viewer 2012 Runtime”项需要“Microsoft.SqlServer.SQLSysClrTypes.11.0”。在“系统必备”对话框中选择缺少的系统必备组件,或者为缺少的系统必备组件创建引导程序包。

    warning MSB3162: 所选的“Microsoft Report Viewer 2012 Runtime”项需要“Microsoft.SqlServer.SQLSysClrTypes.11. ...

  9. SVN基本介绍

    SVN是一种项目合作开发的软件,参与项目的人员可以在不同的地方实现文件和目录的超时空共享. 两个重要的概念: 1.配置库(Repository) SVN的核心是配置库,储存所有的数据,配置库按照文件树 ...

  10. LeetCode -- Sum Root to Leaf NNumbers

    Related Links: Path Sum: http://www.cnblogs.com/little-YTMM/p/4529982.html Path Sum II: http://www.c ...