[NOIP 2005] 运输计划
这是一道假的图论
思维难度很低,代码量偏高
就是一道板子+二分
树上差分就AC了
注意卡常即可
二分枚举答案x,为时间长度
将每一个长度大于x的计划链长记录下来(有几个,总需要减少多少长度)
在树上跑一跑即可
树上差分就将u,vv权值+1,lca(u,v)-2即可
dp[i]:为子树权值和,也是i上面道路有几条路径覆盖
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int read()
{
int f=,ans=;char c;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){ans=ans*+c-'';c=getchar();}
return f*ans;
}
int n,m;
struct node{
int u,v,w,nex;
}x[];
int deep[],vt[],dis[],fa[][],head[],cnt=;
void add(int u,int v,int w)
{
x[cnt].u=u,x[cnt].v=v,x[cnt].w=w,x[cnt].nex=head[u],head[u]=cnt++;
}
void dfs(int f,int fath,int DIS)
{
deep[f]=deep[fath]+;
dis[f]=DIS;
fa[f][]=fath;
for(int i=;(<<i)<=deep[f];i++) fa[f][i]=fa[fa[f][i-]][i-];
for(int i=head[f];i;i=x[i].nex)
{
if(x[i].v==fath) continue;
vt[x[i].v]=x[i].w;
dfs(x[i].v,f,DIS+x[i].w);
}
return;
}
int lca(int u,int v)
{
if(deep[u]<deep[v]) swap(u,v);
for(int i=;i>=;i--)
if(deep[u]-(<<i)>=deep[v]) u=fa[u][i];
if(u==v) return u;
for(int i=;i>=;i--)
{
if(fa[u][i]==fa[v][i]) continue;
u=fa[u][i],v=fa[v][i];
}return fa[u][];
}
struct node1{
int x,y,lca,dis;
}s[];
int l,r,mid,dp[];
void dp_tree(int f,int fath)
{
for(int i=head[f];i;i=x[i].nex)
{
if(x[i].v==fath) continue;
dp_tree(x[i].v,f);
dp[f]+=dp[x[i].v];
}
return;
}
bool check(int st)
{
memset(dp,,sizeof(dp));
int tot=,maxn=;
for(int i=;i<=m;i++)
{
if(s[i].dis>st)
{
tot++;
dp[s[i].x]++,dp[s[i].y]++,dp[s[i].lca]-=;
maxn=max(maxn,s[i].dis-st);
}
}
dp_tree(,);
for(int i=;i<=n;i++)
if(dp[i]==tot&&vt[i]>=maxn) return ;
return ;
}
int data=<<-;
int main()
{
n=read(),m=read();
for(int i=;i<n;i++)
{
int u=read(),v=read(),w=read();
add(u,v,w),add(v,u,w);
}
dfs(,,);
for(int i=;i<=m;i++)
{
s[i].x=read(),s[i].y=read();
s[i].lca=lca(s[i].x,s[i].y);
s[i].dis=dis[s[i].x]+dis[s[i].y]-(dis[s[i].lca]<<);
r=max(r,s[i].dis);
}
while(l<=r)
{
mid=l+r>>;
if(check(mid)) data=min(data,mid),r=mid-;
else l=mid+;
}
printf("%d",data);
}
[NOIP 2005] 运输计划的更多相关文章
- 4632 NOIP[2015] 运输计划
4632 NOIP[2015] 运输计划 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题解 题目描述 Description 公元 2044 ...
- [NOIP 2015]运输计划-[树上差分+二分答案]-解题报告
[NOIP 2015]运输计划 题面: A[NOIP2015 Day2]运输计划 时间限制 : 20000 MS 空间限制 : 262144 KB 问题描述 公元 2044 年,人类进入了宇宙纪元. ...
- Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分)
Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分) Description L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之 ...
- cogs 2109. [NOIP 2015] 运输计划 提高组Day2T3 树链剖分求LCA 二分答案 差分
2109. [NOIP 2015] 运输计划 ★★★☆ 输入文件:transport.in 输出文件:transport.out 简单对比时间限制:3 s 内存限制:256 MB [题 ...
- NOIP[2015] 运输计划
传送门 题目描述 Description 公元 2044 年,人类进入了宇宙纪元.L 国有 n 个星球,还有 n−1 条双向航道,每条航道建立在两个星球之间,这 n−1 条航道连通了 L 国的所有星球 ...
- [noip 2015]运输计划 [LCA][树链剖分]
用了luogu上的题目描述 题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道连通了 L 国的 ...
- NOIP 2015运输计划
题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道连通了 L 国的所有星球. 小 P 掌管一家 ...
- NOIP[2015] 运输计划(codevs 4632)
题目描述 Description 公元 2044 年,人类进入了宇宙纪元.L 国有 n 个星球,还有 n−1 条双向航道,每条航道建立在两个星球之间,这 n−1 条航道连通了 L 国的所有星球.小 P ...
- noip 2015 运输计划 (lca+二分)
/* 95 最后一个点T了 qian lv ji qiong 了 没学过树剖 听chx听xzc说的神奇的方法 Orz 首先求出每个计划的路径长度 这里写的倍增 然后二分答案 对于每个ans 统计> ...
随机推荐
- Uncaught Error: code length overflow. (1604>1056)
解决方法来源~~~https://blog.csdn.net/arrowzz/article/details/80656510 二维码生成时,如果长度太长会有异常: Uncaught Error: c ...
- 【WXS数据类型】Array
属性: 名称 值类型 说明 [Array].constructor [String] 返回值为“Array”,表示类型的结构字符串 [Array].length [Number] 返回数组长度 方法: ...
- Angular6项目搭建
参照 草根专栏- ASP.NET Core + Ng6 实战:https://v.qq.com/x/page/b076702elvw.html 安装工具: Nodejs, npm 最新版, h ...
- Android开发-API指南-<activity>
<activity> 英文原文:http://developer.android.com/guide/topics/manifest/activity-element.html 采集(更新 ...
- [leetcode-738-Monotone Increasing Digits]
Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...
- nginx 添加的配置信息
使用logrotate管理Nginx日志配置如下: [root@vm-10-129-93-51 nginx]# vi /etc/logrotate.d/nginx /letv/log/nginx/*. ...
- Automatic Judge
Description Welcome to HDU to take part in the second CCPC girls’ competition! A new automatic judge ...
- Delegate(QLabel和QComboBox)
一.最终效果 二.实现思路 1.createEditor()中create两个控件,分别是QLabel和QComboBox,将其添加到一个widget中,然后返回该widget: 2.setEdito ...
- lintcode-196-寻找缺失的数
196-寻找缺失的数 给出一个包含 0 .. N 中 N 个数的序列,找出0 .. N 中没有出现在序列中的那个数. 样例 N = 4 且序列为 [0, 1, 3] 时,缺失的数为2. 挑战 在数组上 ...
- lintcode-161-旋转图像
161-旋转图像 给定一个N×N的二维矩阵表示图像,90度顺时针旋转图像. 样例 给出一个矩形[[1,2],[3,4]],90度顺时针旋转后,返回[[3,1],[4,2]] 挑战 能否在原地完成? 标 ...