Bear and Three Musketeers
2015-08-30 12:07
288 查看
B. Bear and Three Musketeers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 ≤ n, ai ≠ 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).
Sample test(s)
input
output
input
output
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.
思路:
就是要找到三个火枪手都互相认识,找到就输出度数(要去重)。找不到就输出-1.
AC代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<set>
using namespace std;
typedef __int64 ll;
#define T 4005
bool g[T][T];
int dge[T];
int main()
{
/*freopen("input.txt","r",stdin);*/
int n,m,i,j,k;
while(~scanf("%d%d",&n,&m))
{
memset(g,false,sizeof(g));
memset(dge,0,sizeof(dge));
int u,v,res=0x3f3f3f3f;
while(m--)
{
scanf("%d%d",&u,&v);
g[u][v]=g[v][u]=true;
dge[u]++;dge[v]++;
}
for(i=1;i<=n;++i)
for(j=i+1;j<=n;++j)
if(g[i][j])
for(k=j+1;k<=n;++k)
if(g[j][k]&&g[i][k])
res=min(res,dge[i]+dge[j]+dge[k]-6);
if(res==0x3f3f3f3f)
printf("-1\n");
else
printf("%d\n",res);
}
return 0;
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 ≤ n, ai ≠ 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).
Sample test(s)
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.
思路:
就是要找到三个火枪手都互相认识,找到就输出度数(要去重)。找不到就输出-1.
AC代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<set>
using namespace std;
typedef __int64 ll;
#define T 4005
bool g[T][T];
int dge[T];
int main()
{
/*freopen("input.txt","r",stdin);*/
int n,m,i,j,k;
while(~scanf("%d%d",&n,&m))
{
memset(g,false,sizeof(g));
memset(dge,0,sizeof(dge));
int u,v,res=0x3f3f3f3f;
while(m--)
{
scanf("%d%d",&u,&v);
g[u][v]=g[v][u]=true;
dge[u]++;dge[v]++;
}
for(i=1;i<=n;++i)
for(j=i+1;j<=n;++j)
if(g[i][j])
for(k=j+1;k<=n;++k)
if(g[j][k]&&g[i][k])
res=min(res,dge[i]+dge[j]+dge[k]-6);
if(res==0x3f3f3f3f)
printf("-1\n");
else
printf("%d\n",res);
}
return 0;
}
相关文章推荐
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- Codeforces Round #265 (Div. 2)
- Codeforces #310 div2 C. Case of Matryoshkas
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- codeforces 16 Problem E fish
- Codeforces round #247 for Div. 2
- Codeforces Round #246 (Div. 2)
- Codeforces #264(div 2)D.Gargari and Permutations
- Codeforces Round #236 (Div. 2)------A,B
- codeforces 257 div2 B
- Codeforces Gym100571A Cursed Query
- Codeforces Gym100342E Minima
- Codeforces Gym100342J Triatrip
- Codeforces Gym100286B Blind Walk (dfs)
- Codeforces Gym100342J Triatrip