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 ...
随机推荐
- 后端拜拜,用不到你了,前端开发数据模拟神器nodejs
后端拜拜,用不到你了,前端开发数据模拟神器nodejs 欢迎关注博主公众号「java大师」, 专注于分享Java领域干货文章, 关注回复「资源」, 免费领取全网最热的Java架构师学习PDF, 转载请 ...
- Spring Boot 入门系列(二十五)读取配置文件的几种方式详解!
在项目开发中经常会用到配置文件,之前介绍过Spring Boot 资源文件属性配置的方法,但是很多朋友反馈说介绍的不够详细全面.所以, 今天完整的分享Spring Boot读取配置文件的几种方式! S ...
- python 回归分析
一.线性回归 1 绘制散点图 import matplotlib.pyplot as plt x = [5,7,8,7,2,17,2,9,4,11,12,9,6] y = [99,86,87,88,1 ...
- Walker
emmm.......随机化. 好吧,我们不熟. 考虑随机选取两组数据高斯消元消除结果后带入检验,能有超过1/2正确就输出. 其实方程就四个,手动解都没问题. 只是要注意看sin与 ...
- IKEv2协议关键知识点总结整理
文章目录 @[toc] 1. IKEv2基本原理 2. IKEv2协议重点注意事项 2.1 情景一:==IKEv2协商密钥逻辑== ①密钥协商流程 ②函数调用关系 ③流程简述 2.2 情景二:==使用 ...
- QT之静态函数发送信号
一.简介 由于博主本人是初学者对QT的机制不了解,所以遇到了一个比较大的坑,特此记录一下.我遇到的问题是无法在静态函数中向另外一个类发送信号.解决办法:先将信号发送给同类中的普通函数,然后在从普通函数 ...
- 传递集合参数以及SpringMVC和Struts2的区别
一.传递集合参数 二.和Struts2的区别 Struts2是基于类封装请求参数,SpringMVC是基于方法封装参数:
- freeswitch编译安装依赖
ncurses:提供字符界面 zlib:数据压缩 libjpeg:JPEG图片格式数据的解码/编码/其他. lua:lua解释器 libedit:一种编辑操作的库,对一些可以交互操作的场景,或转为了自 ...
- Django学习day15BBS项目开发4.0(完结)
每日测验 """ 今日考题: 1.简述自定义标签,过滤器,inclusion_tag的方法,并简要说一说三者的特点及响应流程 2.简述个人侧边栏展示及筛选业务逻辑 3.简 ...
- 学习PHP中好玩的Gmagick图像操作扩展的使用
在 PHP 的图像处理领域,要说最出名的 GD 库为什么好,那就是因为它不需要额外安装的别的什么图像处理工具,而且是随 PHP 源码一起发布的,只需要在安装 PHP 的时候添加上编译参数就可以了. G ...