题目连接: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 g[4005][4005]={0};int main()
{int  n,m,a,b;int res=999999;int num[4005]={0};scanf("%d%d",&n,&m);for(int i=0;i<m;i++){scanf("%d%d",&a,&b);g[a][b]=g[b][a]=1;num[a]++;num[b]++;}for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){if(g[i][j]!=0){for(int k=1;k<=n;k++){if(g[j][k]!=0&&k!=i&&g[k][i]!=0){int sum=num[i]+num[j]+num[k]-6;res=min(res,sum);}}}}}if(res==999999)res=-1;cout<<res<<endl;
}

转载于:https://www.cnblogs.com/cryingrain/p/7594135.html

codeforces-574B相关推荐

  1. 【CodeForces - 574B】Bear and Three Musketeers (枚举边,思维,优秀暴力)

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

  2. codeforces 574B 暴力+复杂度分析

    题目: B. Bear and Three Musketeers time limit per test 2 seconds memory limit per test 256 megabytes i ...

  3. 【CodeForces 574B】Bear and Three Musketeers

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

  4. 《爱乐之城》:两个追梦人令人唏嘘又动容的爱情故事

    潦倒的爵士乐钢琴家遇到落魄的服务员兼龙套演员,同是天涯沦落人.彼此吸引,谈吐默契,塞巴斯蒂安坚持做复古爵士乐,却饱受白眼,连一份稳定的工作都没有.米娅从法学院辍学成了咖啡服务员,各个剧组面试,却处处碰 ...

  5. CodeForces 375D Tree and Queries

    传送门:https://codeforces.com/problemset/problem/375/D 题意: 给你一颗有根树,树上每个节点都有其对应的颜色,有m次询问,每次问你以点v为父节点的子树内 ...

  6. 「日常训练」Bad Luck Island(Codeforces Round 301 Div.2 D)

    题意与分析(CodeForces 540D) 是一道概率dp题. 不过我没把它当dp做... 我就是凭着概率的直觉写的,还好这题不算难. 这题的重点在于考虑概率:他们喜相逢的概率是多少?考虑超几何分布 ...

  7. 【codeforces 812C】Sagheer and Nubian Market

    [题目链接]:http://codeforces.com/contest/812/problem/C [题意] 给你n个物品; 你可以选购k个物品;则 每个物品有一个基础价值; 然后还有一个附加价值; ...

  8. CodeForces 获得数据

    针对程序的输出可以看见 CodeForces :当输入.输出超过一定字符,会隐藏内容 所以:分若干个程序进行输入数据的获取 1. 1 for (i=1;i<=q;i++) 2 { 3 scanf ...

  9. codeforces水题100道 第二十七题 Codeforces Round #172 (Div. 2) A. Word Capitalization (strings)...

    题目链接:http://www.codeforces.com/problemset/problem/281/A 题意:将一个英文字母的首字母变成大写,然后输出. C++代码: #include < ...

  10. CodeForces 595A

    题目链接: http://codeforces.com/problemset/problem/595/A 题意: 一栋楼,有n层,每层有m户,每户有2个窗户,问这栋楼还有多少户没有睡觉(只要一个窗户灯 ...

最新文章

  1. 探讨 | 目前SLAM存在的问题
  2. ExecutorService为创建的线程池ExecutorService pool = Executors.newFixedThreadPool(POOL_SIZE)
  3. 使用FileZilla Server轻松搭建个人FTP服务器
  4. jqueryvar语句_你真的掌握变量和类型了吗
  5. Angular.js示例应用程序
  6. 召回离线评估指标(一)
  7. c语言做简单的水果店程序,怎么开发一款生鲜水果小程序?水果店+小程序该如何组合运营?...
  8. windows下批处理删除文件及注册表项
  9. 书单丨5本书带你学习Kubernetes,掌控云计算的未来
  10. 2.RabbitMQ实战 --- 理解消息通信
  11. 计算机到开机画面,更改win7开机动画如何操作_win7电脑开机动画怎么设置
  12. 超星阅读器pdz文件打印转pdf文件
  13. 含泪整理最优质草食动物unity3d模型素材,你想要的这里都有
  14. MPB:南农韦中组-​根系分泌物调控土壤微生物群落结构和功能的研究方法
  15. 主板aspm关闭_【装机加人品】主板如何开启与关闭超线程技术?
  16. USB2.0系列(锆石科技FPGA)
  17. 直接耦合的互补输出级
  18. Spring boot集成Spring-data-Jpa中遇到的问题
  19. Android 9.0 (P)
  20. 如何制作视频抠图?制作视频抠像的教程分享给你

热门文章

  1. vim加载systemverilog语法高亮
  2. 2019.7.13--jzDay9
  3. FineReport——登录不到决策系统
  4. 为什么电脑运行越来越慢?解决方法又是什么呢?
  5. 扫地机器人的特点描写_扫地机器人的特点是什么 扫地机器人的原理
  6. 一周市场摘抄20210208
  7. 二十四节气-冬至,海报/文案分享,一口饺子,一口吉。
  8. 通俗易懂_汉诺塔(java递归实现)
  9. 去掉linux 打印信息
  10. 一个大专生的java从业心路历程