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 n n integers a 1 ,a 2 ,…,a n a1,a2,…,an construct a connected undirected graph of n n vertices such that:
- the graph contains no self-loops and no multiple edges;
- the degree d i di of the i i -th vertex doesn't exceed a i ai (i.e. d i ≤a i 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 n (3≤n≤500 3≤n≤500 ) — the number of vertices in the graph.
The second line contains n n integers a 1 ,a 2 ,…,a n a1,a2,…,an (1≤a i ≤n−1 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 m — the number of edges in the resulting graph.
The i i -th of the next m m lines should contain two integers v i ,u i vi,ui (1≤v i ,u i ≤n 1≤vi,ui≤n , v i ≠u i vi≠ui ) — the description of the i i -th edge. The graph should contain no multiple edges — for each pair (x,y) (x,y) you output, you should output no more pairs (x,y) (x,y) or (y,x) (y,x) .
Examples
3
2 2 2
YES 2
2
1 2
2 3
5
1 4 1 1 1
YES 2
4
1 2
3 2
4 2
5 2
3
1 1 1
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=;
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=;
rep(i,,N) scanf("%d",&a[i].F),a[i].S=i;
sort(a+,a+N+);
b[++L]=a[].S; b[++L]=a[].S;
int pre=b[],bg=;
rep(i,,N){
if(a[i].F>){
f[++tot]=pre,c[tot]=a[i].S,pre=a[i].S,ans++;
if(!bg) bg=i;
}
}
f[++tot]=pre,c[tot]=b[];
int pos=bg,F=true;
rep(i,,bg-) {
if(a[i].F==) {
while(a[pos].F<=){
pos++; if(pos==N+) {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-) puts("NO");
else {
printf("YES %d\n%d\n",ans,N-);
rep(i,,tot) printf("%d %d\n",f[i],c[i]);
}
return ; }
CF1082D:Maximum Diameter Graph (简单构造)的更多相关文章
- cf1082D Maximum Diameter Graph(构造+模拟+细节)
QWQ不得不说 \(cf\)的\(edu\ round\)出这种东西 有点太恶心了 题目大意:给你\(n\)个点,告诉你每个点的最大度数值(也就是说你的度数要小于等于这个),让你构造一个无向图,使其满 ...
- [CF1082D]Maximum Diameter Graph
题目描述 Description Graph constructive problems are back! This time the graph you are asked to build sh ...
- Codeforces 1082D Maximum Diameter Graph (贪心构造)
<题目链接> 题目大意:给你一些点的最大度数,让你构造一张图,使得该图的直径最长,输出对应直径以及所有的边. 解题分析:一道比较暴力的构造题,首先,我们贪心的想,要使图的直径最长,肯定是尽 ...
- 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 ...
- Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph
D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...
- D. Maximum Diameter Graph 贪心+图论+模拟
题意:给出n个点的度数列 上限(实际点可以小于该度数列)问可以构造简单路最大长度是多少(n个点要连通 不能有平行边.重边) 思路:直接构造一条长链 先把度数为1的点 和度数大于1的点分开 先把度数 ...
- CodeForces 1082 D Maximum Diameter Graph
题目传送门 题意:现在有n个点,每个点的度数最大为di,现在要求你构成一棵树,求直径最长. 题解:把所有度数为2的点先扣出来,这些就是这颗树的主干,也就是最长的距离. 然后我们把度数为2的点连起来,之 ...
- 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 ...
- CF1157D N Problems During K Days(简单构造)
题目 题目 原数据是水成啥样了,\(<\longrightarrow <=,>=\longrightarrow <=,\)这也能过 被\(hack\)后身败名裂 做法 简单的贪 ...
随机推荐
- linux进程间通讯的几种方式的特点和优缺点
# 管道( pipe ):管道是一种半双工的通信方式,数据只能单向流动,而且只能在具有亲缘关系的进程间使用.进程的亲缘关系通常是指父子进程关系.# 有名管道 (named pipe) : 有名管道也是 ...
- (转) bicabo Visual Studio 2012自动添加注释(如版权信息等)
如何使用Visual Studio 2012给程序文件的头部自动添加如下的注释? /********************************************************** ...
- Python:笔记(7)——yield关键字
Python:笔记(7)——yield关键字 yield与生成器 所谓生成器是一个函数,它可以生成一个值的序列,以便在迭代中使用.函数使用yield关键字可以定义生成器对象. 一个例子 我们调用该函数 ...
- FFmpeg 入门(6):音频同步
本文转自:FFmpeg 入门(6):音频同步 | www.samirchen.com 音频同步 上一节我们做了将视频同步到音频时钟,这一节我们反过来,将音频同步到视频.首先,我们要实现一个视频时钟来跟 ...
- P1052 过河(离散化+dp)
P1052 过河 dp不难,重点是要想到离散化. 石子个数$<=100$意味着有大量空间空置,我们可以缩掉这些空间. 实现的话自己yy下就差不多了. #include<iostream&g ...
- Python3.x:免费代理ip的批量获取并入库
Python3.x:免费代理ip的批量获取并入库 一.简介 网络爬虫的世界,向来都是一场精彩的攻防战.现在许多网站的反爬虫机制在不断的完善,其中最令人头疼的,莫过于直接封锁你的ip.但是道高一尺魔高一 ...
- ssh-copy-id使用非默认22端口
ssh-copy-id使用及非默认22端口时报错 ssh-copy-id使用介绍 说明:ssh-copy-id命令可以把本地的ssh公钥文件安装到远程主机对应的账户下. 功能:ssh-copy-id ...
- Net Quartz使用
安装Quartz 已经先安装了2.5版本,现在换成2.4 程序包管理器控制台: PM> Install-Package Quartz -Version 2.4 正在尝试收集与目标为“.NETFr ...
- (译)综合指南:通过Ubuntu 16.04上从Source构建来安装支持GPU的Caffe2
(译)综合指南:通过Ubuntu 16.04上从Source构建来安装支持GPU的Caffe2 译者注: 原文来自:https://tech.amikelive.com/node-706/compre ...
- Android下拉刷新控件--PullToRefresh的简单使用
Android中很多时候都会用到上下拉刷新,这是一个很常用的功能,Android的v4包中也为我们提供了一种原生的下拉刷新控件--SwipeRefreshLayout,可以用它实现一个简洁的刷新效果, ...