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 ...
随机推荐
- vim使用跳转列表 jumps 来跟踪 (历史位置的)导航
参考: Vim使用跳转列表来跟踪你的导航,你可以通过这个列表来向前或者向后导航. 跳转列表保留所有地方的轨迹,它可以跟踪文件名.行号和列号. 查看调整列表::jumps 导航键 描述 CTRL-o 跳 ...
- 【做题】arc068_f-Solitaire——糊结论
把所有数字放入双端队列后,结果大概是这样一个排列: \[P_1 1 P_2\] 其中\(P_1\)是递减序列,\(P_2\)是递增序列. 我们以\(1\)所在的位置\(k\)分割最终的排列\(A\). ...
- 【做题】Codeforces Round #436 (Div. 2) F. Cities Excursions——图论+dfs
题意:给你一个有向图,多次询问从一个点到另一个点字典序最小的路径上第k个点. 考虑枚举每一个点作为汇点(记为i),计算出其他所有点到i的字典序最小的路径.(当然,枚举源点也是可行的) 首先,我们建一张 ...
- oracle单行函数 之 字符函数
Upper(字符串 / 列):将输入的字符串变成大写 Lower(字符串 / 列):将输入的字符串变成小写 Initcap(字符串 / 列):开头首字母大写 Length(字符串 / 列):字符串长度 ...
- (zhuan) Prioritized Experience Replay
Prioritized Experience Replay JAN 26, 2016 Schaul, Quan, Antonoglou, Silver, 2016 This Blog from: ht ...
- [bootstrapValidator] - bootstrap的验证工具
翻了下之前和同事做的一个验证 <!--bootstrapValidator--> <script type="text/javascript" th:inline ...
- 日系插画学习笔记(一):SAI软件基础
检测驱动是否安装正确:1.画笔没有压感,线条没有粗细变化2.画笔线条有锯齿 一.文件:新建文件:预设尺寸:一般选择A3(8k),A4(16k),A5(32k)作业要求:A4A5 - 300dpi,像素 ...
- CentOS7使用httpd apache 和firewalld打开关闭防火墙与端口
Centos7 使用systemctl 工具操作命令 systemctl 是Centos7的服务管理工具中的主要工具 ,它融合之前service和chkconfig的功能于一体 一.httpd的设置 ...
- Luncene学习 第一天 《入门程序》
整个luncene 流程 下面贴出代码 package com.zuoyan.lucene.demo; import java.io.File; import org.apache.commons.i ...
- js try catch 的使用,容错处理
tag: js try catch 的使用,js容错处理 网友回答: try{ //正常执行 }catch(e/*你感觉会出错的 错误类型*/){ // 可能出现的意外 eg:用户自己操作失误 或者 ...