题目大意:
让你构造出来一个图,使得其包含N个点,并且min(G的直径,G的补图的直径)==k;
思路(这个博客写的很好有证明:http://blog.csdn.net/jasonvictoryan/article/details/54572646):
1、首先明确无向图的直径的定义:对于一个无向图,其任意两点间距离的最大值,就是无向图的直径长度。
2、这种题拿到手没有思路怎么办,不妨枚举出来点情况进行讨论。
通过枚举不难发现:
①k==1的时候无解,k>=4的时候也是无解的。
②k==2的时候我们只要按照第二组样例那样形成一条从1-n的链即可。其N的限制通过枚举也能发现当N<=4的时候无解。
③k==3的时候,我们搞出来一条1-3的链,再将其他孤立点连入节点3即可。其N的限制通过枚举也能发现,当N<=3的时候无解。
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
if(m==1||m>=4)
{
printf("-1\n");
}
else
{
if(m==2)
{
if(n>=5)
{
printf("%d\n",n-1);
for(int i=1;i<=n-1;i++)
{
printf("%d %d\n",i,i+1);
}
}
else printf("-1\n");
}
else
{
if(n>=4)
{
printf("%d\n",n-1);
printf("1 2\n2 3\n");
for(int i=4;i<=n;i++)
{
printf("3 %d\n",i);
}
}
else printf("-1\n");
}
}
}
}