Codeforces 550D - Regular Bridge(構造)

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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章