CF #318 B. 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.
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.

1 #include <stdio.h> 2 #include <string.h> 3 #include <vector> 4 #include <algorithm> 5 using namespace std; 6 const int inf=0x3f3f3f3f; 7 vector <int> edg[4005]; 8 int vis[4005]; 9 int x,y,num=inf; 10 int n,m,sum; 11 void check(int v,int ud) 12 { 13 int i; 14 for(i=0;i<edg[v].size();i++) 15 { 16 int u=edg[v][i]; 17 if(vis[u]==ud && (sum+edg[v].size()+edg[u].size()-6)<num) 18 { 19 num=sum+edg[v].size()+edg[u].size()-6; 20 } 21 } 22 } 23 int main() 24 { 25 int i,j,k; 26 scanf("%d %d",&n,&m); 27 memset(vis,0,sizeof(vis)); 28 for(i=0;i<=n;i++) 29 if(edg[i].size()) 30 edg[i].clear(); 31 for(i=1;i<=m;i++) 32 { 33 scanf("%d %d",&x,&y); 34 edg[x].push_back(y); 35 edg[y].push_back(x); 36 } 37 for(i=1;i<=n;i++) 38 { 39 sum=edg[i].size(); 40 for(j=0;j<edg[i].size();j++) 41 { 42 int v=edg[i][j]; 43 vis[v]=i; 44 check(v,i); 45 } 46 } 47 if(num!=inf) 48 printf("%d\n",num); 49 else 50 printf("-1\n"); 51 return 0; 52 }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 历时 8 年,我冲上开源榜前 8 了!
· 物流快递公司核心技术能力-海量大数据处理技术
· 四大AI编程工具组合测评
· 关于能否用DeepSeek做危险的事情,DeepSeek本身给出了答案
· 几个技巧,教你去除文章的 AI 味!