码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #318 (Div. 2) B Bear and Three Musketeers

时间:2015-08-30 09:54:16      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

不要想多了直接暴。对于u枚举a和b,判断一个是否连边,更新答案。

#include<bits/stdc++.h>
using namespace std;

int n,m;
const int maxn = 4001;
#define PB push_back
vector<int> G[maxn];
bool g[maxn][maxn];
int deg[maxn];
const int INF = 0x3f3f3f3f;
int main()
{
    //freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    for(int i = 0; i < m; i++){
        int a,b;scanf("%d%d",&a,&b);
        deg[a]++; deg[b]++;
        g[a][b] = g[b][a] = true;
        G[a].PB(b); G[b].PB(a);
    }
    int ans = INF;
    for(int u = 1; u <= n; u++){
        int sz = G[u].size();
        for(int i = 0; i < sz; i++){
            int a = G[u][i];
            for(int j = i+1; j < sz; j++){
                int b = G[u][j];
                if(g[a][b]){
                    ans = min(ans,deg[u]+deg[a]+deg[b]-6);
                }
            }
        }
    }
    if(ans<INF){
        printf("%d\n",ans);
    }else printf("-1\n");
    return 0;
}

 

Codeforces Round #318 (Div. 2) B Bear and Three Musketeers

标签:

原文地址:http://www.cnblogs.com/jerryRey/p/4770261.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!