• 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
    %d
    ",ans,N-1);
            rep(i,1,tot) printf("%d %d
    ",f[i],c[i]);
        }
        return 0;
    
    }
  • 相关阅读:
    Download: Microsoft Access Database Engine 2010 Redistributable
    18大顺丰不发航空件
    北京南站不是24*7的
    360压缩虽然有占霸道,但是for free,我已经不想去找破解软件了
    VS2010、SQL Server 2008和SQL Server 2012安装详解
    【新提醒】LENOVO_WIN7_SP1_UM_64_CN_RDVD远景Windows7,Windows8,旗舰版,系统下载,主题
    原来qq下载也有类似迅雷的功能了
    如意通5元卡办理了,可以用wifi热点了
    SQLEXPR.EXE 和 SQLEXPR32.EXE的区别 挨踢人 博客园
    HTTP Proxy Support
  • 原文地址:https://www.cnblogs.com/hua-dong/p/10038505.html
Copyright © 2020-2023  润新知