【打CF,学算法——三星级】CodeForces 550D Regular Bridge (构造)

【CF简介】

提交链接:CF 550D


题面:

D. Regular Bridge
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

An undirected graph is called k-regular, if the degrees of all its vertices are equalk. An edge of a connected graph is called abridge, if after removing it the graph is being split into two connected components.

Build a connected undirected k-regular graph containing at least one bridge, or else state that such graph doesn't exist.

Input

The single line of the input contains integer k (1 ≤ k ≤ 100) — the required degree of the vertices of the regular graph.

Output

Print "NO" (without quotes), if such graph doesn't exist.

Otherwise, print "YES" in the first line and the description of any suitable graph in the next lines.

The description of the made graph must start with numbers n and m — the number of vertices and edges respectively.

Each of the next m lines must contain two integers,a and b (1 ≤ a, b ≤ n,a ≠ b), that mean that there is an edge connecting the verticesa and b. A graph shouldn't contain multiple edges and edges that lead from a vertex to itself. A graph must be connected, the degrees of all vertices of the graph must be equalk. At least one edge of the graph must be a bridge. You can print the edges of the graph in any order. You can print the ends of each edge in any order.

The constructed graph must contain at most 106 vertices and106 edges (it is guaranteed that if at least one graph that meets the requirements exists, then there also exists the graph with at most106 vertices and at most106 edges).

Examples
Input
1
Output
YES
2 1
1 2
Note

In the sample from the statement there is a suitable graph consisting of two vertices, connected by a single edge.


题意:

    求构造一个无向图,图中每个节点的度数都为k,且该图存在至少一个割边,即断开后,图不连通。k小于等于100,要求构造的图中,点数不大于10^6,且边数也不大于10^6。


解题:

     先画一条割边,割边上的一点A,必然会有另外K-1条边连出去,对于A这一侧,设还需添加a个节点,x条边,根据度数和边的关系可得。(k-1)*(k-1)+k*a=2*x,明显(k-1)*(k-1)为偶数,欲使等式有解,最小的a取值为2。明显当k为奇数无解。

      于是开始构造,A侧新加2个节点,将这两个节点与k-1个节点相连,且两个节点间连一条边,这两点已符合度数要求,剩余的k-1个点通过循环后连的方式补足剩余度数即可,B侧同理。


代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <cstdio>
#include <vector>
#define pe(u,v) printf("%d %d\n",u,v)
using namespace std;
void show(int x,int y,int t)
{
	int tmp;
	for(int i=x;i<=y;i++)
	{
		for(int j=1;j<=t;j++)
		{
			tmp=i+j;
			if(tmp>y)
				tmp=tmp-y+x-1;
			pe(i,tmp);
		}
	}
}
int main()
{
    int k;
	scanf("%d",&k);
	if(k%2)
	{
		printf("YES\n");
		if(k==1)
			printf("2 1\n1 2\n");
        else
		{
			printf("%d %d\n",2*k+4,k*k+2*k);
			for(int i=1;i<=k-1;i++)
		    {
				pe(1,i+1);
				pe(k+1,i+1);
				pe(k+2,i+1);
			}
			pe(k+1,k+2);
			show(2,k,(k-3)/2);
			pe(1,k+3);
			for(int i=1;i<=k-1;i++)
			{
				pe(k+3,k+3+i);
				pe(2*k+3,k+3+i);
				pe(2*k+4,k+3+i);
			}
			pe(2*k+3,2*k+4);
			show(k+4,2*k+2,(k-3)/2);
		}
	}
	else
		printf("NO\n");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值