Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph
D. Maximum Diameter Graph
题目链接:https://codeforces.com/contest/1082/problem/D
题意:
给出n个点的最大入度数,要求添加边构成一个无环无向无重边的图,并且最大化图中的最短路径。
题解:
要求最短路径最大,我们会想到把每一个允许入度可以大于1的点先连起来,形成一个”链“,当前肯定满足条件,并且两端点的最短路径长度是所有情况中最大的。
然后对于入度为1的点,先尽量考虑放在链的两端,然后再在中间随便插入。
代码如下:
先附上自己用邻接矩阵存储的丑代码。。。代码思路比较直接:
#include <bits/stdc++.h>
using namespace std; const int N = ;
int Map[N][N],vis[N],a[N];
int n; int main(){
scanf("%d",&n);
int cnt = ,tot = ,st = ;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]==) cnt++;
else st=i;
}
vis[st]=;bool flag=false;
int end=st;
for(int i=st;;i=end){
if(flag) break;
int tmp=;
for(int j=;j<=n;j++){
if(a[j]> && !vis[j]){
tmp++;
Map[i][j]=Map[j][i]=;
vis[j]=;
a[i]--;a[j]--;
end=j;
break;
}
}
if(!tmp) flag=true;
}
int tmp = ;
for(int i=;i<=n;i++) if(vis[i]) tot+=a[i],tmp++;
if(cnt>tot){
cout<<"NO";
return ;
}
int ans,edges=tmp-;
if(cnt>) ans=edges+;
else if(cnt==) ans=edges+;
else ans=edges;
cout<<"YES"<<" "<<ans<<endl;
if(cnt){
cnt--;
for(int i=;i<=n;i++) if(!vis[i]){
vis[i]=Map[i][st]=Map[st][i]=;
edges++;
a[st]--;a[i]--;
break ;
}
if(cnt){
cnt--;
for(int i=;i<=n;i++) if(!vis[i]){
vis[i]=Map[i][end]=Map[end][i]=;
edges++;
a[end]--;a[i]--;
break;
}
}
if(cnt){
for(int i=n;i>=;i--) if(!vis[i]){
vis[i]=;
for(int j=;j<=n;j++){
if(a[j] && vis[j] &&j!=i){
edges++;
Map[j][i]=Map[i][j]=;
a[j]--;a[i]--;
break;
}
}
}
}
}
cout<<edges<<endl;
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
if(Map[i][j]) printf("%d %d\n",i,j);
return ;
}
再来一个O(N)的算法,注意一下代码的细节,直接像链一样连边:
#include <bits/stdc++.h>
using namespace std; const int N = ;
int a[N];
int n,sum; int main(){
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]),sum+=a[i];
if(sum<*(n-)){
cout<<"NO";return ;
}
vector <int> ones;
for(int i=;i<=n;i++){
if(a[i]==) a[i]= , ones.push_back(i);
}
int t=ones.size();
printf("YES %d\n%d\n",n-t-+min(t,),n-);
int st=;
if(!ones.empty()){
st=ones.back();
ones.pop_back();
}
for(int i=;i<=n;i++){//从起点开始添边形成一个链
if(a[i]>){
if(st){
a[st]--;a[i]--;
printf("%d %d\n",st,i);
}
st=i;
}
}
for(int i=n;i>=;i--){ // 从尾开始添加(在链上)
while(a[i]> && !ones.empty()){
a[i]--;
int now1 = ones.back();ones.pop_back();
printf("%d %d\n",i,now1);
}
}
return ;
}
Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph的更多相关文章
- Educational Codeforces Round 55 (Rated for Div. 2):E. Increasing Frequency
E. Increasing Frequency 题目链接:https://codeforces.com/contest/1082/problem/E 题意: 给出n个数以及一个c,现在可以对一个区间上 ...
- Educational Codeforces Round 55 (Rated for Div. 2):C. Multi-Subject Competition
C. Multi-Subject Competition 题目链接:https://codeforces.com/contest/1082/problem/C 题意: 给出n个信息,每个信息包含专业编 ...
- Educational Codeforces Round 47 (Rated for Div. 2) :D. Relatively Prime Graph
题目链接:http://codeforces.com/contest/1009/problem/D 解题心得: 题意就是给你n个点编号1-n,要你建立m条无向边在两个互质的点之间,最后所有点形成一个连 ...
- Educational Codeforces Round 55 (Rated for Div. 2)E
题:https://codeforces.com/contest/1082/problem/E 题意:给出n个数和一个数c,只能操作一次将[L,R]之间的数+任意数,问最后该序列中能存在最多多少个c ...
- Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition 【vector 预处理优化】
传送门:http://codeforces.com/contest/1082/problem/C C. Multi-Subject Competition time limit per test 2 ...
- Educational Codeforces Round 55 (Rated for Div. 2) A/B/C/D
http://codeforces.com/contest/1082/problem/A WA数发,因为默认为x<y = = 分情况讨论,直达 or x->1->y or x-& ...
- Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies 【贪心 】
传送门:http://codeforces.com/contest/1082/problem/B B. Vova and Trophies time limit per test 2 seconds ...
- Codeforces 1082 C. Multi-Subject Competition-有点意思 (Educational Codeforces Round 55 (Rated for Div. 2))
C. Multi-Subject Competition time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces 1082 A. Vasya and Book-题意 (Educational Codeforces Round 55 (Rated for Div. 2))
A. Vasya and Book time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
随机推荐
- STL——泛型编程
1.指针的算术运算 对于一个存储int数据的vector,我们要查找某值是否存在于其中,采用下标操作的做法如下: int* find(const vector<int> &vec, ...
- vs2013中将复制过来的文件或文件夹显示到解决方案管理
先将文件夹和文件复制到VS程序所在的位置,在VS2013解决方案资源管理器中找到这些文件所在的上一级文件夹,先将那个上层文件夹收缩起来,然后再点击解决方案资源管理器上的“显示所有文件”按纽,展开这个文 ...
- 【MVC】 小问题
[MVC] 小问题 1. url 传参中文乱码 : encodeURIComponent 转码 2. RedirectToAction 重定向 : ajax 调用无效, 直接 url 访问有效 3. ...
- 使用Visual Studio 2017构建.Net Core的Docker镜像
1 Docker 镜像优化 微软在为开发人员生成 Docker 镜像时,提供以下三种主要方案: 用于开发 .NET Core 应用的 镜像 用于构建生成 .NET Core 应用的 镜像 用于运行 ...
- Linux-Shell脚本编程-学习-2-Linux基本命令
接上篇,接着学习Linux下的部分命令,后面的这些命令用到的频率可能没有那么多,不过也是经常需要的. 第一部分:程序监测部分,ps和top top命令可能比较眼熟,所以我们先说ps ps命令最烦人了, ...
- VSCode 前端必备插件
VSCode 前端必备插件 Debugger for Chrome 让 vscode 映射 chrome 的 debug功能,静态页面都可以用 vscode 来打断点调试 { "versio ...
- Ubuntu16.04 + CUDA9.0 + cuDNN7.3 + Tensorflow-gpu-1.12 + Jupyter Notebook 深度学习环境配置
目录 一.Ubuntu16.04 LTS系统的安装 二.设置软件源的国内镜像 1. 设置方法 2.关于ubuntu镜像的小知识 三.Nvidia显卡驱动的安装 1. 首先查看显卡型号和推荐的显卡驱动 ...
- 3.爬虫 urlib库讲解 总结
urllib库的总结: 用ProcessOn(安利这个软件,够用了)根据前面的几节内容做了个思维导图. urllib库一共有四个模块: request:它是最基本的模块,可以用来模拟发送请求 erro ...
- 简易cmake多文件多目录工程模板
今天心血来潮,想在服务器上试试写libevent的工程是什么感受,那第一步就是学会怎么用cmake建工程,之前也没接触过cmake,然后一上午,比较懵逼,下午看实验室哥们给的一个教程,然后,慢慢理解C ...
- HDU 4744 Starloop System(最小费用最大流)(2013 ACM/ICPC Asia Regional Hangzhou Online)
Description At the end of the 200013 th year of the Galaxy era, the war between Carbon-based lives a ...