【CodeForces - 574B】Bear and Three Musketeers (枚举边,思维,优秀暴力)

题干:

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 ≤ nai ≠ 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).

Examples

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.

题目大意:

给你n个人和m个关系,(a,b)表示a和b认识,现在这个人想雇佣其中3个人,

1: 这3个人必须相互认识

2: 这3个人的识别度的总和最小(一个人的识别度为:除了另外两人认识的人的数,三个人的识别度相加最小)

解题报告:

    显然,2000ms的时限,4000的数据量,考虑是否o(n^2)可以解决。(其实这题直接枚举三个点,o(n^3)也可以过?)怎么优化到平方级别呢?其实也很简单,就是第一层循环遍历m条边就好了。因为你三层for枚举顶点的时候,其实前两层有很多是浪费掉了的,因为有很多顶点的之间根本没有边,而你都遍历了一遍,所以我们都记录下来边了,为什么不直接遍历边呢?这样我们就有两个顶点了呀,然后再来一层循环遍历每一个点就好了。注意这种暴力的题一定别忘判断一下是否遍历到了自己,比如【CodeForces - 761C】Dasha and Password 这道题,最开始就忘了判断是否遍历到了同一个字符串。

AC代码:

#include<bits/stdc++.h>using namespace std;
const int MAX = 4000 + 5 ;
int du[MAX];
bool maze[MAX][MAX];
int head[MAX];
struct Edge {int fm,to;int w;
}e[50000 + 5];
int top;
void add(int u,int v) {e[++top].fm = u;e[top].to = v;
}
int main()
{int n,m,u,v;cin>>n>>m;for(int i = 1; i<=m; i++) {scanf("%d%d",&u,&v);add(u,v);add(v,u);du[u]++;du[v]++;maze[u][v] = maze[v][u] = 1;}int ans = 0x3f3f3f3f;for(int i = 1; i<=top; i++) {for(int j = 1; j<=n; j++) {if(j == e[i].fm || j == e[i].to) continue;if(maze[e[i].fm][j] && maze[e[i].to][j]) {ans = min(ans,du[e[i].to] + du[e[i].fm] + du[j]);}}}if(ans == 0x3f3f3f3f) puts("-1");else printf("%d\n",ans-6);return 0 ;} 

再来一个代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const int maxn = 4050;int n,m,d[maxn];
bool g[maxn][maxn];
//一开始以为暴力的话是n^3会超时,后来看了题解原来前两层放上去之后加个剪枝,第三层的复杂度是加上去的而不是乘上去的 
//即剪枝使得复杂度由O(N^3)降为O(N^2+NM) 
int main(){//62 ms	18100 KBint x,y;scanf("%d%d",&n,&m);for(int i=1;i<=m;++i){scanf("%d%d",&x,&y);g[x][y]=g[y][x]=1;d[x]++;d[y]++; }int ans=inf;for(int i=1;i<=n;++i){for(int j=1;j<=n;++j){if(g[i][j] && d[i]+d[j]<=inf){//只有M个g[i][j]成立  for(int k=1;k<=n;++k){if(g[i][k] && g[j][k]){ans=min(ans,d[i]+d[j]+d[k]);}}}}}//相当于外面枚举N^2次,对于其中的M次才有第三层for 故复杂度为 O(N^2+M*N) if(ans>=inf) puts("-1");else printf("%d\n",ans-6);return 0;
}

 

 

 

AC代码2:(其实也不是拓扑排序啦,就是用vector表示了二维数组,并且,由第二个点找第三个点的时候直接在第一个点所连的边中找就可以了。但是那个c函数其实也有点消耗时间?如果直接用个bool类型的maze二维数组来表示无向图两点之间是否相连就更快了)

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441630.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux下apache配置文件测试,重启 Apache 服务器及测试配置

在 CentOS / RHEL / Fedora Linux 服务器上安装的 Apache 网页服务器程序在系统中的进程名称为 httpd。每次更改 Apache 服务器的配置&#xff0c;不管是 httpd.conf 还是 vhost.conf 或者自己配置的什么 .conf&#xff0c;只要有改动都需要重新加载配置或者重启 httpd 服务才能…

【CodeForces - 574C】Bear and Poker(思维,剪枝,数学)

题干&#xff1a; Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars. Ea…

linux变量赋值取值,linuxshell编程对变量的赋值

linux shell编程对变量的赋值shell对变量的赋值&#xff0c;所有的变量都是由字符串组成&#xff0c;不需要对变量名预先声明&#xff0c;而且有很多关键字供编程者使用。下面列举例子详细说明。 在终端下建立文件 #vi s2.sh#!/bin/sh#set a variable aa"hello world"…

*【CodeForces - 574A】Bear and Elections (优先队列,水题模拟)

题干&#xff1a; Limak is a grizzly bear who desires power and adoration. He wants to win in upcoming elections and rule over the Bearland. There are n candidates, including Limak. We know how many citizens are going to vote for each candidate. Now i-th …

stm32linux区别,STM32MPU和OpenSTLinux你了解多少?

早在2019年年初的时候&#xff0c;ST就发布了首款STM32MPU&#xff1a;STM32MP1。 STM32MP1通用微处理器产品系列&#xff0c;系基于混合的 双Arm Cortex-A7核 和 Cortex-M4核架构产品。 一、支持STM32MPU 生态系统熟悉 Cortex-M4 MCU 环境的开发人员能轻松实现他们的目标&…

算法总结 -- 博弈论(PN图)

博弈论&#xff1a;组合博弈 * 必败点(P点) :前一个选手(Previous player)将取胜的位置称为必败点。 * 必胜点(N点) :下一个选手(Next player)将取胜的位置称为必胜点。 * 必败&#xff08;必胜&#xff09;点的属性&#xff1a;* (1) 所有终结点是必败点&#xff08;P点&#…

linux安全模式改文件,嵌入式Linux的安全模式设计 - 嵌入式操作系统 - 电子发烧友网...

本系统的架构如下图&#xff1a;产品所使用的flash总大小为16M。系统包括三大部分&#xff0c;即Bootloader&#xff0c;config, kernel rootfs&#xff1a;另外&#xff0c;/dev/mtdblock/0&#xff0c;在系统中对应整个flash block&#xff0c;即整个16M空间。系统启动时&am…

【POJ - 1661】Help Jimmy(记忆化搜索,dp)

题干&#xff1a; Help Jimmy" 是在下图所示的场景上完成的游戏。 场景中包括多个长度和高度各不相同的平台。地面是最低的平台&#xff0c;高度为零&#xff0c;长度无限。 Jimmy老鼠在时刻0从高于所有平台的某处开始下落&#xff0c;它的下落速度始终为1米/秒。当Jim…

c语言linux消息机制,linux消息机制的过程是什么啊,请赐教

LINUX的安装过程很简单,按照它的提示进行就可以了.重点就是分区那里.通常我们使用双系统,那样我们在LINUX分区的时候不要把WIN分区跟格掉就好,然后要记得分一个SWAP分区,然后根据您的需要进行相应的分区,比如/HOME,/USR,/VAR等等.下面介绍一下具体操作步骤&#xff1a;假定用户…

ACM与Java -- 大整数类的常用函数一览表

BigInteger abs() 此方法返回一个BigInteger&#xff0c;其值是此BigInteger的绝对值。2BigInteger add(BigInteger val) 此方法返回一个BigInteger&#xff0c;其值是(this val).3BigInteger and(BigInteger val) 此方法返回一个BigInteger&#xff0c;其值是 (this & v…

linux虚拟机上安装域名,虚拟机如何安装linux系统

虚拟机如何安装linux系统&#xff1f;安装linux系统首先要在电脑上安装好虚拟机&#xff0c;然后逐步进行linux系统安装。大致介绍下安装的流程。1.首先打开虚拟机软件,点击文件/新建虚拟机&#xff0c;用户可以按照默认选择&#xff0c;然后点击下一步。2.在浏览里&#xff0c…

想打ACM?想刷题?来这些online judge!

原文地址&#xff1a;https://blog.csdn.net/tigerisland45/article/details/52134189

基于arm下的Linux控制,基于ARMuCLinux的网络控制系统设计与实现

引言 随着网络和通信技术的发展&#xff0c;嵌入式系统现已进入高速发展阶段。并在社会各个领域得到了广泛的应用。本文介绍了一种采用ARMuCLinux作为开发平台。实现基于TCP&#xff0f;IP的远程系统监控&#xff0e;从而取代传统单片机来实现数据采集、预处理和通信功能&am…

【qduoj - 1121】小明的贪心题(Dijkstra最短路 + 最短路条数)

题干&#xff1a; 小明的贪心题 描述 小明来到青岛上学已经一年了&#xff0c;他给青岛这座城市画了一张地图。在这个地图上有n个点&#xff0c;小明的起始点为1号点&#xff0c;终点为n号点&#xff0c;并且地图上的所有边都是单向的。小明知道从i号点到j号点的时间花费为w分…

nodejs 监控linux,linuxServerMonitoring

linux服务器监控平台技术&#xff1a;nodejs vue java mongodb springboot linux shelllinux服务器监控项目&#xff0c;前后端分离vuespringbootmongodb&#xff1a;1、启动前台&#xff1a;使用命令&#xff1a;A 先安装nodejs并配置好环境变量B 先控制台cmd命令切换到项目目…

【qduoj - 纳新题】小明的dp(快速幂 + 乘法原理)(简单组合数学)

题干&#xff1a; 描述 小明有n种珠宝&#xff0c;每种无限个&#xff0c;他想从这些珠宝中取出m个&#xff0c;编成手链&#xff08;长链哦&#xff09;&#xff0c;并按顺序排列起来&#xff0c;为了好看&#xff0c;相邻的珠宝不能相同。小明想知道有多少种排列的方法。 输…

linux查看显卡核心数,linux查看硬件信息,linux查看硬盘信息,linux查看CPU信息,linux查看显卡,硬件型号信息 | 帮助信息-动天数据...

linux查看硬件信息,linux查看硬盘信息,linux查看CPU信息,linux查看显卡,硬件型号信息作者&#xff1a;dthost |时间&#xff1a;2015-09-30 |8,325 次阅读linux服务器大家都知道&#xff0c;他和WIN系统不一样&#xff0c;他不能和WIN电脑一样&#xff0c;用桌面的电脑属性查看…

【qduoj - 纳新题】凑数题(恰好装满类0-1背包 或 母函数)

题干&#xff1a; 描述 小Q手里有n枚硬币&#xff0c;每枚硬币有一定的金额x,他想知道&#xff0c;用这些硬币能组成多少种不同的金额。但是他太笨了&#xff0c;自己数懵了&#xff0c;你来帮帮他好不好&#xff1f; 注意&#xff1a;组成金额时&#xff0c;每枚硬币只能用一…

c语言中short作用,C语言short

C语言short教程C语言short定义详解语法short int varname value;short varname1 value2; //简写形式参数参数描述short int定义 short 类型变量使用的类型。varname变量名。value可选&#xff0c;变量的初始值&#xff0c;该值不可以超过 short 类型的最大值。说明使用 short…

dls的BM算法模板(线性递推问题,解决矩阵快速幂解决不了的问题)

模板&#xff1a;往里面扔数就可以了&#xff08;据说>8个&#xff1f;&#xff09;%%%dls https://www.cnblogs.com/zzqsblog/p/6877339.html #include<bits/stdc.h> using namespace std; #define rep(i,a,n) for (int ia;i<n;i) #define per(i,a,n) for (int …