zoukankan      html  css  js  c++  java
  • CF #318 B. Bear and Three Musketeers

    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 ≤ 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 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 #include <stdio.h>
     2 #include <string.h>
     3 #include <vector>
     4 #include <algorithm>
     5 using namespace std;
     6 const int inf=0x3f3f3f3f;
     7 vector <int> edg[4005];
     8 int vis[4005];
     9     int x,y,num=inf;
    10         int n,m,sum;
    11 void check(int v,int ud)
    12 {
    13     int i;
    14     for(i=0;i<edg[v].size();i++)
    15     {
    16         int u=edg[v][i];
    17         if(vis[u]==ud && (sum+edg[v].size()+edg[u].size()-6)<num)
    18         {
    19             num=sum+edg[v].size()+edg[u].size()-6;
    20         }
    21     }
    22 }
    23 int main()
    24 {
    25     int i,j,k;
    26     scanf("%d %d",&n,&m);
    27     memset(vis,0,sizeof(vis));
    28     for(i=0;i<=n;i++)
    29         if(edg[i].size())
    30             edg[i].clear();
    31     for(i=1;i<=m;i++)
    32     {
    33         scanf("%d %d",&x,&y);
    34         edg[x].push_back(y);
    35         edg[y].push_back(x);
    36     }
    37     for(i=1;i<=n;i++)
    38     {
    39         sum=edg[i].size();
    40         for(j=0;j<edg[i].size();j++)
    41         {
    42             int v=edg[i][j];
    43             vis[v]=i;
    44             check(v,i);
    45         }
    46     }
    47     if(num!=inf)
    48         printf("%d
    ",num);
    49     else
    50         printf("-1
    ");
    51     return 0;
    52 }
    View Code
  • 相关阅读:
    网络设备
    Linux常用操作
    工作常用笔记
    性能测试问题总结
    mysql性能分析
    Java8新特性学习笔记-CompletableFuture
    Java语言定义的线程状态分析
    Gatling-插件开发
    RabbitMQ记录
    Go常用库
  • 原文地址:https://www.cnblogs.com/cyd308/p/4770800.html
Copyright © 2011-2022 走看看