Codeforces 1082D Maximum Diameter Graph (贪心构造)
<题目链接>
题目大意:
给你一些点的最大度数,让你构造一张图,使得该图的直径最长,输出对应直径以及所有的边。
解题分析:
一道比较暴力的构造题,首先,我们贪心的想,要使图的直径最长,肯定是尽可能的将所有的顶点连在同一条链上,并且,所有度数为1的点都只能作为最外围的点。所以,基本思想就是先将两个度为1的顶点放在链的两端(如果有的话),然后所有度>=2的点放在链的中间,建好链之后,再将多余的度为1的点挂在链上最大度数未满的点上。
#include <bits/stdc++.h>
using namespace std; const int N = ;
int n,ind[N];
struct Node{
int ind,loc;
bool operator < (const Node &tmp){
return ind>tmp.ind;
}
};
vector<Node>vec1,vec; int main(){
scanf("%d",&n); for(int i=;i<=n;i++){
int numd;scanf("%d",&numd);
if(numd==)vec1.push_back(Node{numd,i});
if(numd>)vec.push_back(Node{numd,i});
}
if(vec1.size()<=){
if(vec1.size()==){
printf("YES %d\n",vec.size()-);
printf("%d\n",vec.size()-);
int u=vec[].loc;
for(int i=;i<vec.size();i++){
int v=vec[i].loc;printf("%d %d\n",u,v);
u=v;
}
}else {
if(vec1.size()==){
printf("YES %d\n",vec.size());
printf("%d\n",vec.size());
int u=vec1[].loc;
for(int i=;i<vec.size();i++){
int v=vec[i].loc;printf("%d %d\n",u,v);
u=v;
}
}else if(vec1.size()==){
printf("YES %d\n",vec.size()+);
printf("%d\n",vec.size()+);
int u=vec1[].loc;
for(int i=;i<vec.size();i++){
int v=vec[i].loc;printf("%d %d\n",u,v);
u=v;
}
printf("%d %d\n",vec[vec.size()-].loc,vec1[].loc);
}
}
}else { //如果度为1的点多于2个
int sum=;
for(int i=;i<vec.size();i++){
vec[i].ind-=;
sum+=vec[i].ind;
}
if(sum<vec1.size()-)return puts("NO"),;
printf("YES %d\n",vec.size()+);
printf("%d\n",vec.size()++vec1.size()-);
int u=vec1[].loc;
for(int i=;i<vec.size();i++){
int v=vec[i].loc;printf("%d %d\n",u,v);
u=v;
}
printf("%d %d\n",vec[vec.size()-].loc,vec1[].loc);
sort(vec.begin(),vec.end());
int pos=;
for(int i=;i<vec1.size();i++){
vec[pos].ind--;
printf("%d %d\n",vec1[i].loc,vec[pos].loc);
if(vec[pos].ind==)pos++;
}
}
}
Codeforces 1082D Maximum Diameter Graph (贪心构造)的更多相关文章
- D. Maximum Diameter Graph 贪心+图论+模拟
题意:给出n个点的度数列 上限(实际点可以小于该度数列)问可以构造简单路最大长度是多少(n个点要连通 不能有平行边.重边) 思路:直接构造一条长链 先把度数为1的点 和度数大于1的点分开 先把度数 ...
- cf1082D Maximum Diameter Graph(构造+模拟+细节)
QWQ不得不说 \(cf\)的\(edu\ round\)出这种东西 有点太恶心了 题目大意:给你\(n\)个点,告诉你每个点的最大度数值(也就是说你的度数要小于等于这个),让你构造一个无向图,使其满 ...
- 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 ...
- CF1082D:Maximum Diameter Graph (简单构造)
Graph constructive problems are back! This time the graph you are asked to build should match the fo ...
- CodeForces 1082 D Maximum Diameter Graph
题目传送门 题意:现在有n个点,每个点的度数最大为di,现在要求你构成一棵树,求直径最长. 题解:把所有度数为2的点先扣出来,这些就是这颗树的主干,也就是最长的距离. 然后我们把度数为2的点连起来,之 ...
- [CF1082D]Maximum Diameter Graph
题目描述 Description Graph constructive problems are back! This time the graph you are asked to build sh ...
- CodeForces - 459E Pashmak and Graph[贪心优化dp]
E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Educational Codeforces Round 20 C 数学/贪心/构造
C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...
随机推荐
- 10分钟了解Android的事件分发
什么是事件分发? 大家知道Android中的视图是由一个个View嵌套构成的层级视图,即一个View里包含有子View,而这个子View里面又可以再添加View.当用户触摸屏幕产生一系列事件时,事件会 ...
- ant 相关命令
# jmeter-ant A Simple Ant project for JMeter Performance Test # Pre-Requisite* Java 1.7 or above* JM ...
- IOS 将状态栏改为白色
1.将 View controller-based status bar appearance 删除(默认为 YES),或设置为YES 2.设置rootViewcontroller,如果为viewC ...
- FlashBack 闪回
[学习目标] Flashback Database 功能非常类似与RMAN的不完全恢复,它可以把整个数据库回退到 过去的某个时点的状态,这个功能依赖于Flashback log日志.比RMAN 更快速 ...
- Allegro PCB Design GXL (legacy) 设置十字大光标
Allegro PCB Design GXL (legacy) version 16.6-2015 1.菜单:Setup > User Preferences... 2.User Prefere ...
- eclipse打Jar包问题
1.首先,如果你的Java项目中没有任何第三方包,是十分容易的,只需要通过eclipse的Export就可以按操作一步步运行,如下: 选择你要导出的Java项目,右键选择Export,如下图,选择JA ...
- 调整Windows操作系统下时间同步的频率
今天发现时间不对,同步时间后看到Windows系统默认是一周才同步一次时间,频率太低了.查找了一下资料,找到了两种调整Win7时间同步频率的方法. 方法一:注册表法 这种方法是通过修改注册表中的键值来 ...
- 494. Target Sum
You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...
- .net core 发布后提示Start error
纪录篇: 发布Core版本的项目后一直提示error,通过网络查询资料后确认梳理问题的逻辑 1.验证环境是否支持,开发环境及server环境 参考:https://docs.micr ...
- vue-all
http://v1-cn.vuejs.org/guide/ [1]. vue-cli [项目不完整,跳过]https://github.com/vuejs/vue-cli vue-cli-master ...