Codeforces 574B Bear and Three Musketeers【思维】

来源:互联网 发布:js设置style的left 编辑:程序博客网 时间:2024/06/13 02:19

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 hisrecognition 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 andm (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). Warriorsai andbi 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).

Examples
Input
5 61 21 32 32 43 44 5
Output
2
Input
7 42 13 65 11 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 is0. The second musketeer has recognition 1 because he knows warrior number 4. The third musketeer also has recognition1 because he knows warrior 4. Sum of recognitions is0 + 1 + 1 = 2.

The other possible triple is 2, 3, 4 but it has greater sum of recognitions, equal to1 + 1 + 1 = 3.

In the second sample there is no triple of warriors knowing each other.


题目大意:

给你N个点,M条边(关系),关系不传递。

让你找到三个人组成一队,需要保证这三个人互相认识,并且除了这三个人以外的人认识他们三个最少。

问除了这三个人以外认识他们的关系数。


思路:


1、首先维护每个点的度,然后考虑,O(n^3)枚举出来三个人,维护degree【i】+degree【j】+degree【k】-6的最小值是一定超时的。

那么我们考虑如何优化这个问题。


2、肯定是要找到三个人出来的,那么如何搞出来三个人最快呢?

答案肯定是要枚举边的,因为枚举出来一条边就枚举出来了两个人,那么我们再O(n)枚举第三人判断并且维护最小值即可。

时间复杂度O(nm);


Ac代码:

#include<stdio.h>#include<iostream>#include<string.h>using namespace std;int degree[4005];int a[4005][4005];int u[4005];int v[4005];int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        memset(degree,0,sizeof(degree));        memset(a,0,sizeof(a));        for(int i=0;i<m;i++)        {            scanf("%d%d",&u[i],&v[i]);            degree[u[i]]++;            degree[v[i]]++;            a[u[i]][v[i]]=a[v[i]][u[i]]=1;        }        int output=0x3f3f3f3f;        for(int i=0;i<m;i++)        {            int x=u[i];            int y=v[i];            for(int j=1;j<=n;j++)            {                if(j!=x&&j!=y)                {                    if(a[x][j]==1&&a[y][j]==1)                    {                        output=min(degree[x]+degree[y]+degree[j]-6,output);                    }                }            }        }        if(output==0x3f3f3f3f)printf("-1\n");        else         printf("%d\n",output);    }}





0 0
原创粉丝点击