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 ...
随机推荐
- [技巧篇]05.关于eclipse模版
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templa ...
- AndroidStudio下加入百度地图的使用(一)——环境搭建
AndroidStudio下加入百度地图的使用(一)--环境搭建 最近有学生要做毕业设计,会使用到定位及地图信息的功能,特此研究了一下,供大家参考,百度定位SDK已经更新到了5.0,地图SDK已经更新 ...
- ZooKeeper开发者指南(五)
引言 这个文档是为了想利用ZooKeeper的协调服务来创建分布式应用的开发者提供的指南.它包括概念和实践的信息. 这个文档的一开始的的四部分呈现了不同ZooKeeper高级概念的的讨论.理解Zook ...
- 在linux下创建软链接(即目录映射)
在linux中创建软链接,使用命令:ln -s. 语法:ln -s 源文件 目标文件.
- UVA 1645 Count
https://vjudge.net/problem/UVA-1645 题意:有多少个n个节点的有根树,每个深度中所有节点的子节点数相同 dp[i] 节点数为i时的答案 除去根节点还有i-1个点,如果 ...
- 鸽巢排序Pigeonhole sort
原理类似桶排序,同样需要一个很大的鸽巢[桶排序里管这个叫桶,名字无所谓了] 鸽巢其实就是数组啦,数组的索引位置就表示值,该索引位置的值表示出现次数,如果全部为1次或0次那就是桶排序 例如 var pi ...
- mysql 创建视图
1.单表创建视图 例如:创建一个选择语句,选出学生的编号,姓名和考号 //创建一个视图名字为stu_view1选择 来自数据表student中的id,name 和kn 中的数据 create view ...
- 【HNOI】矩阵染色 数论
[题目描述]一个2*i的矩阵,一共有m种颜色,相邻两个格子颜色不能相同,m种颜色不必都用上,f[i]表示这个答案,求Σf[i]*(2*i)^m (1<=i<=n)%p. [数据范围] 20 ...
- monkey测试===Monkey测试结果分析(系列三)转
Monkey测试结果分析 一. 初步分析方法: Monkey测试出现错误后,一般的差错步骤为以下几步: 1. 找到是monkey里面的哪个地方出错 2. 查看Monkey里面出错前的一些事件动作,并手 ...
- 网络知识===wireshark抓包,三次握手分析
TCP需要三次握手建立连接: 网上的三次握手讲解的太复杂抽象,尝试着使用wireshark抓包分析,得到如下数据: 整个过程分析如下: step1 client给server发送:[SYN] Seq ...