码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces550D:Regular Bridge

时间:2015-06-09 17:16:47      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:codeforces

An undirected graph is called k-regular, if the degrees of all its vertices are equal k. An edge of a connected graph is called a bridge, 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?≤?na?≠?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 equal k. 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 and 106 edges (it is guaranteed that if at least one graph that meets the requirements exists, then there also exists the graph with at most 106 vertices and at most 106 edges).

Sample test(s)
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,并且在无向图中有一条边如果去掉之后会把这个无向图分成两个部分,要求输出这个图


思路:

首先,我们可以确定,这个图可以分为两个对称的部分,而这两个对称的部分就是以一条边来连接的,这条边就是题目中要求的桥

确定了对称的特性之后,我们只需要着力讨论一边的状况即可

假设连接这条桥的节点为n,那么n节点就只剩下k-1的边可以分配,也就是对于这一边我们最少还需要k-1个节点,那么我们先放置k-1个节点,并且使得它们都与n相连,那么n现在的度为k,但是现在这k-1个节点的度数都只有1,而且如果这k-1个节点的话,是无法使得它们的度数都为k的

那么怎么办呢?证明我们还需要增加节点才行,那么增加几个呢?增加一个是不行的,因为只增加1个节点的话,我们是无法使得这个新增加的节点达到K的度的,那么两个呢?

我们可以发现,增加两个刚刚好,首先我们令新增加的两个节点编号为n-1,n-2,首先使得它们互相相连,那么他们正好都只剩下k-1个度了,正好可以与之前的k-1个节点相连

在相连之后,我们可以发现之前k-1个节点,编号为n-3~1,现在都只剩下k-3个度了,那么问题就解决了,对于这些节点,我们只需要奇偶来考虑,就可以使得它们都满足k个度的要求,那么问题就解决了,图形如下

而且我们要注意,偶数的度是不可行的,因为偶数的度的情况下,编号为2的度是处理不了的


技术分享


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 2000005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8

int main()
{
    int n,k,i,j;
    while(~scanf("%d",&k))
    {
        if(k == 1)
        {
            printf("YES\n2 1\n1 2\n");
            continue;
        }
        if(k%2==0)//偶数不行
        {
            printf("NO\n");
            continue;
        }
        n = k+2;//我们要增加两个节点
        printf("YES\n");
        printf("%d %d\n",n*2,n*k);//有两边,每个节点有k个度
        printf("%d %d\n",n-1,n-2);//让新增加的节点相连接
        printf("%d %d\n",2*n-1,2*n-2);//对称
        for(i = 1; i<=n-3; i++)
        {
            //让n,n-1,n-2与k-1个节点相连接
            printf("%d %d\n",n,i);
            printf("%d %d\n",n-1,i);
            printf("%d %d\n",n-2,i);
            //对称
            printf("%d %d\n",2*n,n+i);
            printf("%d %d\n",2*n-1,n+i);
            printf("%d %d\n",2*n-2,n+i);
        }
        for(i = 1; i<=n-3; i++)//k-1个节点相互连接
        {
            if(i%2) j = i+2;//奇数跳过一个与后面的相连
            else j = i+1;//偶数直接与后面相连
            while(j<=n-3)
            {
                printf("%d %d\n",i,j);
                printf("%d %d\n",n+i,n+j);
                j++;
            }
        }
        printf("%d %d\n",n,n+n);//连接两块

    }

    return 0;
}


Codeforces550D:Regular Bridge

标签:codeforces

原文地址:http://blog.csdn.net/libin56842/article/details/46427673

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!