Educational Codeforces Round 43 (Rated for Div. 2)D. Degree set(贪心递归构造)

D. Degree Set

Stetement

    You are given a sequence of n positive integers d 1 , d 2 , . . . , d n ( d 1 < d 2 < . . . < d n ) . Your task is to construct an undirected graph such that:
    there are exactly d n + 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 d n + 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 106 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 d 1 , d 2 , . . . , d n ( 1 d i 1000 , d 1 < d 2 < . . . < d n ) — the degree set.

Output

    In the first line print one integer m ( 1 m 10 6 ) — 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 10 6 edges.
    Each of the next m lines should contain two integers v i and u i ( 1 v i , u i d n + 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

题意

    给出一个有n个不同的数的度数集合,让你构造出一个图使得这个图满足它恰好有 d n + 1 个点,且无重边自环,边不超过 10 6 条,且这个图中所有的点的度数insert到一个set中后与输入的集合相同。

思路&&分析

    首先可以确定的是,度数为d[n]的点肯定和其他所有点都连了边,那么我们假设度数为d[1]的点所有的边都是和度数为d[n]的点连的边,于是我们就有了d[1]个度数为d[n]的点,这时候相当于所有点的度数都被占去了d[1]。那么我们把d[2]~d[n-1]所有都减去d[1],于是就将他转化成了一个子问题,但是这个子问题要求点数恰好是当前的最大度数+1,也就是d[n-1]-d[1]+1。我们知道我们目前构造出来的这个图除了d[1]个度数最大的点还剩下d[n]-d[1]+1个点,那么我们只要取d[n]-d[n-1]个度数为d[1]的点就行了。剩下的全部递归下去,边界的话是没有点时return,或者只有一种度数时连完全图。

Code

#pragma GCC optimize(3)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
bool Finish_read;
template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}
template<class T>inline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+'0');}
template<class T>inline void writeln(T x){if(x<0)putchar('-');x=abs(x);print(x);putchar('\n');}
template<class T>inline void write(T x){if(x<0)putchar('-');x=abs(x);print(x);}
/*================Header Template==============*/
int n,d[305],cnt,u[1000006],v[1000006];
inline void solve(int l,int r,int vl,int vr) {
    if(l>r)
        return;
    if(l==r) {
        for(int i=vr;i>vl;--i)
            for(int j=vl;j<i;j++)
                cnt++,u[cnt]=i,v[cnt]=j;
        return;
    } 
    int w=d[l];
    for(int i=vr;i>vr-w;--i)
        for(int j=vl;j<i;j++)
            cnt++,u[cnt]=i,v[cnt]=j;
    int nxtl=vl+d[r]-d[r-1],nxtr=vr-d[l];
    for(int i=r;i>=l;--i)
        d[i]-=d[l];
    solve(l+1,r-1,nxtl,nxtr);
}
int main() {
    read(n);
    for(int i=1;i<=n;i++)
        read(d[i]);
    solve(1,n,1,d[n]+1);
    writeln(cnt);
    for(int i=1;i<=cnt;i++)
        printf("%d %d\n",u[i],v[i]);
}

猜你喜欢

转载自blog.csdn.net/effervescence/article/details/80156143