A10035 | Bear and Three Musketeers
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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 $a_{i}$ and $b_{i}$ ( $1<=a_{i},b_{i}<=n$ , $a_{i}≠b_{i}$ ). Warriors $a_{i}$ and $b_{i}$ know each other. Each pair of warriors will be listed at most once.
$i$ -th of the following $m$ lines contains two space-separated integers $a_{i}$ and $b_{i}$ ( $1<=a_{i},b_{i}<=n$ , $a_{i}≠b_{i}$ ). Warriors $a_{i}$ and $b_{i}$ 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).
输入输出样例
输入 #1
5 6 1 2 1 3 2 3 2 4 3 4 4 5
输出 #1
2
输入 #2
7 4 2 1 3 6 5 1 1 7
输出 #2
-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.
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted