当前位置 : 主页 > 编程语言 > c语言 >

Codeforces Round #306 (Div. 2) D. Regular Bridge 构造

来源:互联网 收集:自由互联 发布时间:2023-09-06
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 undirec

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
inputCopy
1
outputCopy
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必须为奇数,那么怎么构造呢?桥的端点先与 k-1 个点相连,再加两个点,与这K-1个点相连,然后它们自己相连,那么这K-1 个点的度数为 k-3 ,怎么做? 想象为多边形的边就可以了,不连对角线即可。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 500005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-7
#define pll pair<ll,ll>



ll quickpow(ll a, ll b) {
    ll ans = 1;
    a = a % mod;
    while (b > 0) {
        if (b % 2)ans = ans * a;
        b = b / 2;
        a = a * a;
    }
    return ans;
}

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}


int main()
{
    ios::sync_with_stdio(false);
    int k;
    cin >> k;
    if (k % 2 == 0) {
        cout << "NO" << endl;

    }
    else {
        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);
        //  cout << j << endl;
            while (j <= n - 3) {
                cout << i << ' ' << j << endl;
                cout << i + n << ' ' << j + n << endl;
                j++;
            }
        }
        cout << n << ' ' << 2 * n << endl;
    }
}
【本文来源:美国服务器 http://www.558idc.com/mg.html提供,感恩】
网友评论