cf Educational Codeforces Round 43 D. Degree Set

原题:

D. Degree Set
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a sequence of n positive integers d1, d2, …, dn (d1 < d2 < … < dn). Your task is to construct an undirected graph such that:

there are exactly dn + 1 vertices;
there are no self-loops;
there are no multiple edges;
there are no more than 10^6 edges;
its degree set is equal to d.
Vertices should be numbered 1 through (dn + 1).

Degree sequence is an array a with length equal to the number of vertices in a graph such that ai is the number of vertices adjacent to i-th vertex.

Degree set is a sorted in increasing order sequence of all distinct values from the degree sequence.

It is guaranteed that there exists such a graph that all the conditions hold, and it contains no more than 10^6 edges.

Print the resulting graph.

Input
The first line contains one integer n (1 ≤ n ≤ 300) — the size of the degree set.

The second line contains n integers d1, d2, …, dn (1 ≤ di ≤ 1000, d1 < d2 < … < dn) — the degree set.

Output
In the first line print one integer m (1 ≤ m ≤ 106) — the number of edges in the resulting graph. It is guaranteed that there exists such a graph that all the conditions hold and it contains no more than 106 edges.

Each of the next m lines should contain two integers vi and ui (1 ≤ vi, ui ≤ dn + 1) — the description of the i-th edge.

Examples
input
3
2 3 4
output
8
3 1
4 2
4 5
2 5
5 1
3 2
2 1
5 3
input
3
1 2 3
output
4
1 2
1 3
1 4
2 3

中文:

有一个图,这个图中所有节点的度为d1,d2,…dn,从小到大。

这个图没有重边,没有定点上的边连接该定点自己,一共dn+1个定点,边数不超过10^6。

现在让你找出这么样的一个图来,满足上面的条件。

代码:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;


struct node
{
    int f,t;
};

int n;
deque<int> G;

deque<node> dfs(int x,deque<int> g)
{
    deque<node> v;
    if(g.empty())
        return v;
    for(int i=0;i<g.front();i++)
    {
        for(int j=x+i+1;j<=x+g.back();j++)
        {
            v.push_back(node{x+i,j});
        }
    }
    for(int i=1;i<g.size();i++)
        g[i]-=g[0];

    int ind= x+g.front();
    g.pop_front();
    if(!g.empty())
        g.pop_back();

    deque<node> res=dfs(ind,g);
    for(int i=0;i<res.size();i++)
        v.push_back(res[i]);

    return v;
}

int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n)
    {
        G.clear();
        int res;
        for(int i=0;i<n;i++)
        {
            cin>>res;
            G.push_back(res);
        }
        deque<node> ans=dfs(0,G);
        cout<<ans.size()<<endl;
        for(int i=0;i<ans.size();i++)
            cout<<ans[i].f+1<<" "<<ans[i].t+1<<endl;

    }
    return 0;
}

思路:

这题很难想,看了题解,感觉构造的方式很巧妙的。

由于有 dn+1 d n + 1 个节点,那么度为 dn d n 的节点一定是用边连接上了所有的节点。

这里如下构造:
第一步:找出 d1 d 1 个节点连接所有的节点,那么当前的图中会有两个度的值,分别是 d1 d 1 dn d n ,度为 dn d n 的节点有 d1 d 1 个,度为 d1 d 1 的节点有 dn+1d1 d n + 1 − d 1 个。

第二步:将 d2 d 2 dn1 d n − 1 之间的度,全部都减去 d1 d 1 ,其目的是要在第一步中构造的图中,获取一个子图,这个子图同样也可以用解决这个问题的算法来进行递归的求解。

目前已经构造处了度 d1 d 1 dn d n ,且度为 dn d n 的节点有 d1 d 1 个,度为 d1 d 1 的节点有 dn+1d1 d n + 1 − d 1 个。

第三步:取一部分度为 d1 d 1 的节点,来构造子图,这个子图的格式同样满足题目中的要求,那么取多少个度 d1 d 1 的节点合适呢?原问题中要求顶点的个数是 dn+1 d n + 1 个,在经过第一步和第二步以后,剩余的度数变成 [d2d1,d3d1,...,dn1d1] [ d 2 − d 1 , d 3 − d 1 , . . . , d n − 1 − d 1 ] ,相当于当前最大的度是 dn1d1 d n − 1 − d 1 ,所以要有 dn1d1+1 d n − 1 − d 1 + 1 个顶点。

所以,留给度 d1 d 1 的顶点的个数等于 (dn1d1+1)(dn1d1+1)=dndn1 ( d n − 1 − d 1 + 1 ) − ( d n − 1 − d 1 + 1 ) = d n − d n − 1 ,其余的 dn1d1+1 d n − 1 − d 1 + 1 个定点进行递归的处理,处理方式同样遵循第1,2,3步。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值