Codeforces Round #318 (Div. 2)

Source: Internet
Author: User

<pre name= "code" class= "CPP" >

Idea: The most violent idea, with a triple loop to traverse, the problem gives the time is 2 seconds, and the CF server must be very good, so time should be no pressure, but my heart has been no end, the total feeling will time out.

Here, look at the code of Daniel, learn a new idea, for a triple loop, you can find out what the relationship between their 22, with pair, or strcut to save, the topic of M is 4000, far less than N, this will make time pressure to reduce a lot,


62ms


#include <bits/stdc++.h>using namespace std;const int Inf=0x3f3f3f3f;const int Maxn=5005;int g[maxn][maxn];int Degree[maxn];int Main (void) {    int n,m;    scanf ("%d%d", &n,&m);    memset (degree,0,sizeof (degree));    memset (G,0,sizeof (g));    for (int i=1;i<=m;i++)    {        int a, b;        scanf ("%d%d", &a,&b);        G[a][b]=1;        G[b][a]=1;        degree[a]++;        degree[b]++;    }    int minn=inf;    for (int i=1;i<=n;i++)    {for        (int j=i+1;j<=n;j++)        {            if (G[i][j])            {for                (int k=j+1 ; k<=n;k++)                {                    if (G[i][k]&&g[j][k])                    {                        minn=min (Minn,degree[i]+degree[j]+degree[k] -6)    ;    }}}} if (minn!=inf)    printf ("%d\n", Minn);    else printf (" -1\n");    return 0;}


31ms


#include <bits/stdc++.h>using namespace std;const int Inf=0x3f3f3f3f;const int Maxn=5005;int g[maxn][maxn];int Degree[maxn];int Main (void) {    int n,m;    scanf ("%d%d", &n,&m);    memset (degree,0,sizeof (degree));    memset (G,0,sizeof (g));    for (int i=1;i<=m;i++)    {        int a, b;        scanf ("%d%d", &a,&b);        G[a][b]=1;        G[b][a]=1;        degree[a]++;        degree[b]++;    }    int minn=inf;    for (int i=1;i<=n;i++)    {for        (int j=i+1;j<=n;j++)        {            if (G[i][j])            {for                (int k=j+1 ; k<=n;k++)                {                    if (G[i][k]&&g[j][k])                    {                        minn=min (Minn,degree[i]+degree[j]+degree[k] -6)    ;    }}}} if (minn!=inf)    printf ("%d\n", Minn);    else printf (" -1\n");    return 0;}


Copyright NOTICE: Reprint must please indicate the source, thank you

Codeforces Round #318 (Div. 2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.