Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B. Bear and Three Musketeers(STL_暴力)
来源:互联网 发布:中控考勤机软件 编辑:程序博客网 时间:2024/05/16 05:16
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.
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.
If Richelimakieu can choose three musketeers, print the minimum possible sum of their recognitions. Otherwise, print "-1" (without the quotes).
5 61 21 32 32 43 44 5
2
7 42 13 65 11 7
-1
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;}
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B. Bear and Three Musketeers(STL_暴力)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)B Bear and Three Musketeers
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B 暴力
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) B. Bear and Blocks dp
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) A. Bear and Elections(优先队列)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)C. Bear and Poker(gcd模拟)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)C Bear and Poker
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)D Bear and Blocks
- Codeforces Round #318 (Div. 2) B. Bear and Three Musketeers 位运算压缩
- Codeforces Round #318 574B Bear and Three Musketeers(模拟)
- Codeforces Round #318 B - Bear and Three Musketeers
- Codeforces Round #318-(B. Bear and Three Musketeers)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) A. Bear and Poker gcd
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1)A,B
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)A
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) A 模拟
- shell生成随机文件名
- 关于测试
- 【C++】双向链表
- gdb使用及原理【转】
- 由点击页面其它地方隐藏div所想到的jQuery的delegate
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B. Bear and Three Musketeers(STL_暴力)
- Android 蓝牙API
- poj 2377 Bad Cowtractors
- org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [si
- [leetcode-127]Word Ladder(java)
- IOS CoreText.framework --- 基本用法
- solr Repeater(中转器)
- php 系统的代码组织
- SurfaceView+MediaPlayer 播放视频 锁屏 没有画面等各种问题的解决方案。