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

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Submit Status

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

Sample Input

Input
5 6
1 2
1 3
2 3
2 4
3 4
4 5
Output
2
Input
7 4
2 1
3 6
5 1
1 7
Output
-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的更多相关文章

  1. Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B. Bear and Three Musketeers 枚举

                                          B. Bear and Three Musketeers                                   ...

  2. Codeforces 385C Bear and Prime Numbers

    题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...

  3. Codeforces 385B Bear and Strings

    题目链接:Codeforces 385B Bear and Strings 记录下每一个bear的起始位置和终止位置,然后扫一遍记录下来的结构体数组,过程中用一个变量记录上一个扫过的位置,用来去重. ...

  4. Codeforces 680D Bear and Tower of Cubes 贪心 DFS

    链接 Codeforces 680D Bear and Tower of Cubes 题意 求一个不超过 \(m\) 的最大体积 \(X\), 每次选一个最大的 \(x\) 使得 \(x^3\) 不超 ...

  5. Codeforces 385C Bear and Prime Numbers(素数预处理)

    Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...

  6. [Codeforces 639F] Bear and Chemistry (Tarjan+虚树)(有详细注释)

    [Codeforces 639F] Bear and Chemistry(Tarjan+虚树) 题面 给出一个n个点,m条边的无向图(不保证连通,可能有自环和重边),有q次询问,每次询问给出p个点和q ...

  7. 【CodeForces 574B】Bear and Three Musketeers

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

  8. Codeforces Round #318 (Div. 2) B Bear and Three Musketeers (暴力)

    算一下复杂度.发现可以直接暴.对于u枚举a和b,判断一下是否连边,更新答案. #include<bits/stdc++.h> using namespace std; int n,m; ; ...

  9. 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 ...

随机推荐

  1. Chrome 控制台新玩法-向输出到console的文字加样式

    Chrome 控制台新玩法-向输出到console的文字加样式 有兴趣的同学可以文章最后的代码复制贴到控制台玩玩. Go for Code 在正常模式下,一般只能向console 控制台输出简单的文字 ...

  2. [转]Loading and Running a Local Package Programmatically

    本文转自:http://msdn.microsoft.com/en-us/library/ms136090.aspx You can run Integration Services packages ...

  3. 流畅的python第十一章接口学习记录

    鸭子协议(忽略对象真正类型,转而关注对象有没有实现所需的方法,签名和语义) 标准库中的抽象基类 collections.abc模块中的抽象基类 抽象方法是抽象基类中用来强制子类必须实现的方法,如果子类 ...

  4. 细说linux IPC(四):posix 共享内存

    [版权声明:尊重原创.转载请保留出处:blog.csdn.net/shallnet 或 .../gentleliu,文章仅供学习交流,请勿用于商业用途]         上一节讲了由open函数打开一 ...

  5. [Angular] Introduction to Angular Internationalization (i18n)

    To add translation to the application: <button (click)="onEditCourse()" i18n>Edit bu ...

  6. CentOS7 Failed to start LSB: Bring up/down networking. 已解决!!!

    服务器更换了主板之前配置的静态IP发现启动网卡出现异常. 执行 service network restart 出现以下错误 Restarting network (via systemctl):  ...

  7. redis学习笔记——RDB和AOF持久化一

    为防止数据丢失,需要将 Redis 中的数据从内存中 dump 到磁盘,这就是持久化.Redis 提供两种持久化方式:RDB 和 AOF.Redis 允许两者结合,也允许两者同时关闭. RDB 可以定 ...

  8. node了解

    学习资料 廖雪峰的官方网站—node.js Node.js 中文网:http://nodejs.cn/ Node.js 教程(菜鸟教程):http://www.runoob.com/nodejs/no ...

  9. 【试水CAS-4.0.3】第01节_CAS服务端搭建及导入源代码到MyEclipse

    完整版见https://jadyer.github.io/2015/07/16/sso-cas-server-demo/ /** * @see ---------------------------- ...

  10. Python多线程问题的资料查找与汇总

    Python多线程问题的资料查找与汇总 声明: 1)本报告由博客园bitpeach撰写,版权所有,免费转载,请注明出处,并请勿作商业用途. 2)若本文档内有侵权文字或图片等内容,请联系作者bitpea ...