Gym100739H Hard Molecules
Hard Molecules
给定一个连通图中每个点的度数,求一个满足条件的图,图可以有重边,不能有自环。
n<=5000, di<=109
题解
如果不要求图连通,那么只需要判断
\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的更多相关文章
- Simple Molecules(简单)
Simple Molecules time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- uva 1606 amphiphilic carbon molecules【把缩写写出来,有惊喜】(滑动窗口)——yhx
Shanghai Hypercomputers, the world's largest computer chip manufacturer, has invented a new classof ...
- 【极角排序、扫描线】UVa 1606 - Amphiphilic Carbon Molecules(两亲性分子)
Shanghai Hypercomputers, the world's largest computer chip manufacturer, has invented a new class of ...
- LightOJ 1118 - Incredible Molecules (两圆面积交)
1118 - Incredible Molecules PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit: ...
- IEEEXtreme 10.0 - Counting Molecules
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Counting Molecules 题目来源 第10届IEEE极限编程大赛 https://www.hac ...
- LightOJ 1118--Incredible Molecules(两圆相交)
1118 - Incredible Molecules PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Lim ...
- CodeForces - 344B Simple Molecules (模拟题)
CodeForces - 344B id=46665" style="color:blue; text-decoration:none">Simple Molecu ...
- codeforces Simple Molecules
link:http://codeforces.com/contest/344/problem/B 刚开始想复杂了.一开始就想当然地以为可以有多个点,其实,人家题目要求只有3个点啊! 然后题目就简单了. ...
- poj2280Amphiphilic Carbon Molecules(极角排序)
链接 卡了几天的破题,对于hdu的那份数据,这就一神题.. 借助极角排序,枚举以每一个点进行极角排序,然后构造两条扫描线,一个上面一个下面,两条同时走,把上线和下线的点以及上线左边的点分别统计出来,如 ...
随机推荐
- 【RS】:论文《Neural Collaborative Filtering》的思路及模型框架
[论文的思路] NCF 框架如上: 1.输入层:首先将输入的user.item表示为二值化的稀疏向量(用one-hot encoding) 2.嵌入层(embedding):将稀疏表示映射为稠密向量( ...
- Alpha冲刺(11/10)——2019.5.3
作业描述 课程 软件工程1916|W(福州大学) 团队名称 修!咻咻! 作业要求 项目Alpha冲刺(团队) 团队目标 切实可行的计算机协会维修预约平台 开发工具 Eclipse 团队信息 队员学号 ...
- Jenkins绑定git
1,新建任务
- Oracle账号,用于下载jdk
账号:liwei@xiaostudy.com 密码:OracleTest1234
- Spring BeanFactory继承结构图
结构图 高清大图:https://img2018.cnblogs.com/blog/813478/201910/813478-20191030114422275-1092084932.jpg 源文件( ...
- word 转 pfd
转自: https://www.cnblogs.com/qiwu1314/p/6101400.html demo: public class Doc2Pdf { public static boole ...
- 【洛谷】P1022 计算器的改良-全AC题解
#include <iostream> #include <cstring> #include <iomanip> using namespace std; int ...
- java -jar参数运行方式设置classpath
转载自:https://www.cnblogs.com/aggavara/archive/2012/11/16/2773246.html 当用java -jar yourJarExe.jar来运行一个 ...
- spring cloud应用
1.什么是注册中心 (1)就是首先有一个eureka server,服务的注册与发现的中心(2)你如果写好了一个服务,就可以将其注册到eureka server上去(3)然后别人的服务如果要调用你的服 ...
- MySQL数据类型之BLOB与TEXT及其最大存储限制
https://blog.csdn.net/q3dxdx/article/details/51014357 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.n ...