Codeforces 574B Bear and Three Musketeers【思维】
来源:互联网 发布:js设置style的left 编辑:程序博客网 时间:2024/06/13 02:19
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 hisrecognition 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 andm (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). Warriorsai andbi 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 is0. The second musketeer has recognition 1 because he knows warrior number 4. The third musketeer also has recognition1 because he knows warrior 4. Sum of recognitions is0 + 1 + 1 = 2.
The other possible triple is 2, 3, 4 but it has greater sum of recognitions, equal to1 + 1 + 1 = 3.
In the second sample there is no triple of warriors knowing each other.
题目大意:
给你N个点,M条边(关系),关系不传递。
让你找到三个人组成一队,需要保证这三个人互相认识,并且除了这三个人以外的人认识他们三个最少。
问除了这三个人以外认识他们的关系数。
思路:
1、首先维护每个点的度,然后考虑,O(n^3)枚举出来三个人,维护degree【i】+degree【j】+degree【k】-6的最小值是一定超时的。
那么我们考虑如何优化这个问题。
2、肯定是要找到三个人出来的,那么如何搞出来三个人最快呢?
答案肯定是要枚举边的,因为枚举出来一条边就枚举出来了两个人,那么我们再O(n)枚举第三人判断并且维护最小值即可。
时间复杂度O(nm);
Ac代码:
#include<stdio.h>#include<iostream>#include<string.h>using namespace std;int degree[4005];int a[4005][4005];int u[4005];int v[4005];int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { memset(degree,0,sizeof(degree)); memset(a,0,sizeof(a)); for(int i=0;i<m;i++) { scanf("%d%d",&u[i],&v[i]); degree[u[i]]++; degree[v[i]]++; a[u[i]][v[i]]=a[v[i]][u[i]]=1; } int output=0x3f3f3f3f; for(int i=0;i<m;i++) { int x=u[i]; int y=v[i]; for(int j=1;j<=n;j++) { if(j!=x&&j!=y) { if(a[x][j]==1&&a[y][j]==1) { output=min(degree[x]+degree[y]+degree[j]-6,output); } } } } if(output==0x3f3f3f3f)printf("-1\n"); else printf("%d\n",output); }}
- Codeforces 574B Bear and Three Musketeers【思维】
- CodeForces 574B-Bear and Three Musketeers
- Codeforces 574B Bear and Three Musketeers
- Codeforces 574 B. Bear and Three Musketeers
- Codeforces Round #318 574B Bear and Three Musketeers(模拟)
- Bear and Three Musketeers CodeForces
- B. Bear and Three Musketeers
- Codeforces Round #318 B - Bear and Three Musketeers
- Codeforces Round #318-(B. Bear and Three Musketeers)
- 模拟赛#1补题 CodeForces 574B Bear and Three Musketeers(模拟判环)
- Bear and Three Musketeers
- Bear and Three Musketeers
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B. Bear and Three Musketeers(STL_暴力)
- Codeforces Round #318 (Div. 2) B. Bear and Three Musketeers 位运算压缩
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)B Bear and Three Musketeers
- CF#318-B - Bear and Three Musketeers-暴力寻找三元环
- CF#318 (Div. 2)B. Bear and Three Musketeers 暴力 复杂度分析
- CF574B Bear and Three Musketeers 简单的图,暴力
- Bootstrap 列表组
- 每天一篇面试题(六)
- 线段树区间最大值查询,单点更新,建立模板
- Java设计模式(二):单例模式的5种实现方式,以及在多线程环境下5种创建单例模式的效率
- Spring中bean的作用域
- Codeforces 574B Bear and Three Musketeers【思维】
- lua位运算
- C语言之单向链表封装
- 单链表的创建与使用
- Spring AOP理解
- Codeforces Round #266 (Div. 2) C. Number of Ways
- 写在MirrorII开始之前
- 高性能网络编程7--tcp连接的内存使用
- 二分——Poj 3273 Monthly Expense