题目描述 Description###

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 \(a_1,a_2,…,a_n\) 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. \(d_i ≤a_i\) );

the diameter of the graph is maximum possible.

Output the resulting graph or report that no solution exists.

输入描述 Input Description###

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

The second line contains n integers $a_1,a_2,…,a_n $ (\(1≤a_i≤n−1\) ) — the upper limits to vertex degrees.

输出描述 Output Description###

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 \(v_i,u_i\) (\(1≤v_i,u_i≤n, v_i≠u_i\) ) — 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).

样例输入 Sample Input###

5
1 4 1 1 1

样例输出 Sample Output###

YES 2
4
1 2
3 2
4 2
5 2

数据范围及提示 Data Size & Hint###

见上面

之前的一些废话###

明天考高代,所以这个比赛根本没人参加,我也是就把这道题首刀了之后就去复习了

题解###

为了使我们的直径最长,我们通过画图发现直径两端的点度数为1,所以我们需要把度数最小的两个点放到直径两端,然后剩下的所有度数大于1的点都可以充当直径上的点,(可以证明这些点不放在直径上都是浪费),然后剩下的那些度数为1的点就用来充当直径上的分岔就好了。

最后一定是一棵树。

代码###

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long LL;
typedef pair<int,int> PII;
inline int read()
{
int x=0,f=1;char c=getchar();
while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){x=x*10+c-'0';c=getchar();}
return x*f;
}
bool e[510][510];
PII a[510];
int n,first,last,list[510],dm,val[510];
bool ok;
void add(int A,int B){e[A][B]=e[B][A]=1;}//printf("add:%d %d\n",A,B);}
int main()
{
n=read();
for(int i=1;i<=n;i++)a[i].first=read(),a[i].second=i;
sort(a+1,a+n+1);
first=a[1].second;last=a[2].second;
list[++dm]=first;val[dm]=a[1].first;
for(int i=3;i<=n;i++)
if(a[i].first>=2)list[++dm]=a[i].second,val[dm]=a[i].first,add(list[dm],list[dm-1]);
list[++dm]=last;val[dm]=a[2].first;add(list[dm],list[dm-1]);
int pos=3;
for(int i=2;i<=dm-1;i++)
for(int j=1;j<=val[i]-2;j++)
{
if(a[pos].first==1)add(list[i],a[pos].second),pos++;//printf("pos %d\n",pos);
if(a[pos].first>1)break;
}
if(a[pos].first==1){
printf("NO\n");
return 0;
}
int cnt=0;
printf("YES %d\n",dm-1);
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)if(e[i][j])cnt++;
printf("%d\n",cnt);
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)if(e[i][j])printf("%d %d\n",i,j);
return 0;
}

总结###

虽然构造题做的少(基本没做过),但是这道题我觉得还是比较简单,多多画图,就知道咋做了。

[CF1082D]Maximum Diameter Graph的更多相关文章

  1. cf1082D Maximum Diameter Graph(构造+模拟+细节)

    QWQ不得不说 \(cf\)的\(edu\ round\)出这种东西 有点太恶心了 题目大意:给你\(n\)个点,告诉你每个点的最大度数值(也就是说你的度数要小于等于这个),让你构造一个无向图,使其满 ...

  2. Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph

    D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...

  3. Educational Codeforces Round 55 (Rated for Div. 2) D. Maximum Diameter Graph (构造图)

    D. Maximum Diameter Graph time limit per test2 seconds memory limit per test256 megabytes inputstand ...

  4. CF1082D:Maximum Diameter Graph (简单构造)

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

  5. D. Maximum Diameter Graph 贪心+图论+模拟

    题意:给出n个点的度数列 上限(实际点可以小于该度数列)问可以构造简单路最大长度是多少(n个点要连通 不能有平行边.重边) 思路:直接构造一条长链  先把度数为1的点 和度数大于1的点分开  先把度数 ...

  6. Codeforces 1082D Maximum Diameter Graph (贪心构造)

    <题目链接> 题目大意:给你一些点的最大度数,让你构造一张图,使得该图的直径最长,输出对应直径以及所有的边. 解题分析:一道比较暴力的构造题,首先,我们贪心的想,要使图的直径最长,肯定是尽 ...

  7. CodeForces 1082 D Maximum Diameter Graph

    题目传送门 题意:现在有n个点,每个点的度数最大为di,现在要求你构成一棵树,求直径最长. 题解:把所有度数为2的点先扣出来,这些就是这颗树的主干,也就是最长的距离. 然后我们把度数为2的点连起来,之 ...

  8. Codeforces 1082 D. Maximum Diameter Graph-树的直径-最长链-构造题 (Educational Codeforces Round 55 (Rated for Div. 2))

    D. Maximum Diameter Graph time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  9. CF1082解题报告

    CF1082A Vasya and Book 模拟一下即可 \(Code\ Below:\) #include <bits/stdc++.h> using namespace std; c ...

随机推荐

  1. Dockerfile编写

    Dockerfile 是一个文本文件,其内包含了一条条的指令,每一条指令构建一层,因此每一条指令的内容,就是描述该层应当如何构建编写命令: 1.FROM作用:声明使用哪个基础镜像格式:FROM IMA ...

  2. Maven简介与使用

    Maven介绍 Maven是项目管理工具,对软件项目提供构建与依赖管理 Mavan是Apache下的Java开源项目 Mavan为Java项目提供了统一的管理方式,已成为业界标准 Maven核心特性 ...

  3. navicat连接不上Linux服务器上的mysql的解决办法

    一开始,心情是沉痛的,截图如下: 转载请注明出处:https://www.cnblogs.com/NaughtyCat/p/how-to-connect-to-mysql-on-linux-by-na ...

  4. chunked

    简介 chunked是一种HTTP协议的分块传输编码的机制,即一个HTTP消息可以分成多个部分进行传输,它对于HTTP请求和HTTP响应都适用.对于非持续连接,浏览器通过连接是否关闭来界定请求和响应实 ...

  5. nginx(4)

    目录 一.安装配置 1.安装 2.配置文件 3.测试和启动 二.功能 1.虚拟主机 1.1 基于IP 1.2 基于域名 1.3 基于端口 2.访问控制 3.用户认证 4.文件共享 5.文件别名 6.状 ...

  6. ceph安装笔记

    配置源 ceph版本为luminous [root@ceph-node1 ~]# yum install -y https://dl.fedoraproject.org/pub/epel/epel-r ...

  7. Django中获取参数(路径,查询,请求头,请求体)

    一.通常HTTP协议向服务器传参有几种途径 : 提取URL的特定部分,如/weather/shanghai/2018,可以在服务器端的路由中用正则表达式截取: 查询字符串(query string), ...

  8. atom 在Ubuntu 18.04 上安装及基本使用

    前记: Atom 是github专门为程序员推出的一个跨平台文本编辑器.具有简洁和直观的图形用户界面,并有很多有趣的特点:支持CSS,HTML,JavaScript等网页编程语言.它支持宏,自动完成分 ...

  9. Eureka工作原理及它和ZooKeeper的区别

    1.Eureka 简介: Eureka 是 Netflix 出品的用于实现服务注册和发现的工具. Spring Cloud 集成了 Eureka,并提供了开箱即用的支持.其中, Eureka 又可细分 ...

  10. Oracle数据库的sql语句性能优化

    在应用系统开发初期,由于开发数据库数据比较少,对于查询sql语句,复杂试图的编写等体会不出sql语句各种写法的性能优劣,但是如果将应用系统提交实际应用后,随着数据库中数据的增加,系统的响应速度就成为目 ...