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.
这个题目就是说 有个人要找三个火枪手 ,这三个火枪手必须互相认识,然后他有N个备胎。。。是备选的人
然后给你m行 ,是这些人互相认识的关系,然后让你求一个组合,使得这个三人组里面的人除了认识组内成员,认识的人数之和最少。因为,认识的人太对说不行会被基友相爱相杀。。。。。。。
#include
#include
#include
#include
using namespace std;
vector < int > p[4100];
int map1[4100][4100];
int dis[4100];
int n,m;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
map1[i][j] = 0;
for(int i = 1; i <= n; i++) { p[i].clear(); dis[i] = 0; }
int x,y;
for(int i = 1; i <= m; i++ )
{
scanf("%d%d",&x,&y);
map1[x][y] = map1[y][x] = 1;
p[x].push_back(y);
p[y].push_back(x);
dis[x]++;
dis[y]++;
}
int ans = 1000000;
for(int i = 1; i <= n; i++)
{
for(int j = 0; j < p[i].size(); j++ )
{
for(int k = j+1; k < p[i].size(); k++)
{
x = p[i][j];
y = p[i][k];
if(map1[x][y]){
ans = min(ans, dis[i] + dis[x] +dis[y] - 6);
}
}
}
}
if(ans == 1000000) ans = -1;
printf("%d\n",ans);
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
vector < int > p[4100];
int map1[4100][4100];
int dis[4100];
int n,m;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
map1[i][j] = 0;
for(int i = 1; i <= n; i++) { p[i].clear(); dis[i] = 0; }
int x,y;
for(int i = 1; i <= m; i++ )
{
scanf("%d%d",&x,&y);
map1[x][y] = map1[y][x] = 1;
p[x].push_back(y);
p[y].push_back(x);
dis[x]++;
dis[y]++;
}
int ans = 1000000;
for(int i = 1; i <= n; i++)
{
for(int j = 0; j < p[i].size(); j++ )
{
for(int k = j+1; k < p[i].size(); k++)
{
x = p[i][j];
y = p[i][k];
if(map1[x][y]){
ans = min(ans, dis[i] + dis[x] +dis[y] - 6);
}
}
}
}
if(ans == 1000000) ans = -1;
printf("%d\n",ans);
}
return 0;
}