Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B. Bear and Three Musketeers 枚举
2 seconds
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.
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.
If Richelimakieu can choose three musketeers, print the minimum possible sum of their recognitions. Otherwise, print "-1" (without the quotes).
5 6
1 2
1 3
2 3
2 4
3 4
4 5
2
7 4
2 1
3 6
5 1
1 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 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=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//**************************************************************************************
int n,m;
int a,b;
int mp[][];
vector<int >G[];
int main()
{ scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
mp[a][b]=;mp[b][a]=;
}
int ans=inf;
for(int i=;i<=n;i++)
{
for(int j=;j<G[i].size();j++)
{
for(int k=;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-);
}
}
}
}if(ans==inf)cout<<-<<endl;else
cout<<ans<<endl;
return ;
}
代码
Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B. Bear and Three Musketeers 枚举的更多相关文章
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) B. Bear and Blocks 水题
B. Bear and Blocks Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/573/pr ...
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) A. Bear and Poker 分解
A. Bear and Poker Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/573/pro ...
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) C. Bear and Drawing
题目链接:http://codeforces.com/contest/573/problem/C题目大意:在两行无限长的点列上面画n个点以及n-1条边使得构成一棵树,并且要求边都在同一平面上且除了节点 ...
- 校内选拔I题题解 构造题 Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) ——D
http://codeforces.com/contest/574/problem/D Bear and Blocks time limit per test 1 second memory limi ...
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) A. Bear and Elections 优先队列
A. Bear and Elections ...
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)C. Bear and Poker
C. Bear and Poker ...
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)
以后每做完一场CF,解题报告都写在一起吧 暴力||二分 A - Bear and Elections 题意:有n个候选人,第一个候选人可以贿赂其他人拿到他们的票,问最少要贿赂多少张票第一个人才能赢 ...
- Codeforces Round #318 (Div. 2) B Bear and Three Musketeers (暴力)
算一下复杂度.发现可以直接暴.对于u枚举a和b,判断一下是否连边,更新答案. #include<bits/stdc++.h> using namespace std; int n,m; ; ...
- Codeforces Round #539Ȟȟȡ (Div. 1) 简要题解
Codeforces Round #539 (Div. 1) A. Sasha and a Bit of Relax description 给一个序列\(a_i\),求有多少长度为偶数的区间\([l ...
随机推荐
- WPF控件模拟双击事件
Action a = () => { i += ; ) { Interval = }; timer.Elapsed += (sender, e) => { timer.Enabled = ...
- 详述Linux ftp命令的使用方法
转自:http://os.51cto.com/art/201003/186325.htm ftp服务器在网上较为常见,Linux ftp命令的功能是用命令的方式来控制在本地机和远程机之间传送文件,这里 ...
- (转)也谈基于NodeJS的全栈式开发(基于NodeJS的前后端分离)
原文链接:http://ued.taobao.org/blog/2014/04/full-stack-development-with-nodejs/ 随着不同终端(pad/mobile/pc)的兴起 ...
- 【初级为题,大神绕道】The app icon set named "AppIcon" did not have any applicable content 错误#解决方案#
The app icon set named "AppIcon" did not have any applicable content 错误,怎样解决 按照您的错误提示您应该 ...
- 《Hadoop基础教程》之初识Hadoop
Hadoop一直是我想学习的技术,正巧最近项目组要做电子商城,我就开始研究Hadoop,虽然最后鉴定Hadoop不适用我们的项目,但是我会继续研究下去,技多不压身. <Hadoop基础教程> ...
- Windows下tcp参数优化
Windows系统下的TCP参数优化2013-04-25 0 个评论 作者:最初的幸福ever收藏 我要投稿Windows系统下的TCP参数优化 TCP连接的状态与关闭方 ...
- 第16章 使用Squid部署代理缓存服务
章节概述: 本章节从代理缓存服务的工作原理开始讲起,让读者能够清晰理解正向代理(普通模式.透明模式)与反向代理的作用. 正确的使用Squid服务程序部署代理缓存服务可以有效提升访问静态资源的效率,降低 ...
- 安装UnityVS 2012步骤
英文原文是: Cracked by Twisted89//////////////////////////////////////////////////// INSTALL INSTRUCTIONS ...
- ubuntu打不开图形界面,显示run in low_graphic mode
我上次因为这个问题重装了ubuntu,结果没两天又有问题了,这次我看到了未重启前的提示,说我的硬盘空间剩0kb,所以我心有余悸的想办法留空间,十分担心会有上次的问题出现,为了验证我的想法,我重启了一下 ...
- [OpenJudge 3063]罪犯问题
[OpenJudge 3063]罪犯问题 试题描述 一天,警官抓获了N个嫌犯,审问N个罪犯的途中,作为警长助手的你突然发现其中被确定为罪犯的K号人是你曾经出生入死的兄弟,你不能眼睁睁看着他被抓进牢里. ...