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 ...
随机推荐
- 《深入浅出MFC》– Document-View深入探讨
1.其实Document/View不是什么新东西,Xerox PARC实验室是这种观念的滥觞.它是Smalltalk环境中的关键性部分,在那里它被称为Model-View-Controller(MVC ...
- linux文件操作篇 (四) 目录操作
#include <sys/stat.h>#include <unistd.h>#include <dirent.h> //创建文件夹 路径 掩码 int mkdi ...
- ionic 打包apk Failure [INSTALL_FAILED_USER_RESTRICTED: Install canceled by user]
错误日志如下: Built the following apk(s): /Users/hongye0/Documents/project/haitoujiaApp/platforms/android/ ...
- RHEL-7.1 Server.x86_64 yum源设置为光盘
1.挂载光盘 首先在media目录下创建文件夹CentOS mkdir CentOS 然后将光盘挂载在CentOS下 mount -t iso9660 -o loop /dev/cdrom /medi ...
- DDR分析与布线要求
基本知识 Double Data Rate Synchronous Dynamic Random Access Memory 简称 DDR SDRAM 双倍数据率同步动态随机存取内存 DDR SDRA ...
- Hbase表格设计
Rowkey设计 Region: 基于RowKey的分区,可理解成MySQL的水平切分. 每个Region Server就是Hadoop集群中一台机器上的一个进程. 比如我们的有1-300号的RowK ...
- 创龙DSP6748开发板SYS/BIOS的LED闪烁-第2篇
1. 作为1个456MHz的处理器,不跑个操作系统说不过去,直接打开工程\Demo\SYSBIOS\Application\GPIO_LED,主函数比较简单 // 创建任务 Task_create(t ...
- 生成Excel.xlsx文件 iOS
使用到的三方库 https://github.com/jmcnamara/libxlsxwriter cocoapods导入 pod 'libxlsxwriter', '~> 0.8.3' 1. ...
- Python 3基础教程31-urllib模块
本文介绍Python里的urllib模块,这个urllib主要处理web服务的,如果需要做接口测试,或者写Python的网络爬虫,这个urllib就是最底层的库.需要用到里面的请求方法等. 1. 先看 ...
- python 网络编程(远程执行命令与粘包)
远程执行命令 先来学习一个新模块 , 一会用到的.. 新模块: subprocess 执行系统命令 r = subprocess.Popen('ls',shell=True,stdout=subpro ...