Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B 暴力

CrossDolphin發表於2016-07-13



連結:戳這裡


B. Bear and Three Musketeers
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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).

Examples
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.


題意:

給出n個點m條邊(N<=4000,m<=4000) 問是否可以選擇三個成環的點,使得與環相連的邊最少


思路:

兩層for列舉兩條邊,也就是四個點,如果其中有兩個點是重複的並且剩下的兩個不重複的點是有邊連線的

表示這三個點構成一個環,統計最小值就可以了


程式碼:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int a[4010][4010];
int n,m;
int u[4010],v[4010],sz[4010];
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d",&u[i],&v[i]);
        a[u[i]][v[i]]=a[v[i]][u[i]]=1;
        sz[u[i]]++;
        sz[v[i]]++;
    }
    int ans=1e9;
    for(int i=1;i<=m;i++){
        int x=u[i],y=v[i];
        for(int j=i+1;j<=m;j++){
            int c=u[j],d=v[j];
            if(x==c && a[y][d]==1){
                ans=min(ans,sz[x]+sz[y]+sz[d]-6);
            } else if(x==d && a[y][c]==1){
                ans=min(ans,sz[x]+sz[c]+sz[y]-6);
            } else if(y==c && a[x][d]==1){
                ans=min(ans,sz[y]+sz[x]+sz[d]-6);
            } else if(y==d && a[x][c]==1){
                ans=min(ans,sz[y]+sz[x]+sz[c]-6);
            }
        }
    }
    if(ans==1e9) cout<<-1<<endl;
    else cout<<ans<<endl;
    return 0;
}



相關文章