Codeforces 1082 D. Maximum Diameter Graph-树的直径-最长链-构造题 (Educational Codeforces Round 55 (Rated for Div. 2))
2 seconds
256 megabytes
standard input
standard output
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 nn integers a1,a2,…,ana1,a2,…,an construct a connected undirected graph of nn vertices such that:
- the graph contains no self-loops and no multiple edges;
- the degree didi of the ii-th vertex doesn't exceed aiai (i.e. di≤aidi≤ai);
- the diameter of the graph is maximum possible.
Output the resulting graph or report that no solution exists.
The first line contains a single integer nn (3≤n≤5003≤n≤500) — the number of vertices in the graph.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n−11≤ai≤n−1) — the upper limits to vertex degrees.
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 mm — the number of edges in the resulting graph.
The ii-th of the next mm lines should contain two integers vi,uivi,ui (1≤vi,ui≤n1≤vi,ui≤n, vi≠uivi≠ui) — the description of the ii-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).
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
Here are the graphs for the first two example cases. Both have diameter of 22.
d1=1≤a1=2d1=1≤a1=2
d2=2≤a2=2d2=2≤a2=2
d3=1≤a3=2d3=1≤a3=2
d1=1≤a1=1d1=1≤a1=1
d2=4≤a2=4d2=4≤a2=4
d3=1≤a3=1d3=1≤a3=1
d4=1≤a4=1
这个题无语,自己打比赛的时候看错题,求成最短的了,mdzz。。。
直接构造一条最长链就可以。从大到小连,为1的两边最多能连2个,其他的中间连,然后就可以了。
代码:
//D
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); int n,m,t,p,ans;
int d[maxn*],first[maxn*],v[maxn*],w[maxn*],nextt[maxn*]; struct node{
int p,d; bool operator <(const node &a)const{
return d<a.d;
} }a[maxn]; vector<pair<int,int> > ve; ll pre[maxn]; int main()
{
int n;
cin>>n;
int flag=;
for(int i=;i<=n;i++){
cin>>a[i].d,a[i].p=i;
if(a[i].d!=)flag=;
}
if(flag==) {cout<<"NO"<<endl;return ;}
sort(a+,a++n);
for(int i=;i<=n;i++)
pre[i]=pre[i-]+a[i].d;
int i=n-,con=n,tail=n,len=,flag1=,flag2=;
while(){
if(a[con].d&&a[i].d) {ve.push_back(make_pair(a[con].p,a[i].p));a[con].d--;a[i].d--;con--;i--;len++;}
else if(!a[con].d&&flag1==) {ve.push_back(make_pair(a[tail].p,a[i].p));a[tail].d--;a[i].d--;i--;len++;flag1=;if(a[tail].d==) tail--;}
else{
if(a[tail].d!=) {ve.push_back(make_pair(a[tail].p,a[i].p));a[tail].d--;a[i].d--;i--;if(a[tail].d==) tail--;}
else {flag2=;break;}
}
if(i==) break;
}
if(flag2==) {cout<<"NO"<<endl;return ;}
vector<pair<int,int> >::iterator it;
cout<<"YES "<<len<<endl;
cout<<ve.size()<<endl;
for(it=ve.begin();it!=ve.end();it++){
cout<<(*it).first<<" "<<(*it).second<<endl;
}
}
Codeforces 1082 D. Maximum Diameter Graph-树的直径-最长链-构造题 (Educational Codeforces Round 55 (Rated for Div. 2))的更多相关文章
- CodeForces 1082 D Maximum Diameter Graph
题目传送门 题意:现在有n个点,每个点的度数最大为di,现在要求你构成一棵树,求直径最长. 题解:把所有度数为2的点先扣出来,这些就是这颗树的主干,也就是最长的距离. 然后我们把度数为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个点的最大入度数,要求添加边构成 ...
- 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 题意 给出每个点的最大度,构造直径尽可能长的树 思路 让度数大于$1$的点构成链,考虑是否能在链的两端加度为$1$的点 代码 #include &l ...
- Educational Codeforces Round 55 (Rated for Div. 2) Solution
A. Vasya and Book Solved. 三种方式取$Min$ #include <bits/stdc++.h> using namespace std; #define ll ...
- Educational Codeforces Round 55 (Rated for Div. 2) A/B/C/D
http://codeforces.com/contest/1082/problem/A WA数发,因为默认为x<y = = 分情况讨论,直达 or x->1->y or x-& ...
- Codeforces 1082 C. Multi-Subject Competition-有点意思 (Educational Codeforces Round 55 (Rated for Div. 2))
C. Multi-Subject Competition time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces 1082 B. Vova and Trophies-有坑 (Educational Codeforces Round 55 (Rated for Div. 2))
B. Vova and Trophies time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 1082 A. Vasya and Book-题意 (Educational Codeforces Round 55 (Rated for Div. 2))
A. Vasya and Book time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
随机推荐
- mysql Innodb索引
基本概念 对于mysql目前的默认存储引擎Innodb来说,索引分为2个,一个是聚集索引,一个是普通索引(也叫二级索引). 聚集索引:聚集索引的顺序和数据在磁盘的顺序一致,因此查询时使用聚集索引,效率 ...
- Java中主线程如何捕获子线程抛出的异常
首先明确线程代码的边界.其实很简单,Runnable接口的run方法所界定的边界就可以看作是线程代码的边界.Runnable接口中run方法原型如下: public void run(); 而所有的具 ...
- jQuery对象初始化的传参方式
jQuery对象初始化的传参方式包括: 1.$(DOMElement) 2.$(' ... '), $('#id'), $('.class') 传入字符串, 这是最常见的形式, 这种传参数经常也传入第 ...
- 数据结构:hash_map
在C++11以上的开发环境中,请直接把map替换成unordered_map,不要使用hash_map 之前我们专门有一篇介绍哈希表,多维哈希表的博文,当时就是以map举例子,然后说了一句把map替换 ...
- 【HNOI】trust 弦图最大独立集
[题目描述]有n个人,每个人之间都有是否信任的关系,要求找出k个人,使得k个人之间彼此信任,且k最大,保证不信任的关系由多个三元环组成,且三元环之间只可能有公共点,没有公共边,且不存在任意一个节点不属 ...
- HTML5获取地理位置信息并在Google Maps上显示
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- MySQL当中的case when then
其实就相当于if else:而且也可以用if来替代. case whent 条件1 then 条件2 else 条件3 end; 如果条件1成立就执行条件2否则执行条件3 mysql ) end; + ...
- MFC不同工程(解决方案)之间对话框资源的复制与重用方法(转)
原文转自 https://blog.csdn.net/lihui126/article/details/45556687
- 【Git/GitHub学习笔记】基本操作——创建仓库,本地、远程同步等
近日想分享一些文件,但是用度盘又太麻烦了(速度也很恶心).所以突发奇想去研究了下GitHub的仓库,这篇文章也就是一个最最最基础的基本操作.基本实现了可以在GitHub上存储文本信息与代码. 由于我的 ...
- Linux汇编教程04:寻址方式
这一节,我们主要来讨论寻址方式,这一点十分重要. 我们上一节有稍微提了一下,内存地址引用的通用格式: 地址或偏移(%基址寄存器, %索引寄存器, 比例因子 ) 结果地址 = 地址或偏移 + %基址寄存 ...