Codeforces 550D - Regular Bridge(构造)

本文介绍如何构造一个包含至少一个桥的k-正则无向图的方法,并提供了解题思路与AC代码。当k为奇数时,通过特定节点连接方式确保图中存在桥。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Regular Bridge
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 ≤ n, a ≠ b), that mean that there is an edge connecting the vertices a 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).

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-1个,那么添加k-1个点,并与桥上的点连接.
要使得这k-1个点的度数都为k,那么还需要增加两个节点.
这两个节点都分别与k-1个点相连接,同时这两个新节点也相互连接.
剩下的k-1个节点还需要与k-3个节点相互连接.
注意k为偶数的情况是不能构造的.

AC代码:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(0);
    int k;
    cin >> k;
    if(k % 2 == 0)
    {
        cout << "NO" << endl;
        return 0;
    }
    cout << "YES" << endl;
    if(k == 1)
    {
        cout << 2 << " " << 1 << endl;
        cout << 1 << " " << 2 << endl;
        return 0;
    }
    int n = k + 2;
    cout << n*2 << " " << n*k << endl;
    cout << n-1 << " " << n-2 << endl;
    cout << 2*n-1 << " " << 2*n-2 << endl;
    for(int i = 1; i <= n-3; i++)
    {
        cout << n << " " << i << endl;
        cout << n-1 << " " << i << endl;
        cout << n-2 << " " << i << endl;
        cout << 2*n << " " << i+n << endl;
        cout << 2*n-1 << " " << i+n << endl;
        cout << 2*n-2 << " " << i+n << endl;
    }
    for(int i = 1; i <= n-3; i++)
    {
        int j = i + 1 + (i & 1);
        while(j <= n-3)
        {
            cout << i << " " << j << endl;
            cout << i+n << " " << j+n << endl;
            j++;
        }
    }
    cout << n << " " << 2*n << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值