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
inputstandard input
outputstandard 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 n integers a1,a2,…,an 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. 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 (3≤n≤500) — the number of vertices in the graph.
The second line contains n integers a1,a2,…,an (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 — the number of edges in the resulting graph.
The i-th of the next m lines should contain two integers vi,ui (1≤vi,ui≤n, vi≠ui) — 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).
Examples
inputCopy
3
2 2 2
outputCopy
YES 2
2
1 2
2 3
inputCopy
5
1 4 1 1 1
outputCopy
YES 2
4
1 2
3 2
4 2
5 2
inputCopy
3
1 1 1
outputCopy
NO
Note
Here are the graphs for the first two example cases. Both have diameter of 2.
d1=1≤a1=2
d2=2≤a2=2
d3=1≤a3=2
d1=1≤a1=1
d2=4≤a2=4
d3=1≤a3=1
d4=1≤a4=1
题意:
给你一个数组a[i] ,代表节点i的入度上限值,让你构造一个符合数组a的图,要求图的直径最大(任意两个节点的最短 路中的最大值是图的直径),不能合法的联通图,就输出no,否则输出yes和图的直径,还有你构建图的边。
思路:
分析我们知道,如果a[i] 的sum和 小于 2*n-2 是无法构建出一个连通图的。直接输出no
先根据节点的入度上限降序来对节点排序,把a[i]>=2 的节点,都连成一条线,如果还剩a[i] = 1 的节点,选择2个扔到刚刚构建的线的首尾(如果就一个的话,就扔一个到首或者尾部即
可),如果还剩就找入度还有剩余的节点随便加入(对图的直径已经没影响了。) 则图的直径就是那条线的长度。(官方题解用竹子来描述要构造出的图,我觉得还是很恰当的。)
画个图就理解了。
黄色圈的是竹子的主干(也就是影响直径的部分) 以外的点是竹子的叶子,不影响直径。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
struct node
{
int id;
int num;
} a[maxn];
bool cmp(node aa , node bb)
{
return aa.num > bb.num;
}
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\code_stream\\out.txt","w",stdout);
int n;
gbtb;
cin >> n;
ll sum = 0ll;
repd(i, 1, n)
{
cin >> a[i].num;
a[i].id = i;
sum += a[i].num;
}
if (sum < 2ll * n - 2ll)
{
return 0 * puts("NO");
}
sort(a + 1, a + 1 + n, cmp);
int ans = 0;
std::vector<pii> v;
int j = -1;
repd(i, 1, n - 1)
{
if (a[i].num >= 2)
{
ans++;
v.push_back(mp(a[i].id, a[i+1].id));
} else
{
j = i+1;
break;
}
}
int id = 1;
if (j != -1)
{
v.push_back(mp(a[1].id, a[j].id));
ans++;
// a[1].num--;
repd(i, j + 1, n)
{
if (a[id].num > 2)
{
a[id].num--;
v.push_back(mp(a[id].id, a[i].id));
} else
{
id++;
if (a[id].num > 2)
{
a[id].num--;
v.push_back(mp(a[id].id, a[i].id));
}
}
}
}
cout<<"YES "<<ans<<endl;
cout<<sz(v)<<endl;
for(auto x:v)
{
cout<<x.fi<<" "<<x.se<<endl;
}
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
Educational Codeforces Round 55 (Rated for Div. 2) D. Maximum Diameter Graph (构造图)的更多相关文章
- 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) C. Multi-Subject Competition 【vector 预处理优化】
传送门:http://codeforces.com/contest/1082/problem/C C. Multi-Subject Competition time limit per test 2 ...
- 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-& ...
- Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies 【贪心 】
传送门:http://codeforces.com/contest/1082/problem/B B. Vova and Trophies time limit per test 2 seconds ...
- 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 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 ...
- Educational Codeforces Round 55 (Rated for Div. 2):E. Increasing Frequency
E. Increasing Frequency 题目链接:https://codeforces.com/contest/1082/problem/E 题意: 给出n个数以及一个c,现在可以对一个区间上 ...
- Educational Codeforces Round 55 (Rated for Div. 2):C. Multi-Subject Competition
C. Multi-Subject Competition 题目链接:https://codeforces.com/contest/1082/problem/C 题意: 给出n个信息,每个信息包含专业编 ...
- Educational Codeforces Round 55 (Rated for Div. 2)E
题:https://codeforces.com/contest/1082/problem/E 题意:给出n个数和一个数c,只能操作一次将[L,R]之间的数+任意数,问最后该序列中能存在最多多少个c ...
随机推荐
- Java 实现日期 Date 的赋值
关键的语句也就三句话: (1) SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd"); (2) Date ...
- Zookeeper集群及配置
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- eclipse导入工程
一般项目配置信息完全可直接导入,即import 如果缺失.project等文件,eclipse无法识别,则将工程拷贝到工作空间目录下,在eclipse中新建一个同名工程即可
- 原生JS去重
方式一: function deleteRepetionChar(arr){ //先判断输入进来的是数组对象还是字符串 if( typeof arr == "object"){ v ...
- linux(centOS7)的基本操作(一) 概述
linux服务器的连接 1.连接 window环境下需要安装XShell.XFtp等软件,暂时不表: macOS环境下直接用ssh命令登录即可,用以下任意一种 ssh [-p port] userna ...
- 阶段3 1.Mybatis_02.Mybatis入门案例_1.mybatis的入门
H:\BaiDu\黑马传智JavaEE57期 2019最新基础+就业+在职加薪\讲义+笔记+资料\主流框架\31.会员版(2.0)-就业课(2.0)-Mybatis\mybatis\mybatis_d ...
- 【疑难杂症】Firefox 火狐浏览器 关闭到 http://detectportal.firefox.com 的流量
日期:2019-07-18 00:02:58 作者:Bay0net 介绍: 0x01. 问题描述 火狐浏览器的时候,抓包会出现很多这样的数据包.  具体的 URL http://detectport ...
- oracle-SYSTEM表空间的备份与恢复
oracle-SYSTEM表空间的备份与恢复 这一篇在介绍备份及恢复数据文件的方法时,以备份和重做日志(包括归档日志和在线日志)没有丢失为前提 所谓关键数据文件:system表空间的数据文件与参数un ...
- 别把&和nohup混为一谈, 根本不是同一个东西好不好 ------ 聊聊./a.out & , nohut ./a.out , nohup ./a.out &的区别
在第一家公司工作的时候, 我认识了&,在第二家公司工作的时候, 我认识了nohup, 这就是渊源. 随后, 我就一直糊涂用他们, 但并不懂这两个东西. 网上很多地方是乱扯, 瞎复制, 为什 ...
- xmake v2.2.9 发布, 新增c++20 modules的实验性支持
这个版本没啥太大新特性,主要对c++20 modules进行了实验性支持,目前支持clang/msvc编译器,除此之外改进了不少使用体验,并且提高了一些稳定性. 另外,这个版本新增了socket.io ...