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的那份数据,这就一神题.. 借助极角排序,枚举以每一个点进行极角排序,然后构造两条扫描线,一个上面一个下面,两条同时走,把上线和下线的点以及上线左边的点分别统计出来,如 ...
随机推荐
- 【计算机视觉】BRIEF特征匹配
Binary Robust Independent Elementary Features www.cnblogs.com/ronny 1. BRIEF的基本原理 我们已经知道SIFT特征采用了128 ...
- 第九次作业 DFA最小化,语法分析初步
1.将DFA最小化:教材P65 第9题 Ⅰ {1,2,3,4,5} {6,7} {1,2}b={1,2,3,4,5} 3,4}b={5} {6,7} Ⅱ {1,2}{3,4}{5} {6,7} 2.构 ...
- docker compose 编排
Compose是Docker的服务编排工具,主要用来构建基于Docker的复杂应用,Compose 通过一个配置文件来管理多个Docker容器,非常适合组合使用多个容器进行开发的场景. 说明:Comp ...
- vue-router学习笔记(一)
学习vue-router首先要认识的两个属性 $router 和 $route. $router指的是router实例,$route则是当前激活的路由信息对象,是只读属性,不可更改,但是可以watch ...
- HTML5 Canvas实战之刮奖效果【转】
开源项目地址:https://github.com/artwl/Lottery 作者博客地址:http://www.cnblogs.com/jscode/p/3580878.html 谢谢浏览!
- python面试题_01
前言 现在面试测试岗位,一般会要求熟悉一门语言(python/java),为了考验求职者的基本功,一般会出2个笔试题,这些题目一般不难,主要考察基本功.要是给你一台电脑,在编辑器里面边写边调试,没多大 ...
- AGC038
Contest Page 开题开错翻车场.jpg A sol $A > \frac{W}{2}$或者$B > \frac{H}{2}$的时候无解,否则构造方法长下面这样 #include& ...
- -Shell 教程 Bash 脚本 基础语法 MD
目录 目录 Shell 简介 Shell 脚本 Shell 环境 第一个shell脚本 Shell 变量 定义变量 使用变量 只读变量 删除变量 Shell 字符串 单引号 双引号 字符串基本操作 S ...
- WPF设置全局控件样式
原文:WPF设置全局控件样式 方法: 在资源文件APP.XAML中添加如下资源 <Application x:Class="_360UI.App" xmlns="h ...
- SQL Server的NTEXT类型不支持等号"="操作(转载)
SQL SERVER – Fix: Error : 402 The data types ntext and varchar are incompatible in the equal to oper ...