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 ...
随机推荐
- WPF 分辨率无关性原理
WPF在计算窗口尺寸大小时使用的是系统的DPI设置.WPF窗口以及窗口中所有的元素都是使用设备无关单位度量.一个设备无关单位被定义为1/96英寸. [物理单位尺寸]=[设备无关单位尺寸]*[系统DPI ...
- JavaMail实现邮件的发送
1,拷贝mail.jar 和activation.jar到项目中 2,开启邮箱的 POP3/SMTP服务,以QQ邮箱为例 进去QQ邮箱-->设置-->账号-->进行设置如下图 注意: ...
- Nginx完整配置配置样例
nginx.conf user www www; ## Default: nobody worker_processes 5; ## Default: 1 error_log logs/error.l ...
- 优雅退出telnet
echo "" |telnet IP 端口
- MySQL和Postgresql的区别
一.PostgreSQL相对于MySQL的优势 1.在SQL的标准实现上要比MySQL完善,而且功能实现比较严谨:2.存储过程的功能支持要比MySQL好,具备本地缓存执行计划的能力:3.对表连接支持较 ...
- RabbitMQ与AMQP
1. 消息队列的历史 了解一件事情的来龙去脉,将不会对它感到神秘.让我们来看看消息队列(Message Queue)这项技术的发展历史. Message Queue的需求由来已久,80年代最早在金融交 ...
- 贿赂囚犯 Bribe the prisoners ( 动态规划+剪枝)
一个监狱里有P个并排着的牢房,从左往右一次编号为1,2,-,P.最初所有牢房里面都住着一个囚犯.现在要释放一些囚犯.如果释放某个牢房里的囚犯,必须要贿赂两边所有的囚犯一个金币,直到监狱的两端或者空牢房 ...
- 【shell】shell中各种括号的作用()、(())、[]、[[]]、{}
一.小括号,圆括号() 1.单小括号 () ①命令组.括号中的命令将会新开一个子shell顺序执行,所以括号中的变量不能够被脚本余下的部分使用.括号中多个命令之间用分号隔开,最后一个命令可以没有 ...
- [bzoj1070] 修车
这周学习了费用流,就写了几题.其中有一题就是bzoj上的修车,看起来很丧,交了6次都是除了样例全wa(事实证明样例说明不了什么,还会误导你……). 题目大意:有m个技术人员n辆车,一个技术人员只能同时 ...
- 自己动手实现arm函数栈帧回溯【转】
转自:http://blog.csdn.net/dragon101788/article/details/18668505 内核版本:2.6.14 glibc版本:2.3.6 CPU平台:arm gl ...