Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)B Bear and Three Musketeers

思路:直接n^3显然会超时的,那么我们做一个小小的优化就是先判断两个人是否互相认识,如果这两个人互相认识才进入到下一层循环中,这样能大大减少复杂度


#include<bits\stdc++.h>
using namespace std;
int n,m;
int e[4005][4005];
int degree[4005];

int main()
{
    cin >> n >> m;
	for (int i = 0;i<m;i++)
	{
		int u,v;
		cin >> u >> v;
		e[u][v]=1;
		e[v][u]=1;
		degree[u]++;
		degree[v]++;
	}
	int ans = 1e9;
    for(int i = 1;i<=n;i++)
		for(int j = i+1;j<=n;j++)
			if(e[i][j])
			{
			   for(int k = j+1;k<=n;k++)
				  if(e[i][j]&&e[j][k]&&e[i][k])
					  ans = min(ans,degree[i]+degree[j]+degree[k]-6);
			}
	if(ans==1e9)
		cout << "-1"<<endl;
	else
		cout << ans << endl;

}


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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值