Codeforces 574B Bear and Three Musketeers
2015-08-30 17:13
183 查看
1.英文题意
B. Bear and Three Musketeers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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).
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.
2.中文题意
现有一无向无重边图,求其中一个三元环,要求与该环相邻的边数总和最少,若无三元环输出“-1”,否则输出“与该环相邻的边数总和”。
3.思路
CF div2 B题O(n^3)可过,我的是O(n^2),预处理求得每个顶点度数和,邻接矩阵和边集,每次检验边集中的一条边,是否有第三个点可与它构成环,更新“环上三个点度数和最小值-6”(不含3元环上度)。
4.实现
B. Bear and Three Musketeers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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).
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.
2.中文题意
现有一无向无重边图,求其中一个三元环,要求与该环相邻的边数总和最少,若无三元环输出“-1”,否则输出“与该环相邻的边数总和”。
3.思路
CF div2 B题O(n^3)可过,我的是O(n^2),预处理求得每个顶点度数和,邻接矩阵和边集,每次检验边集中的一条边,是否有第三个点可与它构成环,更新“环上三个点度数和最小值-6”(不含3元环上度)。
4.实现
/********************************* 日期:2015-08-29 作者:matrix68 题号: Codeforces 574B Bear and Three Musketeers **********************************/ #include <iostream> #include <cstring> #include <string> #include <cstdio> #define MAXN 4000 #define inf 99999 using namespace std; typedef struct { int x; int y; } Node; Node net[MAXN+10];//边集 int Du[MAXN+10];//度数和 bool matrix[MAXN+10][MAXN+10];//邻接矩阵 int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int n,m; while(~scanf("%d%d",&n,&m)) { memset(matrix,0,sizeof matrix); memset(Du,0,sizeof Du); for(int i=0; i<m; i++) { int a,b; scanf("%d %d",&a,&b); net[i].x=a; net[i].y=b; Du[a]++; Du[b]++; matrix[a][b]=matrix[b][a]=1; } int ans=inf; bool flag=false; //找环 for(int i=0; i<m; i++) { int a0=net[i].x; int a1=net[i].y; for(int j=1; j<=n; j++) { if(matrix[a1][j]) { int a2=j; if(matrix[a0][a2]) { flag=true; int tmp=Du[a0]+Du[a1]+Du[a2]-6; ans=min(ans,tmp); } } } } if(flag) cout<<ans<<endl; else cout<<-1<<endl; } return 0; }
相关文章推荐
- jmeter while controller 条件的写法
- 面向对象知识点梳理(三)
- stm32 外部中断的使用(含实例)
- linux 上用root登录不上数据库
- HDU 2732 Leapin' Lizards(拆点法+最大流)
- multiprocessing在python中的高级应用-共享数据与同步
- 一起talk C栗子吧(第四十五回:C语言实例--深度优先遍历二)
- 基本游戏框架介绍
- ExpandableListView(listView嵌套)
- Java权限中遇到的问题汇总
- 面向对象知识点梳理(二)
- 加班能体现编程的热情吗?
- Guava学习笔记:Preconditions优雅的检验参数
- EQueue - 一个纯C#写的分布式消息队列介绍2
- Python程序退出方式(sys.exit() os._exit() os.kill() os.popen(...))
- 二叉搜索树与双向链表
- jquery.mousewheel实现整屏翻屏效果
- 深入浅出CChart 每日一课——快乐高四第十二课 必也正名,Win32标准控件ChartCtrl之初识
- 实现渐渐消失的提示框
- Linux轮询目录FTP传输文件