Codeforces Round #318 [Russiancodecup Thanks-round] (Div. 2) B. Bear and Three Musketeers (stl_ violence)

Source: Internet
Author: User

Do you know a stories about the Three musketeers? Anyway, you'll learn about it 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 the fight against bad guys.

The Re Are  n  warriors. Richelimakieu wants to choose three of them to become musketeers but it's not so easy. The most important condition are the Musketeers must know each and the cooperate efficiently. And they shouldn ' t is too well known because they could is betrayed by old friends. For each musketeer his recognition  is The number of warriors he knows, excluding other and musketeers.

Help richelimakieu! Find if it is possible to choose three Musketeers knowing all other, and what is minimum possible sum of their recognitio Ns.

Input

The first line contains the 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 followingmLines contains, space-separated integers ai and bi (1?≤? a i,? b i? ≤? N , ai? ≠? b I ). Warriors ai and bi Know each of the other. Each pair of warriors is listed at the 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 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 triple1,2,3. The first musketeer doesn ' t know anyone except other, musketeers so he recognition is0. The second musketeer has recognition1Because he knows warrior number4. The third musketeer also has recognition1Because he knows warrior4. Sum of Recognitions is0?+?1?+?1?=?2.

The other possible triple was 2,?3,?4 but it had greater sum of recognitions, equal to 1?+?1?+?1?=?3.

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

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < vector>using namespace Std;vector<int>ve[4444];const int Maxn=4000+10;int m[maxn][maxn];int num[maxn];int Main () {memset (num,0,sizeof (num)); memset (M,0,sizeof (m)); int N,q,u,v,t,ok;int I,j,k;cin>>n>>q;while ( q--) {cin>>u>>v;num[u]++;num[v]++;m[u][v]=m[v][u]=1;ve[u].push_back (v); Ve[v].push_back (u);} int Mmin=maxn;for (i=1;i<=n;i++) {for (J=0;j<ve[i].size (); j + +) {for (K=j+1;k<ve[i].size (); k++) {U=ve[i][j];v =ve[i][k];if (M[u][v]) {t=num[i]+num[u]+num[v];mmin=min (mmin,t);}}} if (MMIN==MAXN) {printf (" -1\n"); return 0;} Mmin=mmin-6;cout<<mmin<<endl;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces Round #318 [Russiancodecup Thanks-round] (Div. 2) B. Bear and Three Musketeers (stl_ violence)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.