题目传送门

题意:现在有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. C#中属性的解析

    一.域的概念 C#中域是指成员变量和方法,在OOP编程中(面向对象编程)我们要求用户只知道类是干什么的,而不许知道如何完成的,或者说不允许访问类的内部,对于有必要在类外可见的域,我们用属性来表达,所以 ...

  2. 使用python画2D线条

    """用于验证整体趋势正确性,不做关闭操作时保持显示""" #!python3 #-*- coding:utf-8 -*- import m ...

  3. 算法与数据结构基础 - 合并查找(Union Find)

    Union Find算法基础 Union Find算法用于处理集合的合并和查询问题,其定义了两个用于并查集的操作: Find: 确定元素属于哪一个子集,或判断两个元素是否属于同一子集 Union: 将 ...

  4. jQuery中的append中含有onClick的问题

    在jQuery中,当append中含有onClick时,点击事件无效果.需要在append完之后再额外绑定点击事件.

  5. 减谈迷宫C++

    今天老师让做了个迷宫问题,我一看到就发现和我之前写过的一个程序是一样 的,但是在后来编写的时候有一个地方搞错了,最后下课了我还是没有正确的编写好,然后今天回来之后自己有看了一下,现在已经解决了. #i ...

  6. Unity进阶之ET网络游戏开发框架 06-游客登录

    版权申明: 本文原创首发于以下网站: 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123 优梦创客的官方博客:https://91make.top ...

  7. python paramiko外部传参和内部调用命令的方法

    学习了很久的python,但在工作中使用的时候,却发现不知道怎么传参进入到python中执行,所以这两天就研究 了python args怎么将外部参数传入到python中执行 1.首先使用python ...

  8. Linux x86和x64的区别

    0x01:寄存器分配的不同 (1)64位有16个寄存器,32位只有8个.但是32位前8个都有不同的命名,分别是e _ ,而64位前8个使用了r代替e,也就是r _.e开头的寄存器命名依然可以直接运用于 ...

  9. Oracle数据库之SQLPLUS

    三.SQLPLUS ​ SQLPlus 是 Oracle 数据库提供的一个专门用于数据库管理的交互式工具,使用 SQLPlus 可以管理 Oracle 数据库的所有任务,SQLPlus 通过命令的方式 ...

  10. 良许Linux | Linux学习方法及学习资料汇总

    很多人想学习Linux,却不知道怎么着手,甚至不知道Linux有哪些方向,非常迷茫.基于此,我特地写了篇文章介绍Linux方向性问题,没想到一不小心成了爆款: 到什么程度才叫精通 Linux?​ 看完 ...