【2020.12.03提高组模拟】黎明卿 (bondorudo)

【2020.12.03提高组模拟】黎明卿 (bondorudo)

思路: 由于m<=2n-1,所以ans只能是1~3,分三类

1.连成链,ans=1

2.连成环,但无法连接所有直径,ans=2。由于答案肯定为2,连完环后可以乱连。

3.连成环,且能连接所有直径,ans=3。然后继续乱连。

#include<iostream>
#include<cstdio>
using namespace std;
int n,m,b[55][55],tot;
int main()
{
    freopen("bondorudo.in","r",stdin);
    freopen("bondorudo.out","w",stdout);
    scanf("%d%d",&n,&m);
    if(m==n-1)
    {
        printf("1
");
        for(int i=1;i<n;i++)
        {
            printf("%d %d
",i,i+1);
        }
        fclose(stdin);fclose(stdout);return 0;
    }
    if(m<n+(n>>1)+(n&1))
    {
        printf("2
");
        for(int i=1;i<n;i++)
        {
            printf("%d %d
",i,i+1);
            b[i][i+1]=1;
        }
        printf("%d %d
",n,1);b[1][n]=1;
        tot=n;
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                if(tot==m){fclose(stdin);fclose(stdout);return 0;}
                if(!b[i][j])
                {
                    b[i][j]=1;
                    printf("%d %d
",i,j);
                    tot++;
                }
            }
        }
    } 
    else
    {
        printf("3
");
        for(int i=1;i<n;i++)
        {
            printf("%d %d
",i,i+1);
            b[i][i+1]=1;
        }
        printf("%d %d
",n,1);b[1][n]=1;
        tot=n;
        if(n%2)
        {
            for(int i=1;i<=n/2+1;i++)
            {
                printf("%d %d
",i,i+n/2);
                b[i][i+n/2]=1;
                tot++;
            }
            for(int i=1;i<=n;i++)
            {
                for(int j=i+1;j<=n;j++)
                {
                    if(tot==m){fclose(stdin);fclose(stdout);return 0;}
                    if(!b[i][j])
                    {
                        b[i][j]=1;
                        printf("%d %d
",i,j);
                        tot++;
                    }
                }
            }
        }
        else 
        {
            for(int i=1;i<=n/2;i++)
            {
                printf("%d %d
",i,i+n/2);
                b[i][i+n/2]=1;
                tot++;
            }
            for(int i=1;i<=n;i++)
            {
                for(int j=i+1;j<=n;j++)
                {
                    if(tot==m){fclose(stdin);fclose(stdout);return 0;}
                    if(!b[i][j])
                    {
                        b[i][j]=1;
                        printf("%d %d
",i,j);
                        tot++;
                    }
                }
            }
        }
    }
}
原文地址:https://www.cnblogs.com/HYDcn666/p/14082250.html