Codeforces Round #318-(B. Bear and Three Musketeers)
来源:互联网 发布:快感增强液 知乎 编辑:程序博客网 时间:2024/06/04 17:44
这道题做的时候还剩下30多分钟了,于是有点慌。。。果然心理素质还是有待提高,这么水的题都没有过):导致又掉rating了。。。
题意:
现在给你n个人,m个询问,然后接下来m行告诉你a与b是相互认识的,但是这个认识不能够相互传递,也就是说a认识b,b认识c,但是a并不认识c。
然后问你是否能够在这n个人中选取3个人,使得它们认识其他人的总数最少,并且这三个人要相互认识(也就是a-b,b-c,c-a),如果不存在则输出-1
思路:
1)暴力,枚举3个人(我只能说cf的服务器好,竟然这都能够跑过去)==
2)也是暴力,但是小处理一下,把每个人认识的都相当于用邻接表的形式存下来。
1)
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<math.h>#include<vector>using namespace std;#define inf 99999999#define maxn 4010vector<int> G[maxn];int num[maxn];int e[maxn][maxn];int main(){int n,m;scanf("%d%d",&n,&m);for(int i=1;i<=m;i++){int a,b;scanf("%d%d",&a,&b);e[a][b]=e[b][a]=1;num[a]++;num[b]++;G[a].push_back(b);G[b].push_back(a);}int lmin=inf;for(int i=1;i<=n;i++){int nn=0,a,b,c;for(int j=0;j<G[i].size();j++){b=G[i][j];for(int k=0;k<G[b].size();k++){//一开始这里写成了G[j].size(),结果一直错!! a=i; b=G[i][j]; c=G[b][k];if(e[a][b]&&e[b][c]&&e[c][a]){nn=num[a]+num[b]+num[c];lmin=min(lmin,nn-6);}}}}if(lmin==inf) printf("-1\n");else printf("%d\n",lmin);}
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<math.h>#include<vector>using namespace std;#define inf 99999999#define maxn 4010vector<int> G[maxn];int vis[maxn][maxn];int a[maxn],b[maxn];int main(){int n,m;scanf("%d%d",&n,&m);for(int i=1;i<=m;i++){scanf("%d%d",&a[i],&b[i]);G[a[i]].push_back(b[i]);G[b[i]].push_back(a[i]);vis[a[i]][b[i]]=vis[b[i]][a[i]]=1;}int lmax=inf;for(int i=1;i<=m;i++){int u=a[i],v=b[i];if(G[u].size()>=2&&G[v].size()>=2){for(int j=0;j<G[v].size();j++){int w=G[v][j];if(vis[u][v]&&vis[v][w]&&vis[w][u]){int ans=G[u].size()+G[v].size()+G[w].size()-6;lmax=min(lmax,ans);}}}}if(lmax==inf) printf("-1\n");else printf("%d\n",lmax);}
0 0
- Codeforces Round #318 574B Bear and Three Musketeers(模拟)
- Codeforces Round #318 B - Bear and Three Musketeers
- Codeforces Round #318-(B. Bear and Three Musketeers)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B. Bear and Three Musketeers(STL_暴力)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)B 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 (Div. 2) B. Bear and Three Musketeers 位运算压缩
- Bear and Three Musketeers CodeForces
- B. Bear and Three Musketeers
- Codeforces 574B Bear and Three Musketeers【思维】
- Bear and Three Musketeers
- Bear and Three Musketeers
- 模拟赛#1补题 CodeForces 574B Bear and Three Musketeers(模拟判环)
- CF#318-B - Bear and Three Musketeers-暴力寻找三元环
- CF#318 (Div. 2)B. Bear and Three Musketeers 暴力 复杂度分析
- 573B. Bear and Blocks(Codeforces Round #318)
- codeBlocks创建新的c语言项目
- 明天上课了!!!
- 对NSDictionary的遍历
- 关于敏捷开发的26个心得
- 【提高学习效率】☞(利用WIN+R 快速打开 软件 及 网页 )
- Codeforces Round #318-(B. Bear and Three Musketeers)
- 深入理解Android消息处理系统——Message、Looper、Handler
- remove ele when iterative hashmap
- 给一千万个数据量的文件排序
- poj1125floyd
- C#125个基本的面试问答清单
- 二分查找的两种写法
- 树莓派安装中文字库和输入方法
- 源泉书签今日更新:在用户注册后,会显示首页的超链接,操作更加人性化