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. OpenCV 传统分割测试

    github官网源文件:https://github.com/opencv/opencv/tree/master/samples/python 最好是先克隆整个仓库下来,再测试里面的:floodfil ...

  2. 理解ASP.NET Core - [02] Middleware

    注:本文隶属于<理解ASP.NET Core>系列文章,请查看置顶博客或点击此处查看全文目录 中间件 先借用微软官方文档的一张图: 可以看到,中间件实际上是一种配置在HTTP请求管道中,用 ...

  3. Linux系统的内核编译

    <1>给虚拟机分配2048M内存 <2>配置高可用yum源 <3>下载软件 <1>安装内核源码包 根据依赖性提示,安装对应的包 下载并安装软件包(3个) ...

  4. Python安装环境配置和多版本共存

    Python安装环境配置和多版本共存 1.环境变量配置: (1) 右键点击"计算机",然后点击"属性" (2) 然后点击"高级系统设置" ( ...

  5. golang 模板 html/template与text/template

    html模板生成: html/template包实现了数据驱动的模板,用于生成可对抗代码注入的安全HTML输出.它提供了和text/template包相同的接口,Go语言中输出HTML的场景都应使用t ...

  6. 20210715 noip16

    考场 乍一看 T1 像是二分答案,手玩样例发现可以 \(O(k^2)\) 枚举点对,贪心地更新答案,完了?有点不信,先跳了 T2 的形式有点像逆序对,但没啥想法 T3 的式子完全不知道如何处理,一看就 ...

  7. Spring Boot入门系列(二十六)超级简单!Spring Data JPA 的使用!

    之前介绍了Mybatis数据库ORM框架,也介绍了使用Spring Boot 的jdbcTemplate 操作数据库.其实Spring Boot 还有一个非常实用的数据操作框架:Spring Data ...

  8. C# AutoMaper使用自定义主键

    有时候实际业务中主键不一定叫Id,比如示例数据库Northwind中的RegionID,TerritoryID等,本示例用Abp框架并以Northwind数据库Region表为数据依据 一.在Core ...

  9. 面试HashMap你都扛不住,还想拿到offer?

    当我们面试Java开发岗位时,面试官问的频率出现最多的问题,就是这个HashMap,不管是传统型公司还是互联公司,HashMap是必问的,所以作者爆肝整理了HashMap的23个问题以及答案,请查收! ...

  10. IO流实现GBK写入文件然后转换UTF-8

    public static void main(String[] args) throws IOException { File file = new File("olol\\a.txt&q ...