一种巧妙到暴力方式,这题到抽象化:在一个无向图中,找一个度数和最小到三阶到圈。
首先对边进行枚举,这样确定了两个顶点,然后在对点进行枚举,找到一个可以构成三元圈到点,则计算他们到度数和。
最后保存最小到度数和到三元圈即可。
 
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. jquery dialog close icon missing 关闭图片丢失,样式丢失问题

    http://stackoverflow.com/questions/17367736/jquery-ui-dialog-missing-close-icon

  2. iOS:quartz2D绘图(在PDF文件上绘制图片)

    quartz2D还可以在PDF文件上绘制图片,它有自己的PDF Graphics Context上下文,通过UIGraphicsBeginPDFContextToFile方法开始上下文后就可以绘制图片 ...

  3. XML-RPC 实现C++和C#交互

    我们通常会面对这样的问题:整合不同平台或不同类库,这些类库可能来自不同的语言,甚至不同的操作系统. 如何解决这类棘手的问题呢? 一.方案介绍 解决不同语言交互的方法有不少,对我了解的windows系统 ...

  4. Hive计算的临时文件清理

    hive 的存储路径的 .hive-staging_hive_yyyy-MM-dd_HH-mm-ss_SSS_xxxx-x 文件可以清理掉吗 https://blog.csdn.net/sparkex ...

  5. How to make an executable jar file?

    https://stackoverflow.com/questions/5258159/how-to-make-an-executable-jar-file 文件的路径如下: package com. ...

  6. DataGridView绑定泛型List时,利用BindingList来实现增删查改

    DataGridView绑定泛型List时,利用BindingList来实现增删查改  一.   DataGridView绑定泛型List的种种 1.DataGridView数据绑定对比(DataTa ...

  7. Python描写叙述符(descriptor)解密

    Python中包括了很多内建的语言特性,它们使得代码简洁且易于理解.这些特性包括列表/集合/字典推导式,属性(property).以及装饰器(decorator).对于大部分特性来说,这些" ...

  8. asp.net页面生命周期之页面的终结阶段

    处理过回发事件之后,页面便为浏览器生成输出做好了准备.呈现阶段分为两部分:预呈现和标记生成.预呈现这个子阶段有两个事件表征,分别为:预处理和投递处理. PreRender事件 通过处理该事件,页面和控 ...

  9. OpenJudge.poj CR2(Enclosure-ceil向上取整)

    0:Enclosure 查看 提交 统计 提问 总时间限制:  1000ms  内存限制:  131072kB 描述 为了防止爆零而加入了一道热身题.大家轻虐- Picks在参加NOI(网上同步赛)时 ...

  10. python的__init__和__new__

    本文所有实例代码在python3.7下 一.__new__和__init__区别 1.__new__先于__init__执行;__new__是相当于其他OOP语言的构造方法,负责创建实例:之后,__i ...