• 【codeforces 550D】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.

    【题目链接】:http://codeforces.com/contest/550/problem/D

    【题解】

    k为偶数的时候、发现画不出来;
    k为奇数的时候可以构造一下;
    这里写图片描述
    如图分成左右两块;
    左边是k+1个点的完全图,右边也是一样..
    左边那个完全图每个点的度数都为k;
    现在考虑把k+2节点和2*k+4号节点连在一起;
    这样k+2号节点度数为1;
    然后我们在左边那个完全图里面1..k-1号节点都接一条边到第k+2号节点上
    然后把
    1-2,3-4,5-6…(k-2)-(k-1)这(k-1)/2条边删掉;
    这样这k-1个节点的读还是k,然后k+2号节点的度变成了k;
    右边类似处理就好;
    (实际上直接节点编号都加上k+2输出就好)
    那条k+2到2*k+4之间的边则最后输出;

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%I64d",&x)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    const int MAXN = 2e2+10;
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    
    int k,n,m;
    bool bo[MAXN][MAXN];
    vector <pii> ans;
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        rei(k);
        if (!(k&1))
        {
            puts("NO");
            return 0;
        }
        if (k==1)
        {
            puts("YES");
            puts("2 1");
            puts("1 2");
            return 0;
        }
        rep1(i,1,k+1)
            rep1(j,1,k+1)
                if (i!=j)
                    bo[i][j] = true;
        int ma = (k-1)/2;
        int now = 1;
        rep1(i,1,ma)
        {
            bo[now][now+1] = false;
            bo[now+1][now] = false;
            bo[now][k+2] = bo[k+2][now] = true;
            bo[now+1][k+2] = bo[k+2][now+1] = true;
            now+=2;
        }
        rep1(i,1,k+2)
            rep1(j,i+1,k+2)
                if (bo[i][j])
                    ans.pb(mp(i,j));
        int m = ans.size();
        puts("YES");
        printf("%d %d
    ",(k+2)*2,m*2+1);
        rep1(i,0,m-1)
        {
            int x = ans[i].fi,y = ans[i].se;
            printf("%d %d
    ",x,y);
            printf("%d %d
    ",x+k+2,y+k+2);
        }
        printf("%d %d
    ",k+2,2*k+4);
        return 0;
    }
  • 相关阅读:
    Shell Sort 希尔排序
    Quick Sort 快速排序
    Merge Sort 归并排序
    Insertion Sort
    Bubble Sort
    dubbo的异常栈问题
    IoC 容器
    .Net Core集成RabbitMQ
    .NET CORE Skywalking的集成
    制造业的信息化之路
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626782.html
Copyright © 2020-2023  润新知