QWQ不得不说 \(cf\)的\(edu\ round\)出这种东西 有点太恶心了

题目大意:给你\(n\)个点,告诉你每个点的最大度数值(也就是说你的度数要小于等于这个),让你构造一个无向图,使其满足直径最大且包含所有点

一看就是个构造题

QWQ但是细节非常多。

大致上的思路就是,我们考虑把度数大于1的点拿出来,然后构成一条链,剩下的往这条链上挂就行,但是挂的时候要注意,优先往最头上的两个点挂,因为这样可以扩大直径,然后搞一搞就好

QWQ

(其实是强行凑博客文章数目)

记得特判一下只有一个度数大于1,或者没有的情况啊!

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#define mk make_pair
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
const int maxn = 2010;
vector<pair<int,int> > ans;
vector<int> v;
int a[maxn];
int n,m;
bool flag=1;
vector<int> v1;
int tmp;
int ymh;
int main()
{
n=read();
for (int i=1;i<=n;i++)
{//
a[i]=read();
if (a[i]>1) v.push_back(i),tmp+=a[i];
}
for (int i=1;i<=n;i++) if (a[i]==1) v1.push_back(i);
if (v.size()==1)
{
int pos=0;
for (int i=1;i<=n;i++) if (a[i]>1) pos=i;
if (tmp>=n-1)
{
cout<<"YES"<<" "<<min(n-1,2)<<endl;
cout<<n-1<<endl;
for (int i=1;i<=n;i++)
{
if (i!=pos)
{
cout<<i<<" "<<pos<<"\n";
}
}
}
else
{
cout<<"NO";
}
return 0;
}
if (v.size()==0)
{
cout<<"NO";
return 0;
}
for (int i=0;i<v.size()-1;i++) ans.push_back(mk(v[i],v[i+1])),a[v[i]]--,a[v[i+1]]--;
ymh = v.size()-1;
if (v1.size()==1)
{
ymh++;
ans.push_back(mk(v[0],v1[0]));
}
int now = 0;
if (v1.size()>=2)
{
ymh+=2;
now = 2;
ans.push_back(mk(v[0],v1[0]));
ans.push_back(mk(v[v.size()-1],v1[1]));
a[v[0]]--;
a[v[v.size()-1]]--;
int i=0;
while (i<v.size() && now<v1.size())
{
while (i<v.size() && a[v[i]]==0) i++;
if (i==v.size()) break;
ans.push_back(mk(v[i],v1[now]));
now++;
a[v[i]]--;
} }
if (now!=v1.size() && v1.size()>=2)
{
cout<<"NO";
}
else
{
cout<<"YES"<<" "<<ymh<<endl;
cout<<ans.size()<<endl;
for (int i=0;i<ans.size();i++)
{
cout<<ans[i].first<<" "<<ans[i].second<<"\n";
}
}
return 0;
}

cf1082D Maximum Diameter Graph(构造+模拟+细节)的更多相关文章

  1. [CF1082D]Maximum Diameter Graph

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

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

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

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

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

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

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

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

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

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

  7. CodeForces 1082 D Maximum Diameter Graph

    题目传送门 题意:现在有n个点,每个点的度数最大为di,现在要求你构成一棵树,求直径最长. 题解:把所有度数为2的点先扣出来,这些就是这颗树的主干,也就是最长的距离. 然后我们把度数为2的点连起来,之 ...

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

  9. Codeforces Round #398 (Div. 2) A B C D 模拟 细节 dfs 贪心

    A. Snacktower time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

随机推荐

  1. rabbitMq可靠性投递之手动ACK

    #手动应答#spring.rabbitmq.listener.simple.acknowledge-mode=manual#spring.rabbitmq.listener.simple.acknow ...

  2. 跨域@RequestBody@RequestParam 和JSON.stringify

  3. jquery mobile常用的data-role类型

    data-role参数表: page        页面容器,其内部的mobile元素将会继承这个容器上所设置的属性 header     页面标题容器,这个容器内部可以包含文字.返回按钮.功能按钮等 ...

  4. linux centos7 增加操作日志记录

    2021-08-24 1. 需求产生原因 linux 系统中的日志存放在目录 /var/log/ 下,今天想看看我之前的操作记录,发现系统中的日志并不包括各个用户操作文件的记录,所以打算自己建一个. ...

  5. RabbitMQ从零到集群高可用(.NetCore5.0) -高可用集群构建落地

    系列文章: RabbitMQ从零到集群高可用(.NetCore5.0) - RabbitMQ简介和六种工作模式详解 RabbitMQ从零到集群高可用(.NetCore5.0) - 死信队列,延时队列 ...

  6. (六)羽夏看C语言——函数

    写在前面   由于此系列是本人一个字一个字码出来的,包括示例和实验截图.本人非计算机专业,可能对本教程涉及的事物没有了解的足够深入,如有错误,欢迎批评指正. 如有好的建议,欢迎反馈.码字不易,如果本篇 ...

  7. noip模拟测试18

    打开比赛第一眼--超级树? 点开--原题 百感交集-- 欣喜于发现是半年前做过两遍的原题 紧张于如果A不了比较尴尬 绝望于发现根本不会做了 瞟了一眼t1,瞅了一眼t2,嗯--开始搞t3 10分钟打完暴 ...

  8. harbor高可用集群搭建

    高可用harbor集群搭建 一.安装部署 1.节点角色 角色 数量 名称 备注 harbor主节点 2 harbor-1 harbor-2 双主模式 haproxy 2 HA-1 HA-2 需要通过k ...

  9. 多文件Makefile编写

    工作过程中,平时不怎么关注Makefile的书写规则,对于遇到的编译错误一般能看懂Makefile的基本规则也能解决.但如果想要编写Makefile文件还是有相当的难度的,更不用说包含多个目录和文件的 ...

  10. 将两个byte型拼接成16位二进制,再转化为十进制

    short s = 0; //一个16位整形变量,初值为 0000 0000 0000 0000 byte b1 = 1; //一个byte的变量,作为转换后的高8位,假设初值为 0000 0001 ...