B. Bear and Three Musketeers
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.
There is 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 was 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 following m lines contains II space-separated integers ai an D bi (1≤ ai, bi ≤ n , ai ≠ bi). Warriors Ai and bi know each 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 6
1 2
1 3
2 3
2 4
3 4
4 5
Output
2
Input
7 4
2 1
6 S
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, musketeers so he 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 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.
The main idea is to find a ring composed of three points and three points of the degree plus "minimum", where the "minimum" refers to the three vertices of the ring formed by the degree of the formation of the degrees do not count, that is, three vertices into the ring of the minimum degree-6 is the required answer, if there is no such ring output-1.
Look at the vertex number is only 4000, the edge is only 4000 with the adjacency matrix, and then use an array to record the degrees of each vertex, and then directly enumerate each vertex to take the smallest total number of degrees. The code is as follows
#include <cstdio>#include<cstring>#include<iostream>#include<queue>#include<vector>#include<stack>using namespacestd;Const intM =10005;Const intMAXN = the; typedefLong Longll;Const intINF =9999999; Vector<int>G[maxn];queue<int>Q;stack<int>St;intA[MAXN][MAXN];intVIS[MAXN];intMain () {intn,m; scanf ("%d%d",&n,&m); intu,v; memset (Vis,0,sizeof(VIS)); Memset (A,0,sizeof(a)); for(intI=1; i<=m;i++) {scanf ("%d%d",&u,&v); A[U][V]= A[v][u] =1; Vis[u]++; VIS[V]++; } intAns =inf; for(intI=1; i<=n;i++){ for(intj=i+1; j<=n;j++){ if(A[i][j]) { for(intk=j+1; k<=n;k++){ if(a[i][k]&&A[k][j]) {ans= Min (ans,vis[i]+vis[j]+Vis[k]); } } } } } if(ans = = inf) printf ("-1\n"); Elseprintf"%d\n", ans-6); return 0;}
View Code
CF Div2 318 B