题目传送门

题意:现在有n个点,每个点的度数最大为di,现在要求你构成一棵树,求直径最长。

题解:把所有度数为2的点先扣出来,这些就是这颗树的主干,也就是最长的距离。

然后我们把度数为2的点连起来,之后就处理1的点,先在主干的最左边和最右边加上新的点,这样可以使得直径边长。

然后其他的点随便放就好了。

代码:

#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL mod = (int)1e9+;
const int N = 1e5 + ;
pll p[N];
int d[N];
vector<int> vc[];
vector<pll> ans;
int main(){
int n;
scanf("%d", &n);
for(int i = ; i <= n; ++i){
scanf("%d", &d[i]);
if(d[i] > ) vc[].pb(i);
else vc[].pb(i);
}
if(vc[].size() == ){
puts("NO");
return ;
}
int fans = ;
for(int i = ; i < vc[].size(); ++i){
ans.pb({vc[][i], vc[][i-]});
--d[vc[][i]], --d[vc[][i-]];
++fans;
}
int p = ;
for(int j = ; j < vc[].size(); ++j){
if(j == ){
if(d[vc[][]]){
--d[vc[][]];
++fans;
ans.pb({vc[][j], vc[][]});
}
}
else if(j == ){
if(d[vc[][vc[].size()-]]){
--d[vc[][vc[].size()-]];
++fans;
ans.pb({vc[][j], vc[][vc[].size()-]});
}
}
else {
while(p < vc[].size() && d[vc[][p]] == ) ++p;
if(p == vc[].size()) break;
--d[vc[][p]];
ans.pb({vc[][j], vc[][p]});
}
}
if(p == vc[].size()) puts("NO");
else {
printf("YES %d\n", fans);
printf("%d\n", ans.size());
for(int i = ; i < ans.size(); ++i){
printf("%d %d\n", ans[i].fi, ans[i].se);
}
}
return ;
}

CodeForces 1082 D Maximum Diameter Graph的更多相关文章

  1. 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 ...

  2. Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph

    D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...

  3. 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 ...

  4. Codeforces 1082D Maximum Diameter Graph (贪心构造)

    <题目链接> 题目大意:给你一些点的最大度数,让你构造一张图,使得该图的直径最长,输出对应直径以及所有的边. 解题分析:一道比较暴力的构造题,首先,我们贪心的想,要使图的直径最长,肯定是尽 ...

  5. CF1082D:Maximum Diameter Graph (简单构造)

    Graph constructive problems are back! This time the graph you are asked to build should match the fo ...

  6. [CF1082D]Maximum Diameter Graph

    题目描述 Description Graph constructive problems are back! This time the graph you are asked to build sh ...

  7. Codeforces 1082 G - Petya and Graph

    G - Petya and Graph 思路: 最大权闭合子图 对于每条边,如果它选了,那么它连的的两个点也要选 边权为正,点权为负,那么就是求最大权闭合子图 代码: #pragma GCC opti ...

  8. D. Maximum Diameter Graph 贪心+图论+模拟

    题意:给出n个点的度数列 上限(实际点可以小于该度数列)问可以构造简单路最大长度是多少(n个点要连通 不能有平行边.重边) 思路:直接构造一条长链  先把度数为1的点 和度数大于1的点分开  先把度数 ...

  9. CodeForces 1082 G Petya and Graph 最大权闭合子图。

    题目传送门 题意:现在有一个图,选择一条边,会把边的2个顶点也选起来,最后会的到一个边的集合 和一个点的集合 , 求边的集合 - 点的集合最大是多少. 题解:裸的最大权闭合子图. 代码: #inclu ...

随机推荐

  1. .net持续集成测试篇之Nunit that断言

    系列目录 that是Nunit的新语法,语义上不如简单断言,使用上也更加复杂,但是其功能更加强大. 其基本语法如下代码片段示: [Test] public void DemoTest() { bool ...

  2. solidity智能合约字节数最大值及缩减字节数

    智能合约最大字节数 在Solidity中,EIP 170将contract的最大大小限制为24 KB .因此,如果智能合约内容过多,会导致无法进行发布操作. 减少压缩字节数方法 方法及变量命名 在一定 ...

  3. kubernetes集群升级的正确姿势

    kubernetes社区非常活跃,每季度都会发布一个release.但是线上集群业务可用性要求较高,场景复杂,任何微小的变更都需要非常小心,此时跟随社区版本进行升级略显吃力.但是为了能够使用到最新的一 ...

  4. Spring Boot 2.X 如何快速整合jpa?

    本文目录 一.JPA介绍二.Spring Data JPA类结构图1.类的结构关系图三.代码实现1.添加对应的Starter2.添加连接数据库的配置3.主要代码 一.JPA介绍 JPA是Java Pe ...

  5. 用 程序 解决 windows防火墙 的 弹窗 问题

    今天用户反馈了一个问题,运行程序弹了个框 这个只有程序第一次运行会出来,之后就不会了. 当然改个程序名字,又会弹出来. 强烈怀疑是写到了注册表,果然被我找到了. “HKEY_LOCAL_MACHINE ...

  6. flink 1.7.2 安装详解

    ##flink 1.7.2 安装需要java环境 下载地址 https://flink.apache.org/downloads.html#1.单机版 #创建用户flinkuseradd flink ...

  7. Python 面向導向語言 Object Oriented Programming Language

    Pytho 是面向對象的程式語言,舉凡 Literals 值都是 Object.例如: >>> id(38)8791423739696 與 >>> id('ABC' ...

  8. 熔断器Hystrix

    什么是服务雪崩? 单个服务发生故障,占用过多的系统资源,从而导致级联故障的情况称为服务雪崩. 什么是Hystrix? 在分布式环境中,许多服务依赖项中的一些必然会失败.(服务挂了) Hystrix是一 ...

  9. EL表达式forEach中索引获取

    有的时候,不得不使用循环中的索引,比如label对应的单选多选: <c:forEach items="${lpalls }" var="pall" var ...

  10. idea(java)实用开发插件

    Idea常用的插件: mybatisX,      ----------------  (Alt + enter) codeGlace, Lombok, sonarlint, translation, ...