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 ...
随机推荐
- 使用Eclipse进行Makefile项目
最近在MCU on Eclipse网站上看到Erich Styger所写的一篇有关在Eclipse中使用Makefile创建项目的文章,文章讲解清晰明了非常不错,所以呢没人将其翻译过来供各位同仁参考. ...
- maven install 报错 No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
1.控制台打印信息 [INFO] Scanning for projects... [INFO] [INFO] ---------------------< org.cqupt.mauger:R ...
- PHP7运行环境搭建(Windows7)
注:本文来源于< PHP7运行环境搭建(Windows7) > php7号称能直追facebook的HHVM,为了体验一把传说中的高性能,我特意在本地电脑上尝试着安装了php7, ...
- Confluence 6 数据库支持的驱动
数据库 驱动已捆绑? JDBC 驱动 备注 更多信息 PostgreSQL 9.4-1202 JDBC 41 driver download 我们推荐你使用 JDBC 4 的驱动. 如果你希望使用更新 ...
- 用D3.js画的人物关系demo
代码下载地址:https://github.com/zhangzn3/group-explorer ### Demo1功能 *** * 支持节点拖拽 * 支持节点拖拽并固定位置 * 支持鼠标浮到节点显 ...
- Matplotlib模块:绘图和可视化
一.简单介绍Matplotlib 1.Matplotlib是一个强大的Python绘图和数据可视化的工具包 2.安装方法:pip install matplotlib 3.引用方法:import ma ...
- 【scapy】读取pcap
scapy读取pcap包 假设有pcap包test.pcap,读取其中的分层流量信息 代码: import scapy_http.http try: import scapy.all as scapy ...
- 从xtrabackup备份恢复单表
目前对MySQL比较流行的备份方式有两种,一种上是使用自带的mysqldump,另一种是xtrabackup,对于数据时大的环境,普遍使用了xtrabackup+binlog进行全量或者增量备份,那么 ...
- HTTP 599: SSL certificate problem: unable to get local issuer certificate错误
自己在用 PySpider 框架爬虫运行代码后时出现 HTTP 599: SSL certificate problem: unable to get local issuer certificate ...
- gotty---用来作为k8s的web terminal,通过参数读取指定pod的日志输出
不要重复造轮子,我觉得这个方案比较适合现在的情况. 我们知道,如果手工查看k8s里指定pod的日志输出,一般的方案如下: kubectl logs -f -n [namespace] [pod] -c ...