Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B 暴力
来源:互联网 发布:厨具收纳 知乎 编辑:程序博客网 时间:2024/05/01 00:28
链接:戳这里
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)-1using 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;}
0 0
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B 暴力
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B. Bear and Three Musketeers(STL_暴力)
- 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 模拟
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) C 模拟
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) E DFS
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)(574A,574B)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)B Bear and Three Musketeers
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) B. Bear and Blocks dp
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1)(573A,573B)
- 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
- java中各种存储的场景
- 用Jsp设置登录界面和接收界面
- Fragment详解之五——Fragment间参数传递
- ogr2gor sqlserver转geojson
- 网站优化有价值的内容来自于你用心的积累和分析
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B 暴力
- 实体店关闭潮下,上海K11如何玩转邮件营销?
- 链表使用总结
- Spring Security 自定义登录验证与自定义回调地址
- matlab 三个向量数据绘制曲面图
- 广度优先遍历(Breadth-FirstTraversal)
- java-类型转换小结
- Android - 文件读写操作 总结
- **JAVASCRIPT** 浏览器对象二