zoukankan      html  css  js  c++  java
  • CF div2 318 B

    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.

    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.

    题目大意是让找一个由三个点组成的环并且三个点的度数加起来“最小”,这里的“最小”是指成环的三个顶点之间的边所形成的度数不算,也就是三个顶点成环的最小度数-6 就是要求的答案,如果没有这样的环输出 -1.

    看了一下顶点数才4000,边也才4000 用邻接矩阵存图,然后再用一个数组记录一下各个顶点的度数,然后直接枚举每个顶点取总度数最小的。代码如下

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <queue>
    #include <vector>
    #include <stack>
    
    using namespace std;
    
    const int M = 10005;
    const int maxn = 5000;
    typedef long long ll;
    const int inf = 9999999;
    
    vector<int>G[maxn];
    queue<int>Q;
    stack<int>st;
    
    
    int a[maxn][maxn];
    int vis[maxn];
    
    int main()
    {
    
        int n,m;
    
        scanf("%d%d",&n,&m);
        int u,v;
        memset(vis,0,sizeof(vis));
        memset(a,0,sizeof(a));
        for(int i=1;i<=m;i++){
            scanf("%d%d",&u,&v);
            a[u][v] = a[v][u] = 1;
            vis[u]++;
            vis[v]++;
        }
       int  ans = inf;
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++){
                if(a[i][j]){
                    for(int k=j+1;k<=n;k++){
                        if(a[i][k]&&a[k][j]){
                            ans = min(ans,vis[i]+vis[j]+vis[k]);
                        }
                    }
                }
            }
        }
        if(ans == inf ) printf("-1
    ");
        else printf("%d
    ",ans-6);
    
    
    
        return 0;
    }
    View Code
    It is loneliness that make you different,not gregariousness
  • 相关阅读:
    在centos6.5上搭建elk6.5.3
    mysql 表信息查询
    pychram远程调试
    老程序员的十条告诫
    看清程序员要走的道路
    一个程序员的十年总结
    一个资深程序员成功的背后
    weisheng.cf 网站更新动态
    Endless Night 题解
    2021.1.17高一模拟赛题解
  • 原文地址:https://www.cnblogs.com/lmlyzxiao/p/4811532.html
Copyright © 2011-2022 走看看