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

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?≤?nai?≠?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).

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

#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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-09 22:08:36

Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B. Bear and Three Musketeers(STL_暴力)的相关文章

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

以后每做完一场CF,解题报告都写在一起吧 暴力||二分 A - Bear and Elections 题意:有n个候选人,第一个候选人可以贿赂其他人拿到他们的票,问最少要贿赂多少张票第一个人才能赢 分析:正解竟然是暴力!没敢写暴力,卡了很久,导致这场比赛差点爆零!二分的话可以优化,但对于这题来说好像不需要... 收获:以后CF div2的A题果断暴力 代码(暴力): /************************************************ * Author :Runni

Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) A. Bear and Elections(优先队列)

Limak is a grizzly bear who desires power and adoration. He wants to win in upcoming elections and rule over the Bearland. There are n candidates, including Limak. We know how many citizens are going to vote for each candidate. Now i-th candidate wou

Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)——A二分——Bear and Elections

/*贪心WA一发..二分枚举加的数赛后发现直接暴力枚举1到1000也是可以的*//************************************************ * Author :Powatr * Created Time :2015-8-30 0:58:45 * File Name :A.cpp ************************************************/ #include <cstdio> #include <algorith

Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)C. Bear and Poker(gcd模拟)

Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars. Each player can

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

/* 写搜索WA了..赛后发现直接暴力不会超啊,,, */ /************************************************ * Author :Powatr * Created Time :2015-8-30 14:55:10 * File Name :B.cpp ************************************************/ #include <cstdio> #include <algorithm> #in

Codeforces Round #318 (Div. 2) B Bear and Three Musketeers

不要想多了直接暴.对于u枚举a和b,判断一个是否连边,更新答案. #include<bits/stdc++.h> using namespace std; int n,m; const int maxn = 4001; #define PB push_back vector<int> G[maxn]; bool g[maxn][maxn]; int deg[maxn]; const int INF = 0x3f3f3f3f; int main() { //freopen("

Codeforces Round #226 (Div. 2) C. Bear and Prime Numbers(暴力)

题目链接:点击打开链接 题意:给n个数, m次询问, 每次询问时一个区间[l, r]. 求这个区间中的所有素数,能被n个数中的几个数整除的累加和. 思路:没想到什么好办法, 直接筛出素数以后直接暴力的,没想到跑的这么快.. 最费时间的大概就是那个二重循环, 但是可能因为素数比较少, 所以实际的时间复杂度并不高. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostr

Codeforces Round #422 (Div. 2) A. I&#39;m bored with life 暴力

A. I'm bored with life Holidays have finished. Thanks to the help of the hacker Leha, Noora managed to enter the university of her dreams which is located in a town Pavlopolis. It's well known that universities provide students with dormitory for the

在青岛穷游打的cf codeforces Round #318 (Div. 2) A.Bear and Elections

这场cf是暑假集训后在青岛旅游打的一场,好累..... 题意:给出一个序列,要使a[1]大于a[2]~a[n],a[1]每次可以增加一,这个一从a[2]到a[[n]里面任意一个数减一扣除,求最少的步数 思路:要不断地修改值,并从中找出最大的,可以想到优先队列,还要保存下该数的编号,要知道在队首时是a[1].还有处里一种特殊情况,详见代码 总结:这道题并不难,我用了40多分钟,主要前面太急了,一开始并没有想到是优先队列,是一些其他的想法,只要合适一个样例就下手去做,导致有很明显的bug我竟然敲完代