codeforces-574B
题目连接:http://codeforces.com/contest/574/problem/B
2 seconds
256 megabytes
standard input
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.
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.
If Richelimakieu can choose three musketeers, print the minimum possible sum of their recognitions. Otherwise, print "-1" (without the quotes).
5 61 21 32 32 43 44 5
2
7 42 13 65 11 7
-1
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的更多相关文章
- codeForces 574b Bear and Three Musketeers
一种巧妙到暴力方式,这题到抽象化:在一个无向图中,找一个度数和最小到三阶到圈. 首先对边进行枚举,这样确定了两个顶点,然后在对点进行枚举,找到一个可以构成三元圈到点,则计算他们到度数和. 最后保存最小 ...
- 【CodeForces 574B】Bear and Three Musketeers
[链接] 我是链接,点我呀:) [题意] [题解] 枚举每一条边(x,y) 然后再枚举y的出度z 看看g[x][z]是否等于1(表示联通) 如果等于1就说明找到了一个三元环,则尝试用它们的出度和-6更 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- 【活动】参加葡萄城控件主办的“谁是报表达人”知识评测活动,赢取iPad Mini2
一.参与资格 从事报表开发的博客园用户 二.活动时间 4月1日-4月30日 三. 活动形式 在活动期间,活动参与者只要回答从题库中随机抽出的与报表相关的六道题,就可以知道自己的报表知识等级.同时活动主 ...
- WIN8、WIN7访问Windows Server 2003服务器的数据库速度很慢、远程速度很慢的解决方法
原因是微软在WIN7开始上加入了网络速度限制.在控制台执行以下命令即可解决: netsh interface tcp set global autotuninglevel=disabled
- MQ消息中间件
MQ是什么? MQ是Message Queue消息队列的缩写.消息队列是一种应用程序对应用程序的通信方法.应用程序通过写和检索入列队的针对应用程序的数据(消息)来进行通信,而不需要专用连接来链接它们. ...
- jmeter察看结果树响应数据中文乱码解决办法
1.到jmeter目录文件中bin文件夹下找到jmeter.properties文件,该文件为jmeter配置文件.使用编辑工具打开它. 2.找到 #sampleresult.default.e ...
- 上手Caffe(二)
@author:oneBite本文简述如何在windows环境下,运行caffe的“hello world”例程体会适用caffe的流程:转换输入数据格式>在solver.prototxt中配置 ...
- php数组循环的三种方式
PHP 的遍历数组的三种方式:for循环.foreach循环.while.list().each()组合循环 PHP当中数组分为:索引数组[转换成json是数组]和关联数组[转换成json是对象] f ...
- URAL 1942 Attack at the Orbit
B - Attack at the Orbit Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & % ...
- CSS 的Hack 问题
1.什么是CSS hack? CSS hack是通过在CSS样式中加入一些特殊的符号,让不同的浏览器识别不同的符号(什么样的浏览器识别什么样的符号是有标准的,CSS hack就是让你记住这个标准),以 ...
- windows 10 change default program bug
windows 10 change default program bug https://www.isumsoft.com/windows-10/how-to-change-or-set-defau ...
- [bzoj] 1085 骑士精神 || ID-DFS
原题 找到最少的步数成为目标状态. IDDFS(限制层数的dfs)即可 #include<cstdio> #include<algorithm> using namespace ...