CF1082D: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

  • the graph contains no self-loops and no multiple edges;
  • the degree
  • 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

The second line contains

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

The

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

题意:构造一棵树,使得直径最长,需要满足每个点的度数di<=ai。

思路:我们选择ai最小的两个最为直径端点,然后把di>1的加到直径上去,剩下的度数为1的加到直径的枝桠上。

昨天没时间了没有写输出“NO”,WA3了。今天加上了就AC了。

给我30s可能就A了,加上最后一题水题没做。这一次CF血亏。

#include<bits/stdc++.h>
#define pii pair<int,int>
#define F first
#define S second
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=1000010;
int N,sum,L,S,T;
int b[maxn],ans; int f[maxn],c[maxn],tot;
pii a[maxn];
int main()
{
    scanf("%d",&N); ans=1;
    rep(i,1,N) scanf("%d",&a[i].F),a[i].S=i;
    sort(a+1,a+N+1);
    b[++L]=a[1].S; b[++L]=a[2].S;
    int pre=b[1],bg=0;
    rep(i,3,N){
        if(a[i].F>1){
            f[++tot]=pre,c[tot]=a[i].S,pre=a[i].S,ans++;
            if(!bg) bg=i;
        }
    }
    f[++tot]=pre,c[tot]=b[2];
    int pos=bg,F=true;
    rep(i,3,bg-1) {
       if(a[i].F==1) {
           while(a[pos].F<=2){
                pos++; if(pos==N+1) {F=false; break;}
           }
           if(!F) break;
           a[pos].F--; f[++tot]=a[i].S,c[tot]=a[pos].S;
       }
       else break;
    }
    if(!F||tot!=N-1) puts("NO");
    else {
        printf("YES %d\n%d\n",ans,N-1);
        rep(i,1,tot) printf("%d %d\n",f[i],c[i]);
    }
    return 0;

}

猜你喜欢

转载自www.cnblogs.com/hua-dong/p/10038505.html