codeForces 574b Bear and Three Musketeers
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
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 Input
5 6
1 2
1 3
2 3
2 4
3 4
4 5
2
7 4
2 1
3 6
5 1
1 7
-1
#include<iostream>
#include<stdio.h>
using namespace std;
const int maxn = ;
struct Node{
int a,b;
}edg[maxn];
int mapp[maxn][maxn];
int deg[maxn];
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
mapp[i][j]=;
for(int i=;i<=n;i++) deg[i]=;
for(int i=;i<m;i++){
int tmp1,tmp2;
scanf("%d%d",&tmp1,&tmp2);
edg[i].a=tmp1;
edg[i].b=tmp2;
mapp[tmp1][tmp2]=mapp[tmp2][tmp1]=;
deg[tmp1]++;
deg[tmp2]++;
}
int inf=0x7fffffff;
int ans=inf;
for(int i=;i<m;i++){
for(int j=;j<=n;j++){
if(mapp[j][edg[i].a]&&mapp[j][edg[i].b]){
ans=min(ans,(deg[j]+deg[edg[i].a]+deg[edg[i].b]));
}
}
}
if(ans<inf){
printf("%d\n",ans-);
}else{
printf("-1\n");
}
return ;
}
codeForces 574b Bear and Three Musketeers的更多相关文章
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B. Bear and Three Musketeers 枚举
B. Bear and Three Musketeers ...
- Codeforces 385C Bear and Prime Numbers
题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...
- Codeforces 385B Bear and Strings
题目链接:Codeforces 385B Bear and Strings 记录下每一个bear的起始位置和终止位置,然后扫一遍记录下来的结构体数组,过程中用一个变量记录上一个扫过的位置,用来去重. ...
- Codeforces 680D Bear and Tower of Cubes 贪心 DFS
链接 Codeforces 680D Bear and Tower of Cubes 题意 求一个不超过 \(m\) 的最大体积 \(X\), 每次选一个最大的 \(x\) 使得 \(x^3\) 不超 ...
- Codeforces 385C Bear and Prime Numbers(素数预处理)
Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...
- [Codeforces 639F] Bear and Chemistry (Tarjan+虚树)(有详细注释)
[Codeforces 639F] Bear and Chemistry(Tarjan+虚树) 题面 给出一个n个点,m条边的无向图(不保证连通,可能有自环和重边),有q次询问,每次询问给出p个点和q ...
- 【CodeForces 574B】Bear and Three Musketeers
[链接] 我是链接,点我呀:) [题意] [题解] 枚举每一条边(x,y) 然后再枚举y的出度z 看看g[x][z]是否等于1(表示联通) 如果等于1就说明找到了一个三元环,则尝试用它们的出度和-6更 ...
- Codeforces Round #318 (Div. 2) B Bear and Three Musketeers (暴力)
算一下复杂度.发现可以直接暴.对于u枚举a和b,判断一下是否连边,更新答案. #include<bits/stdc++.h> using namespace std; int n,m; ; ...
- Codeforces 791B Bear and Friendship Condition(DFS,有向图)
B. Bear and Friendship Condition time limit per test:1 second memory limit per test:256 megabytes in ...
随机推荐
- LaTeX之参考文献的写法
在编写latex文件时,参考文献是个比较头疼的问题,以前自己写的时候总是用 \begin{thebibliography}\bibitem author,article, year, vol,\end ...
- Hive:用Java代码通过JDBC连接Hiveserver
参考https://www.iteblog.com/archives/846.html 1.hive依赖hadoop,将hdfs当作文件存储介质,那是否意味着hive需要知道namenode的地址? ...
- java数据库编程——读写LOB、可滚动和可更新的结果集、元数据
java 数据库编程 1. 读写LOB 除了数字.字符串和日期之外,许多数据库还可以存储大对象,例如图片或其它数据.在SQL中,二进制大对象称为BLOB,字符型大对象称为CLOB. 要读取LOB,需要 ...
- Python学习1:使用Aptana构建Python开发环境
使用Aptana构建Python开发环境 下载Aptana: http://www.aptana.com/products/studio3/download http://www.newasp.net ...
- Add Two Numbers(from leetcode python 链表)
给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...
- "com.android.ide.s.ProcessException:Process 'cand 'C:\Program Files\Java\jdk1.8.0_60\bin\java.exe'' finished with non-zero exit value 2"
使用Android Studio 出现该问题: "com.android.ide.common.process.ProcessException: org.gradle.process.in ...
- 什么是JSONP?
一.先说说JSONP是怎么产生的: 其实网上关于JSONP的讲解有很多,但却千篇一律,而且云里雾里,对于很多刚接触的人来讲理解起来有些困难,小可不才,试着用自己的方式来阐释一下这个问题,看看是否有帮助 ...
- http_load压力测试使用
介绍:http_load以并行复用的方式运行,用以测试web服务器的吞吐量与负载.但是它不同于大多数压力测试工具,它可以以一个单一的进程运行,一般不会把客户机搞死.还可以测试HTTPS类的网站请求. ...
- Solidworks如何添加齿轮 运动副
建立下面的齿轮装配关系(注意装配体不要先拖入齿轮,因为我们第一个齿轮是要手动让他转的,所以不能固定) 分别在两个齿轮中绘制两条直线,一个从圆心到齿顶圆,一个从圆心到齿根圆(在零件中绘图完成之后要退 ...
- 如何使用Apache设置404页面
方法一:[.htaccess文件配置404] 网上大部分解决办法是:首先你要开启Apache的rewrite_module模块,支持.htaccess,然后在网站根目录建立.htaccess文件(或已 ...