Codeforces Round #318 574B Bear and Three Musketeers(模拟)
2015-08-30 10:38
330 查看
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.
给出每个点相邻的点,不能构成三角形则输出-1,能则输出最小的相邻点数。
AC代码:
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.
给出每个点相邻的点,不能构成三角形则输出-1,能则输出最小的相邻点数。
AC代码:
#include "iostream" #include "cstdio" #include "cstring" #include "algorithm" using namespace std; const int MAXN = 4010; const int INF = 0x3f3f3f3f; int n, m, a[MAXN]; bool vis[MAXN][MAXN]; int main(int argc, char const *argv[]) { scanf("%d%d", &n, &m); for(int i = 0; i < m; ++i) { int x, y; scanf("%d%d", &x, &y); a[x]++; a[y]++; vis[x][y] = vis[y][x] = true; } int ans = INF; for(int i = 1; i <= n; ++i) for(int j = i + 1; j <= n; ++j) if(vis[i][j]) for(int k = j + 1; k <= n; ++k) if(vis[k][j] && vis[k][i]) ans = min(ans, a[i] + a[j] + a[k] - 6); if(ans == INF) printf("-1\n"); else printf("%d\n", ans); return 0; }
相关文章推荐
- 开发者应当敬畏和尊重技术
- 一起talk C栗子吧(第四十四回:C语言实例--深度优先遍历一)
- 验证身份证是否合法算法
- context:component-scan>使用说明
- C++中指针和引用的区别
- iOS开源大全
- Java记录 -16- 抽象类及接口
- Scala深入浅出实战初级入门经典视频课程
- SequoiaDB入门教程
- CodeForces 443A Anton and Letters
- 【财富人生】启蒙之旅01
- 程序员必读书单 1.0
- gangs
- Bestcoder #53
- 基于Hadoop的大数据分析实战-Hadoop部署与实践视频课程
- 优化Eclipse启动和运行速度
- 树的非递归遍历:一种很好的算法
- Android基础知识巩固:关于PendingIntent和广播
- RSA密钥的生成与配置
- maven学习1