AT2112 Non-redundant Drive
题目:https://www.luogu.org/problemnew/show/AT2112
对于这种找路径的就直接上点分治就好。
分治时,算出每一个点到分治重心的后能剩多少油,从分治重心走到每个点最少需要多少起始油量。
对这两个数组排序后合并即可。
注意,合并的时候要保证不属于同一棵子树,这个可以利用boruvka时用到的那个技巧来实现。
#include<bits/stdc++.h>
#define N 220000
#define db double
#define ll long long
#define ldb long double
using namespace std;
inline ll read()
{
char ch=0;
ll x=0,flag=1;
while(!isdigit(ch)){ch=getchar();if(ch=='-')flag=-1;}
while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*flag;
}
const ll inf=1e9;
struct edge{ll to,nxt,w;}e[N*2];
ll num,head[N];
inline void add(ll x,ll y,ll z){e[++num]=(edge){y,head[x],z};head[x]=num;}
bool vis[N];
ll rt,ans,size,cnt1,cnt2,w[N],sz[N],bal[N];
struct node{ll x,w,k;}f[N],g[N];
bool cmp(node a,node b){return a.x<b.x;}
bool operator<(node a,node b){return a.w<b.w;}
void get_rt(ll x,ll fa)
{
sz[x]=1;bal[x]=0;
for(ll i=head[x];i!=-1;i=e[i].nxt)
{
ll to=e[i].to;
if(vis[to]||to==fa)continue;
get_rt(to,x);sz[x]+=sz[to];
bal[x]=max(bal[x],sz[to]);
}
bal[x]=max(bal[x],size-sz[x]);
if(bal[rt]>bal[x])rt=x;
}
void get_sz(ll x,ll fa)
{
sz[x]=1;
for(ll i=head[x];i!=-1;i=e[i].nxt)
{
ll to=e[i].to;
if(vis[to]||to==fa)continue;
get_sz(to,x);sz[x]+=sz[to];
}
}
void cal1(ll x,ll k,ll t,ll fa,ll dep,ll flag)
{
if(fa==rt)flag=x;
if(w[x]>=k)f[++cnt1]=(node){t+w[x],dep,flag};
for(ll i=head[x];i!=-1;i=e[i].nxt)
{
ll to=e[i].to,v=e[i].w;
if(vis[to]||to==fa)continue;
cal1(to,max(v,k+v-w[x]),t-v+w[x],x,dep+1,flag);
}
}
void cal2(ll x,ll k,ll t,ll fa,ll dep,ll flag)
{
if(fa==rt)flag=x;
g[++cnt2]=(node){k,dep,flag};
for(ll i=head[x];i!=-1;i=e[i].nxt)
{
ll to=e[i].to,v=e[i].w;
if(vis[to]||to==fa)continue;
cal2(to,max(k,v-t),t-v+w[to],x,dep+1,flag);
}
}
void solve(ll x)
{
bal[rt=0]=inf;get_rt(x,x);get_sz(rt,rt);vis[rt]=true;
cnt1=0;cal1(rt,0,0,rt,1,rt);sort(f+1,f+cnt1+1,cmp);
cnt2=0;cal2(rt,0,0,rt,0,rt);sort(g+1,g+cnt2+1,cmp);
node a={0,-inf,0},b={0,-inf,0};
for(ll i=1,j=0;i<=cnt1;i++)
{
while(j!=cnt2&&g[j+1].x<=f[i].x)
{
j++;
if(g[j].k==a.k)a=max(a,g[j]);
else
{
b=max(b,g[j]);
if(a<b)swap(a,b);
}
}
if(f[i].k!=a.k)ans=max(ans,f[i].w+a.w);
else ans=max(ans,f[i].w+b.w);
}
for(ll i=head[rt];i!=-1;i=e[i].nxt)
{
ll to=e[i].to;
if(vis[to])continue;
size=sz[to];solve(to);
}
}
int main()
{
ll n=read();
for(ll i=1;i<=n;i++)w[i]=read();
num=-1;memset(head,-1,sizeof(head));
for(ll i=1;i<n;i++)
{
ll x=read(),y=read(),z=read();
add(x,y,z);add(y,x,z);
}
ans=1;size=n;solve(1);printf("%lld\n",ans);
return 0;
}
AT2112 Non-redundant Drive的更多相关文章
- AHCI: Failed to attach drive to Port1 (VERR_GENERAL_FAILURE).
在mac操作系统下,安装VirtualBoxVm虚拟机,虚拟机里面安装wind7操作系统.在启动虚拟机的时候报错:AHCI: Failed to attach drive to Port1 (VERR ...
- Windows Azure Storage (17) Azure Storage读取访问地域冗余(Read Access – Geo Redundant Storage, RA-GRS)
<Windows Azure Platform 系列文章目录> 细心的用户会发现,微软在国外和国内的数据中心建设都是成对的,比如香港数据中心(Asia East)和新加坡的数据中心(Sou ...
- vmware安装win7提示No CD-ROM drive to use:GCDROM not loaded
今天安装win7 64位的操作系统到vmware虚拟机,以为一切事如此的简单,因为自己以前经常拿vmware来装系统,结果确出现下面莫名其妙的错误: 提示说没有CD-ROM,可是我明明在vmware的 ...
- Ignite 配置更新Oracle JDBC Drive
如果使用Oracle 12C 作为Ignite 的Repository的话,在Repository Createion Wizard的配置过程中,会出现ORA-28040:No matc ...
- 网盘的选择,百度网盘、google drive 还是 Dropbox
我是国内用户,需要越过Chinawall 我使用的是一枝红杏,用着还行 如果要买,结账时输入'laod80' 一枝红杏官网:官网地址 Dropbox: 稳定,速度快 Dropbox官网:链接 操作十分 ...
- POJ 3177 Redundant Paths(边双连通的构造)
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13717 Accepted: 5824 ...
- UEFI Bootable USB Flash Drive - Create in Windows(WIN7 WIN8)
How to Create a Bootable UEFI USB Flash Drive for Installing Windows 7, Windows 8, or Windows 8.1 In ...
- Create a SQL Server Database on a network shared drive
(原文地址:http://blogs.msdn.com/b/varund/archive/2010/09/02/create-a-sql-server-database-on-a-network-sh ...
- 【转】Expire Google Drive Files 让Google Docs云盘共享连接在指定时间后自动失效
最近在清理Google Docs中之前共享过的文件链接,发现Google Docs多人协作共享过的链接会一直存在,在实际操作中较不灵活.正好订阅的RSS推送了Pseric写的这篇文章 - Expire ...
随机推荐
- HDU1285 确定比赛问题【拓扑排序+优先队列】
题目 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩, ...
- vba编程基础1
在主要的编程语言中, 结构体是用大括号来表示 代码段的 范围 界定的. 但是在一些比较"老的"语言中,由于历史原因, 还是使用的 是: 关键字来进行界定代码 的 结构段, 如vba ...
- 用RAR将多个文件夹一次性压缩为多个对应zip文件
选中要压缩的所有文件夹.右键,选“添加到压缩文件...”,弹出的菜单如下图: 点击菜单栏“文件”.在“把每个文件都单独压缩文件中”选中,才可以单独创建压缩.如下图
- skype for business 无法共享桌面、无法传输图片
以管理员身份运行如下PowerShell命令,清除Skype for Business缓存记录 #以管理员身份运行如下PowerShell命令,清除Skype for Business缓存记录 Sto ...
- facebook api之Business Manager API
Business-scoped Users - The new user is tied to a particular business and has permissions scoped to ...
- idea 常用设置初始化
1.idea中mybatis关联到mapper.xml文件 2.idea热部署设置 3.IDEA Properties中文unicode转码问题
- Latex: 解决 The gutter between columns is x inches wide (on page x), but should be at least 0.2 inches. 问题
参考: Sample_WCCI.tex Latex: 解决 The gutter between columns is x inches wide (on page x), but should be ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(01字典树求最大异或值)
http://codeforces.com/contest/706/problem/D 题意:有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给 ...
- C语言 深入学习
浮点数: x = Mx*2^Ex为一个规格化浮点数,Mx为x的尾数,Ex为x的阶码. 1e-6:表示1 * 10 ^ (-6). 编译时执行: sizeof是运算符(而非函数),在编译时执行,不会导致 ...
- CIKM 18 | 蚂蚁金服论文:基于异构图神经网络的恶意账户识别方法
小蚂蚁说: ACM CIKM 2018 全称是 The 27th ACM International Conference on Information and Knowledge Managemen ...