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. WebStorm ------------ 调整字体大小和背景

    WebStorm  一款前端编写工具,使用方式与idea 相似 如何调整字体大小 在设置里面找 设置编码背景 拷贝一个样式,,在此样式下进行更改 开始设置 设置好后 dd

  2. SQL Server ----- 生成sql 脚本

    通过生成sql 脚本进行数据库转移 选中需要进行转移的数据库 点击生成脚本后出现 进入的是简介界面    直接点击下一步 进入到选择对象界面. 按照步骤进行设置     选择你需要的    架构和数据 ...

  3. golang 使用 protobuf 的教程

    1.下载protobuf的编译器protoc 地址: https://github.com/google/protobuf/releases window:    下载: protoc-3.3.0-w ...

  4. 通过静态发现方式部署 Etcd 集群

    在「etcd使用入门」一文中对etcd的基本知识点和安装做了一个简要的介绍,这次我们来说说如何部署一个etcd集群. etcd构建自身高可用集群主要有三种形式: 静态发现: 预先已知etcd集群中有哪 ...

  5. 【转】socket通信-C#实现udp通讯

    在日常碰到的项目中,有些场景下不适合使用tcp常连接,而需要靠UDP无连接的数据收发.那么如何使用SharpSocket完成UDP收发数据呢?其中要掌握的关键点是什么呢? 点击查看原博文内容

  6. Swgger2的简单使用

    编写接口文档是一个非常枯燥的工作,我们采用Swagger2这套自动化文档工具来生成文档,它可以轻松的整合到Spring Boot中,并与Spring MVC程序配合组织出强大RESTful API文档 ...

  7. xcode模拟器使用常用的命令。

    1.查看模拟器的udid用的 xcrun instruments -s xcrun simctl list 2.启动这个模拟器: xcrun instruments -w 'B39EC2FF-8A8B ...

  8. Ansible--Ansible之Playbook

    Ansible之Playbook Playbook介绍 playbook参考文档 Playbook与ad-hoc相比,是一种完全不同的运用ansible的方式,类似与saltstack的state状态 ...

  9. jquery获取元素各种宽高及页面宽高

    如何使用jquery来获取网页里各种高度? 示例如下: $(document).ready(function(){  var divWidth = $("#div").width( ...

  10. mac下比较好用的svn软件,SVN客户端CornerStone 2.7.10 破解版

    一.已经破解,可以直接使用. dmg文件无密码,也不需要注册机.直接使用即可     二. 界面... 三.添加repository 点击左侧栏中REPOSITORY那一栏的+选择添加reposito ...