zoukankan      html  css  js  c++  java
  • codeForces 574b Bear and Three Musketeers

    一种巧妙到暴力方式,这题到抽象化:在一个无向图中,找一个度数和最小到三阶到圈。
    首先对边进行枚举,这样确定了两个顶点,然后在对点进行枚举,找到一个可以构成三元圈到点,则计算他们到度数和。
    最后保存最小到度数和到三元圈即可。
     
    Bear and Three Musketeers
    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
    Submit Status

    Description

    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 ≤ nai ≠ 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 Input

    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


    #include<iostream>
    #include<stdio.h>
    using namespace std;
    const int maxn = 4005;
    struct Node{
        int a,b;
    }edg[maxn];
    int mapp[maxn][maxn];
    int deg[maxn];
    int main(){
         int n,m;
         scanf("%d%d",&n,&m);
        for(int i=0;i<=n;i++)
          for(int j=0;j<=n;j++)
            mapp[i][j]=0;
        for(int i=0;i<=n;i++) deg[i]=0;
        for(int i=0;i<m;i++){
            int tmp1,tmp2;
            scanf("%d%d",&tmp1,&tmp2);
            edg[i].a=tmp1;
            edg[i].b=tmp2;
            mapp[tmp1][tmp2]=mapp[tmp2][tmp1]=1;
            deg[tmp1]++;
            deg[tmp2]++;
        }
        int inf=0x7fffffff;
        int ans=inf;
        for(int i=0;i<m;i++){
            for(int j=1;j<=n;j++){
                if(mapp[j][edg[i].a]&&mapp[j][edg[i].b]){
                    ans=min(ans,(deg[j]+deg[edg[i].a]+deg[edg[i].b]));
                }
            }
        }
        if(ans<inf){
            printf("%d
    ",ans-6);
        }else{
            printf("-1
    ");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    django -- 信号
    django缓存设置
    django-debug-toolbar 插件的使用
    scrapy基本操作流程
    scrapy框架持久化存储
    scrapy基础
    phantomJS,谷歌无头浏览器, 模拟登陆qq空间
    python爬虫--selenium
    pytorch掉坑记录:model.eval的作用
    numpy常用函数
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/5678722.html
Copyright © 2011-2022 走看看