Hard Molecules

给定一个连通图中每个点的度数,求一个满足条件的图,图可以有重边,不能有自环。

n<=5000, di<=109

题解

如果不要求图连通,那么只需要判断

\[\sum d_i \mod 2=0\\
\max\{d_i\} \leq \frac{\sum d_i}{2}
\]

然后将度数分成两部分连边即可。

现在要求图连通,那么就先做一棵生成树出来。

由于我们想让每个点的非树边最大度数最小,所以我们可以贪心:维护一棵树,每次选择树上度数最大的点和树外度数最大的点连边。

CO int N=5000+10;
int n,d[N],G[N][N]; IN void connect(int x,int y,int w){
G[x][y]+=w,G[y][x]+=w;
d[x]-=w,d[y]-=w;
} bool vis[N]; bool build_tree(){
int root=1;
for(int i=2;i<=n;++i)
if(d[i]>d[root]) root=i;
vis[root]=1;
for(int t=1;t<n;++t){
int a=-1;
for(int i=1;i<=n;++i)if(vis[i])
if(a==-1 or d[i]>d[a]) a=i;
int b=-1;
for(int i=1;i<=n;++i)if(!vis[i])
if(b==-1 or d[i]>d[b]) b=i;
if(a==-1 or b==-1) return 0;
if(d[a]==0 or d[b]==0) return 0;
connect(a,b,1);
vis[b]=1;
}
return 1;
} typedef pair<int,int> pii;
vector<pii> L,R; bool check(){
LL tot=0;
for(int i=1;i<=n;++i) tot+=d[i];
if(tot&1) return 0;
tot>>=1;
for(int i=1;i<=n;++i)
if(d[i]>tot) return 0;
for(int i=1;i<=n;++i)if(d[i]){
if(tot>=d[i]){
tot-=d[i];
L.push_back(pii(d[i],i));
}
else{
if(tot) L.push_back(pii(tot,i));
R.push_back(pii(d[i]-tot,i));
tot=0;
}
}
return 1;
}
int main(){
read(n);
for(int i=1;i<=n;++i) read(d[i]);
if(build_tree() and check()){
puts("Yes");
for(int i=0,p=0;i<(int)L.size();++i){
for(;p<(int)R.size() and L[i].first>=R[p].first;++p){
connect(L[i].second,R[p].second,R[p].first);
L[i].first-=R[p].first;
}
if(L[i].first){
connect(L[i].second,R[p].second,L[i].first);
R[p].first-=L[i].first;
}
}
for(int i=1;i<=n;++i,puts(""))
for(int j=i+1;j<=n;++j) printf("%d ",G[i][j]);
}
else puts("No");
return 0;
}

Gym100739H Hard Molecules的更多相关文章

  1. Simple Molecules(简单)

    Simple Molecules time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  2. uva 1606 amphiphilic carbon molecules【把缩写写出来,有惊喜】(滑动窗口)——yhx

    Shanghai Hypercomputers, the world's largest computer chip manufacturer, has invented a new classof ...

  3. 【极角排序、扫描线】UVa 1606 - Amphiphilic Carbon Molecules(两亲性分子)

    Shanghai Hypercomputers, the world's largest computer chip manufacturer, has invented a new class of ...

  4. LightOJ 1118 - Incredible Molecules (两圆面积交)

    1118 - Incredible Molecules   PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit: ...

  5. IEEEXtreme 10.0 - Counting Molecules

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Counting Molecules 题目来源 第10届IEEE极限编程大赛 https://www.hac ...

  6. LightOJ 1118--Incredible Molecules(两圆相交)

    1118 - Incredible Molecules      PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Lim ...

  7. CodeForces - 344B Simple Molecules (模拟题)

    CodeForces - 344B id=46665" style="color:blue; text-decoration:none">Simple Molecu ...

  8. codeforces Simple Molecules

    link:http://codeforces.com/contest/344/problem/B 刚开始想复杂了.一开始就想当然地以为可以有多个点,其实,人家题目要求只有3个点啊! 然后题目就简单了. ...

  9. poj2280Amphiphilic Carbon Molecules(极角排序)

    链接 卡了几天的破题,对于hdu的那份数据,这就一神题.. 借助极角排序,枚举以每一个点进行极角排序,然后构造两条扫描线,一个上面一个下面,两条同时走,把上线和下线的点以及上线左边的点分别统计出来,如 ...

随机推荐

  1. odoo前端必填提示

  2. 去除img标签函数

    需要去除一个长字符串中的img标签,网上找到了这个代码试试看,确实是有效的.代码如下: <?php function strip_tags_img($string='') { $pattern= ...

  3. Spring的JdbcTemplate使用教程

    Spring对数据库的操作在jdbc上面做了基本的封装,让开发者在操作数据库时只需关注SQL语句和查询 结果处理器,即可完成功能(当然,只使用JdbcTemplate,还不能摆脱持久层实现类的编写). ...

  4. 树莓派4B基本配置

    一.系统安装 官网下载好系统解压,使用SD Card Formatter格式化内存卡 # 查看内存卡状态,通过内存卡大小判断是哪个 df -lh # 卸载内存卡 diskutil unmount /d ...

  5. 浅析PHP框架Laravel最新SQL注入漏洞

    PHP知名开发框架Laravel,之前在官方博客通报了一个高危SQL注入漏洞,这里简单分析下. 首先,这个漏洞属于网站coding写法不规范,官方给了提示: 但官方还是做了修补,升级最新版本V5.8. ...

  6. [转帖]自动交互式脚本--expect

    自动交互式脚本--expect https://www.cnblogs.com/zhuiluoyu/p/4873869.html 我们经常会遇到一些需要与服务器程序打交道的场景,比如,从登陆某个服务器 ...

  7. python利用dijkstra算法求解图中最短距离

    利用dijkstra算法,来完成图中两个顶点间最短的距离,可以直接复制使用,只需要修改参数即可 def dijkstra_raw(edges, from_node, to_node): "& ...

  8. OpenStack 中 RabbitMQ 的使用

    OpenStack 中 RabbitMQ 的使用 本文是 OpenStack 中的 RabbitMQ 使用研究 两部分中的第一部分,将介绍 RabbitMQ 的基本概念,即 RabbitMQ 是什么. ...

  9. phpstorm 2016.3.2 的最新破解方法

    v2.0 最新的方式 第一:下载PHPStorm20173.2:(下载链接:windows) 第二:直接用浏览器打开 http://idea.lanyus.com/ ,点击页面中的“获得注册码”,然后 ...

  10. 来自后端的逆袭 blazor简介 全栈的福音

    背景 什么是SPA 什么是MPA MPA (Multi-page Application) 多页面应用指的就是最传统的 HTML 网页设计,早期的网站都是这样的设计,所之称为「网页设计」.使用 MPA ...