您的位置:首页 > 其它

模拟赛#1补题 CodeForces 574B Bear and Three Musketeers(模拟判环)

2015-10-14 10:43 351 查看
【题目链接】:click here~~

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 ≤ 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 test(s)

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


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 1 because
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.

【题目大意】:在一个无向图中求一个三元环与外界最小连接边的数量

【思路】数据正好,我们考虑枚举一条边,那么这条边的两端就确定了,再枚举一个点,那么判断该点是否和边的两个端点相连,如果是的情况下,我们统计每个点的入度,每次取最小值,因为是三元环,每个点的内度,也就是每个点必须和其他两个点相连,是2*3=6,最后用总度数减6就是最后答案。

代码:

/*
* Problem: CodeForces 574B
* Running time: 124MS
* Complier: G++
* Author: herongwei
* Create Time: 20:30 2015/10/13星期二
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=4500;
const int inf=0x3f3f3f3f;
bool mat[maxn][maxn]={0};
int u[maxn],v[maxn];///边的两端
int deg[maxn]={0};
inline int min(int a,int b){return a<b?a:b;}
int main()
{
//freopen("1.txt","r",stdin);
int n,m;cin>>n>>m;
for(int i=1; i<=m; ++i)
{
cin>>u[i]>>v[i];
deg[u[i]]++;deg[v[i]]++;
mat[u[i]][v[i]]=mat[v[i]][u[i]]=1;
}
int ans=inf;
for(int i=1; i<=m; ++i)
{
for(int j=1; j<=n; ++j)
{
if(mat[u[i]][j]&&mat[j][v[i]])
{
ans=min(ans,deg[u[i]]+deg[v[i]]+deg[j]);
// cout<<"ans= "<<ans<<endl;
}
}
}
if(ans!=inf) cout<<ans-6<<endl;
else puts("-1");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: