题面

传送门

分析

贪心

将度限制大于1的点连成一条链,然后将度限制等于1的点挂上去

形状如下图,其中(1,2,3)为度数限制>1的点

显然直径长度=(度数限制>1的节点个数)-1+min(度数限制等于1的节点个数,2)

那么具体如何构造?

首先将度为1和度>1的节点分开

如果有至少1个度为1的节点,就把它作为直径的起点,如图中的4

然后再将度>1的节点全部接上去

再从另一头倒着挂(图中3开始向2,1)剩下的度为1的节点

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define maxn 10005
using namespace std;
int n;
int a[maxn];
struct edge{
int from;
int to;
edge(){ }
edge(int u,int v){
from=u;
to=v;
}
void print(){
printf("%d %d\n",from,to);
}
};
vector<edge>ans;
vector<int>l;
int main(){
int leaf=0;
scanf("%d",&n);
int sum=0;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
sum+=a[i];
if(a[i]==1) leaf++;
if(a[i]==0){
printf("NO\n");
return 0;
}
}
if(sum<n*2-2){
printf("NO\n");
return 0;
} for(int i=1;i<=n;i++){
if(a[i]==1){
l.push_back(i);
a[i]=0;
}
} int begin=-1;
if(l.size()){
begin=l[l.size()-1];
l.pop_back();
}
for(int i=1;i<=n;i++){
if(a[i]>1){
if(begin!=-1){
a[begin]--;
a[i]--;
ans.push_back(edge(begin,i));
}
begin=i;
}
}
int t=0;
for(int i=n;i>=1;i--){
while(l.size()&&a[i]>0){
a[i]--;
ans.push_back(edge(i,l[t++]));
if(t>=l.size()) break;
}
if(t>=l.size()) break;
} int d=(n-leaf)-1+min(2,leaf);
if(ans.size()==n-1){
printf("YES %d\n%d\n",d,n-1);
for(int i=0;i<n-1;i++){
ans[i].print();
}
}else{
printf("NO\n");
}
}

Codeforces 1082D (贪心)的更多相关文章

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

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

  2. CodeForces - 893D 贪心

    http://codeforces.com/problemset/problem/893/D 题意 Recenlty Luba有一张信用卡可用,一开始金额为0,每天早上可以去充任意数量的钱.到了晚上, ...

  3. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划

    There are n people and k keys on a straight line. Every person wants to get to the office which is l ...

  4. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心

    Arkady needs your help again! This time he decided to build his own high-speed Internet exchange poi ...

  5. CodeForces - 93B(贪心+vector<pair<int,double> >+double 的精度操作

    题目链接:http://codeforces.com/problemset/problem/93/B B. End of Exams time limit per test 1 second memo ...

  6. C - Ordering Pizza CodeForces - 867C 贪心 经典

    C - Ordering Pizza CodeForces - 867C C - Ordering Pizza 这个是最难的,一个贪心,很经典,但是我不会,早训结束看了题解才知道怎么贪心的. 这个是先 ...

  7. Codeforces 570C 贪心

    题目:http://codeforces.com/contest/570/problem/C 题意:给你一个字符串,由‘.’和小写字母组成.把两个相邻的‘.’替换成一个‘.’,算一次变换.现在给你一些 ...

  8. Codeforces 732e [贪心][stl乱搞]

    /* 不要低头,不要放弃,不要气馁,不要慌张 题意: 给n个插座,m个电脑.每个插座都有一个电压,每个电脑都有需求电压. 每个插座可以接若干变压器,每个变压器可以使得电压变为x/2上取整. 有无限个变 ...

  9. Codeforces 721D [贪心]

    /* 不要低头,不要放弃,不要气馁,不要慌张. 题意: 给一列数a,可以进行k次操作,每次操作可以选取任意一个数加x或者减x,x是固定的数.求如何才能使得这个数列所有数乘积最小. 思路: 贪心...讨 ...

随机推荐

  1. Docker实战部署应用——Redis

    Redis 部署 拉取Redis镜像 docker pull redis 创建Redis容器 docker run -id --name=sun_redis -p 6379:6379 redis 客户 ...

  2. php session之多级目录存储

    当选择以文件形式保存session到服务器时,需要制定保存路径.用到php.ini中的session.save_path,其有三种配置写法: session.save_path = "N;/ ...

  3. linux文档和目录结构

    Linux文件系统结构 Linux通过操作目录来实现对磁盘的读写.Linux通过使用正斜杠" / "来表示目录. Linux通过建立一个根目录,所有的目录都是通过根目录衍生出来的. ...

  4. 【leetcode】1080. Insufficient Nodes in Root to Leaf Paths

    题目如下: Given the root of a binary tree, consider all root to leaf paths: paths from the root to any l ...

  5. python-数据驱动

    1.parameterized.parameterized import unittest from parameterized import parameterized,param class Te ...

  6. python线程池示例

    使用with方式创建线程池,任务执行完毕之后,会自动关闭资源 , 否则就需要手动关闭线程池资源  import threading, time from concurrent.futures impo ...

  7. Xcode出现报错,但是没有给出详细信息,可以看这里

    Xcode出现报错,"Xcode build:clang: error: linker command failed with exit code 1 (use -v to..." ...

  8. Centos7防火墙和SELinux的开启和关闭

    在虚拟机里面开启多个服务,对应多个端口,在防火墙开启的情况下,就要对外开放端口,这样客户端才能正常访问,但比较繁琐,关闭更直接点. 防火墙 临时关闭防火墙 systemctl stop firewal ...

  9. nginx proxy大文件上传失败问题总结

    问题描述: http://www.syhuo.net ota.apk包上传正常 http://www.syhuo.net:8080 ota.apk包上传不正常 查看nginx error日志 [roo ...

  10. 【转】iis解决应用程序池**提供服务的进程意外终止进程ID是**。进程退出代码是'0x80'

    转自:http://blog.sina.com.cn/s/blog_56a68d5501013xdd.html 我们公司旗下的红黑互联会遇到这种问题 事件类型: 警告事件来源: W3SVC事件种类: ...