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. 《Linux就该这么学》培训笔记_ch07_RAID和LVM

    <Linux就该这么学>培训笔记_ch07_RAID和LVM 文章最后会post上书本的笔记照片. 文章主要内容: RAID(独立冗余磁盘阵列) 部署磁盘阵列 损坏磁盘阵列及修复 磁盘阵列 ...

  2. 关于linux 执行权限的理解

    关于linux 执行权限的理解他这个执行权限不是 执行什么命令的 是这个文件 是否能被执行的权限 比方说<pre>shell_exec('/home/crontabtest12.sh'); ...

  3. python入门之数据类型及内置方法

    目录 一.题记 二.整形int 2.1 用途 2.2 定义方式 2.3 常用方法 2.3.1 进制之间的转换 2.3.2 数据类型转换 3 类型总结 三.浮点型float 3.1 用途 3.2 定义方 ...

  4. Thinkphp中的系统变量

    Action中使用的系统常量 THINK_PATH // ThinkPHP 系统目录 APP_PATH // 当前项目目录 APP_NAME // 当前项目名称 MODULE_NAME //当前模块名 ...

  5. Java8 新特性 函数式接口

    什么是函数式接口   函数式接口是Java8引用的一个新特性,是一种特殊的接口:SAM类型的接口(Single Abstract Method).但是它还是一个接口,只是有些特殊罢了.  函数式接口的 ...

  6. SQLServer字符串与数字拼接

    1.使用cast‘’+cast(@ID as varchar) 2.使用LTrim‘’+LTrim(@ID) 感觉第二种方式代码简单,但是可读性不好.

  7. Nginx配置REWRITE隐藏index.php

    server { listen 80; server_name localhost; root D:\workspace\PHP\Atromic; location / { index index.p ...

  8. Webstorm关闭ESLint警告

    使用Webstorm新创建的VUE项目在npm run dev的时候老是报各种格式问题i:比如多个一个空格?多了一行?分号问题?.....简直是受不鸟... 是因为你使用了eslint,这个是esli ...

  9. Python基础笔记(五)

    1. 类(class) 下面的代码建立了一个Employee类: #!/usr/bin/env python3 # -*- coding: utf-8 -*- class Employee(objec ...

  10. ES7.3.0配置邮件告警

    情况说明: 三台es组成集群,一台kibana,版本均为7.3.0 es版本要求是白金版,基础版的不行,不过可以试用30天的白金版 步骤:先说我自己走通的流程,然后介绍官方说明 1.因为我这边使用的是 ...