cf1082D Maximum Diameter Graph(构造+模拟+细节)
QWQ不得不说 \(cf\)的\(edu\ round\)出这种东西 有点太恶心了
题目大意:给你\(n\)个点,告诉你每个点的最大度数值(也就是说你的度数要小于等于这个),让你构造一个无向图,使其满足直径最大且包含所有点
一看就是个构造题
QWQ但是细节非常多。
大致上的思路就是,我们考虑把度数大于1的点拿出来,然后构成一条链,剩下的往这条链上挂就行,但是挂的时候要注意,优先往最头上的两个点挂,因为这样可以扩大直径,然后搞一搞就好
QWQ
(其实是强行凑博客文章数目)
记得特判一下只有一个度数大于1,或者没有的情况啊!
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#define mk make_pair
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
const int maxn = 2010;
vector<pair<int,int> > ans;
vector<int> v;
int a[maxn];
int n,m;
bool flag=1;
vector<int> v1;
int tmp;
int ymh;
int main()
{
n=read();
for (int i=1;i<=n;i++)
{//
a[i]=read();
if (a[i]>1) v.push_back(i),tmp+=a[i];
}
for (int i=1;i<=n;i++) if (a[i]==1) v1.push_back(i);
if (v.size()==1)
{
int pos=0;
for (int i=1;i<=n;i++) if (a[i]>1) pos=i;
if (tmp>=n-1)
{
cout<<"YES"<<" "<<min(n-1,2)<<endl;
cout<<n-1<<endl;
for (int i=1;i<=n;i++)
{
if (i!=pos)
{
cout<<i<<" "<<pos<<"\n";
}
}
}
else
{
cout<<"NO";
}
return 0;
}
if (v.size()==0)
{
cout<<"NO";
return 0;
}
for (int i=0;i<v.size()-1;i++) ans.push_back(mk(v[i],v[i+1])),a[v[i]]--,a[v[i+1]]--;
ymh = v.size()-1;
if (v1.size()==1)
{
ymh++;
ans.push_back(mk(v[0],v1[0]));
}
int now = 0;
if (v1.size()>=2)
{
ymh+=2;
now = 2;
ans.push_back(mk(v[0],v1[0]));
ans.push_back(mk(v[v.size()-1],v1[1]));
a[v[0]]--;
a[v[v.size()-1]]--;
int i=0;
while (i<v.size() && now<v1.size())
{
while (i<v.size() && a[v[i]]==0) i++;
if (i==v.size()) break;
ans.push_back(mk(v[i],v1[now]));
now++;
a[v[i]]--;
}
}
if (now!=v1.size() && v1.size()>=2)
{
cout<<"NO";
}
else
{
cout<<"YES"<<" "<<ymh<<endl;
cout<<ans.size()<<endl;
for (int i=0;i<ans.size();i++)
{
cout<<ans[i].first<<" "<<ans[i].second<<"\n";
}
}
return 0;
}
cf1082D Maximum Diameter Graph(构造+模拟+细节)的更多相关文章
- [CF1082D]Maximum Diameter Graph
题目描述 Description Graph constructive problems are back! This time the graph you are asked to build sh ...
- 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个点的最大入度数,要求添加边构成 ...
- CF1082D:Maximum Diameter Graph (简单构造)
Graph constructive problems are back! This time the graph you are asked to build should match the fo ...
- D. Maximum Diameter Graph 贪心+图论+模拟
题意:给出n个点的度数列 上限(实际点可以小于该度数列)问可以构造简单路最大长度是多少(n个点要连通 不能有平行边.重边) 思路:直接构造一条长链 先把度数为1的点 和度数大于1的点分开 先把度数 ...
- Codeforces 1082D Maximum Diameter Graph (贪心构造)
<题目链接> 题目大意:给你一些点的最大度数,让你构造一张图,使得该图的直径最长,输出对应直径以及所有的边. 解题分析:一道比较暴力的构造题,首先,我们贪心的想,要使图的直径最长,肯定是尽 ...
- 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 ...
- Codeforces Round #398 (Div. 2) A B C D 模拟 细节 dfs 贪心
A. Snacktower time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
随机推荐
- MySQL 常用的聚合函数
[常用的聚合函数] mysql聚合函数一般用户统计一列值进行计算,然后返回计算结果.一般于分组group by 配合使用. count //统计个数 select count(*) from test ...
- vue 封装 axios 和 各类的请求,以及引入 .vue 文件中使用
//src 底下建立 api 文件夹 // api 文件夹下建立 request,js 文件,文件内容复制下面这段代码即可 /** * ajax请求配置 */ import axios fro ...
- JDK和环境配置,eclipse安装与使用
本博客部分参照https://blog.csdn.net/PGY0000/article/details/79256720 (记住要尊重别人的劳动产品) 原博客给的链接和后面的安装过程有点不一样,不能 ...
- Node.js 应用全链路追踪技术——[全链路信息获取]
全链路追踪技术的两个核心要素分别是 全链路信息获取 和 全链路信息存储展示. Node.js 应用也不例外,这里将分成两篇文章进行介绍:第一篇介绍 Node.js 应用全链路信息获取, 第二篇介绍 N ...
- Java基础(三)——内部类
一.内部类 内部类(Inner Class)就是定义在一个类里面的类.与之对应,包含内部类的类被称为外部类.内部类可以用private修饰. 1.为什么要定义内部类?或者内部类的作用是什么? 内部类提 ...
- Hounter
这题是概率与期望,不是很熟,所以冲了两篇题解才来写总结. 首先可以发现1猎人死的轮数是他之前死了的列人数加一. 那么题目转化为求先于一号猎人死的猎人数的期望值. 考虑这样一个事情,就是 ...
- WEB安全性测试之文件上传漏洞
1.漏洞描述:文件上传漏洞,是指可以利用WEB上传一些特定的文件包含特定代码如(<?php phpnfo;?> 可以用于读取服务器配置信息.上传成功后可以点击) 上传漏洞是指用户上传了一个 ...
- Java == 和 equals的区别
== 是操作符,equals是方法. 对于基本类型变量来说,只能使用 == ,因为基本类型的变量没有方法.使用==比较是值比较. 对于引用类型的变量来说,==比较的两个引用对象的地址是否相等.所有类都 ...
- MobaXterm - 渗透之旅的终端神器
一.背景 1.SSH概念 如果想要连接Linux服务器来进行文件之间的传送,那就需要一个Secure Shell软件(简称SSH的)来完成.从概念上来讲,SSH其实是一个网络协议,允许通过网络连接到L ...
- HCNP Routing&Switching之IS-IS路由聚合和认证
前文我们了解了IS-IS路由渗透和开销相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/15302382.html:今天我们来聊一聊IS-IS路由聚合和认 ...