Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B. Bear and Three Musketeers 枚举
2015-08-30 10:40
483 查看
[b] B. Bear and Three Musketeers[/b]
time limit per test
2 seconds
memory limit per test
256 megabytes
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).
Sample test(s)
input
output
input
output
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.
题意:找一个三元环 这个环最少的分支数是多少
题解:枚举顶点就好了
代码
time limit per test
2 seconds
memory limit per test
256 megabytes
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).
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 <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <queue> #include <typeinfo> #include <map> #include <stack> typedef __int64 ll; #define inf 0x7fffffff using namespace std; inline ll read() { ll x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } //************************************************************************************** int n,m; int a,b; int mp[4001][4001]; vector<int >G[4001]; int main() { scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) { scanf("%d%d",&a,&b); G[a].push_back(b); G[b].push_back(a); mp[a][b]=1;mp[b][a]=1; } int ans=inf; for(int i=1;i<=n;i++) { for(int j=0;j<G[i].size();j++) { for(int k=0;k<G[i].size();k++) { if(j==k)continue; if(mp[G[i][j]][G[i][k]]) {int sum=G[i].size()+G[G[i][j]].size()+G[G[i][k]].size(); ans=min(ans,sum-6); } } } }if(ans==inf)cout<<-1<<endl;else cout<<ans<<endl; return 0; }
代码
相关文章推荐
- Android(java)学习笔记202:Handler消息机制的原理和实现
- linux命令汇总(mkdir、rmdir、touch、dirname、basename)
- 使用createrepo自建yum源
- 以此勉励
- 浏览器缓存内幕与getLastModified方法
- 现在走捷径,将来付利息
- Struts+Spring+Hibernate处理Lob(Blob,Clob)-->sessionFactory中注入 org.springframework.jdbc.support.lob.Def
- 数据库插入微信表情错误
- MarkdownPad this view has crashed
- OSPF区域MD5认证
- 8-28 正则表达命令详解及练习
- 用ul、li做横向导航
- LinkCode-最多有多少个点在一条直线上
- C语言的getc()函数和gets()函数的使用对比
- 零基础学习Sencha Touch(资料和教程集合)
- 服务器向客户端发送数据自动中断
- Codeforces Round #318 574B Bear and Three Musketeers(模拟)
- 开发者应当敬畏和尊重技术
- 一起talk C栗子吧(第四十四回:C语言实例--深度优先遍历一)
- 验证身份证是否合法算法