天天看點

Codeforces Contest 1082 problem D Maximum Diameter Graph —— 圖的最短路最長

Graph constructive problems are back! This time the graph you are asked to build should match the following properties.

The graph is connected if and only if there exists a path between every pair of vertices.

The diameter (aka “longest shortest path”) of a connected undirected graph is the maximum number of edges in the shortest path between any pair of its vertices.

The degree of a vertex is the number of edges incident to it.

Given a sequence of n integers a1,a2,…,an construct a connected undirected graph of n vertices such that:

the graph contains no self-loops and no multiple edges;

the degree di of the i-th vertex doesn’t exceed ai (i.e. di≤ai);

the diameter of the graph is maximum possible.

Output the resulting graph or report that no solution exists.

Input

The first line contains a single integer n (3≤n≤500) — the number of vertices in the graph.

The second line contains n integers a1,a2,…,an (1≤ai≤n−1) — the upper limits to vertex degrees.

Output

Print “NO” if no graph can be constructed under the given conditions.

Otherwise print “YES” and the diameter of the resulting graph in the first line.

The second line should contain a single integer m — the number of edges in the resulting graph.

The i-th of the next m lines should contain two integers vi,ui (1≤vi,ui≤n, vi≠ui) — the description of the i-th edge. The graph should contain no multiple edges — for each pair (x,y) you output, you should output no more pairs (x,y) or (y,x).

Examples

inputCopy

3

2 2 2

outputCopy

YES 2

2

1 2

2 3

inputCopy

5

1 4 1 1 1

outputCopy

YES 2

4

1 2

3 2

4 2

5 2

inputCopy

3

1 1 1

outputCopy

NO

Note

Here are the graphs for the first two example cases. Both have diameter of 2.

題意:

給你n個點的最大度,讓你将這n個點畫成一張圖,使得所有點對的最短路徑中最長的長度最長。

題解:

容易想到使得圖聯通并且最短路徑最長的就是樹,并且樹所需要的邊數隻有n-1條,是以如果所有度數和/2<n-1,那麼就是NO,否則我們可以将所有的度數>=2的先連起來,最後在兩旁挂上兩個度數為1的,之後随便連就好了。

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
vector<int>one,oth;
#define pa pair<int,int>
vector<pa>ans;
int a[N];
int main()
{
    int n;
    scanf("%d",&n);
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        sum+=a[i];
        if(a[i]==1)
            one.push_back(i);
        else
            oth.push_back(i);
    }
    if(sum/2<n-1)
        return 0*printf("No\n");
    printf("YES ");
    for(int i=1;i<oth.size();i++)
        ans.push_back({oth[i-1],oth[i]}),a[oth[i-1]]--,a[oth[i]]--;
    if(one.size()==0)
        printf("%d\n",oth.size()-1);
    else if(one.size()==1)
    {
        for(int i=0;i<oth.size();i++)
        {
            if(a[oth[i]])
            {
                ans.push_back({one[0],oth[i]});
                break;
            }
        }
        printf("%d\n",oth.size());
    }
    else
    {
        int pos=0;

        ans.push_back({oth[0],one[0]});
        a[oth[0]]--;
        ans.push_back({oth[oth.size()-1],one[1]});
        a[oth[oth.size()-1]]--;
        for(int i=2;i<one.size();i++)
        {
            while(!a[oth[pos]])
                pos++;
            ans.push_back({one[i],oth[pos]});
            a[oth[pos]]--;
            
        }
        printf("%d\n",oth.size()+1);
    }
    printf("%d\n",ans.size());
    for(int i=0;i<ans.size();i++)
        printf("%d %d\n",ans[i].first,ans[i].second);
    return 0;
}