CodeForces - 1082D 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

Input

3
2 2 2

Output

YES 2
2
1 2
2 3

Input

5
1 4 1 1 1

Output

YES 2
4
1 2
3 2
4 2
5 2

Input

3
1 1 1

Output

NO

Note

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

.

d1=1≤a1=2

d2=2≤a2=2

 

d3=1≤a3=2

 

d1=1≤a1=1

d2=4≤a2=4

 

d3=1≤a3=1

 

d4=1≤a4=1

题意:给你每个点的最大读,是否能形成树,不能输出NO,能构造直径最大的树

题解:先按度数排序,我们构造直径最长,那肯定是尽可能连成一条线,所以度数>=2的直接连接,在把两个度数为1的连接在两头就可以了,其他点连接在度数有剩余的点上

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=500+10;
#define INF 0x3f3f3f3f
struct node
{
    int id, val;
    node(int id_,int val_):id(id_),val(val_){}

};
struct point
{
    int id, val;
}degree[N];
int n;
pair<int, int> ans[N];
int len;
bool cmp(point x,point y)
{
    return x.val > y.val;
}
int main()
{
    int x, val;
	while(~scanf("%d", &n)) {
        queue<node> q;
        for(int i = 1; i <= n; i++) {
            scanf("%d", &val);
            degree[i].val = val;
            degree[i].id = i;
        }
        sort(degree + 1, degree + 1 + n, cmp);
        int flag = 1; // flag表示连接到哪个点了
        for(int i = 2; i <= n; i++) {
            if(degree[i-1].val >= 1) {
                degree[i].val--;
                degree[i-1].val--;
                ans[++len].first = degree[i].id;
                ans[len].second = degree[i - 1].id;
                flag = i ;
            }
            else
            {
                if(degree[1].val >= 1) {
                    degree[i].val--;
                    degree[1].val--;
                    ans[++len].first = degree[i].id;
                    ans[len].second = degree[1].id;
                    flag = i;
                }
                break;
            }
        }
        for(int i = 1; i <= flag; i++)
        {
            if(degree[i].val > 0) q.push(node(degree[i].id, degree[i].val));
        }

        for(int i = flag + 1; i <= n; i++) {
            if(q.empty()) { // 没有可连接的点,不符合条件
                flag = 0;
                break;
            }
            node now = q.front(); q.pop();
            now.val--;
            degree[i].val--;
            ans[++len].first = degree[i].id;
            ans[len].second = now.id;
            if(now.val > 0) q.push(now);
            if(degree[i].val > 0) q.push(node(degree[i].id, degree[i].val));

        }
        if(!flag) printf("NO\n");
        else
        {
            printf("YES %d\n%d\n", flag - 1, n - 1);
            for(int i = 1; i <= len; i++) {
                printf("%d %d\n", ans[i].first, ans[i].second);
            }
        }
    }
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值