题目描述:

luogu

题解:

二分+暴力$vector$+$dfs$。

记录下所有可能的子树内合法方案,双指针+归并合并。

代码:

#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = ;
template<typename T>
inline void read(T&x)
{
T f = ,c = ;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){c=c*+ch-'';ch=getchar();}
x = f*c;
}
int n,fa[N],ch[N][];
ll v[N],dep[N];
void DFS(int u)
{
if(!ch[u][])return ;
dep[ch[u][]]=dep[u]+v[ch[u][]],DFS(ch[u][]);
dep[ch[u][]]=dep[u]+v[ch[u][]],DFS(ch[u][]);
}
struct Pair
{
ll x,y;
Pair(){}
Pair(ll x,ll y):x(x),y(y){}
}g1[N],g2[N],g[N];
vector<Pair>ve[N];
ll mid;
int Merge(int len)
{
int i = ,j = len,k = ;
while(i<=len&&j>=)
{
if(g1[i].x<=g2[j].x)g[++k]=g1[i++];
else g[++k]=g2[j--];
}
while(i<=len)g[++k]=g1[i++];
while(j>=)g[++k]=g2[j--];
return k;
}
void dfs(int u)
{
if(!ch[u][]){ve[u].push_back(Pair(dep[u],dep[u]));return ;}
int ls = ch[u][],rs = ch[u][];
dfs(ls),dfs(rs);int k=;
if(ve[ls].size()>ve[rs].size())swap(ls,rs);
for(int i=,j=-,l1=ve[ls].size(),l2=ve[rs].size();i<l1;i++)
{
while(j<l2-&&ve[ls][i].y+ve[rs][j+].x-2ll*dep[u]<=mid)j++;
if(~j)g1[++k]=Pair(ve[ls][i].x,ve[rs][j].y),g2[k]=Pair(ve[rs][j].y,ve[ls][i].x);
}
int len = Merge(k);
for(int i=;i<=len;i++)
if(i==||g[i].y<g[i-].y)
ve[u].push_back(g[i]);
}
bool check()
{
for(int i=;i<=n;i++)
ve[i].clear();
dfs();
return (int)ve[].size()>;
}
int main()
{
read(n);
for(int i=;i<=n;i++)
{
read(fa[i]),read(v[i]);
if(!ch[fa[i]][])ch[fa[i]][]=i;
else ch[fa[i]][]=i;
}
DFS();
ll l = ,r = 200000ll*n,ans = r;
while(l<=r)
{
mid = (l+r)>>;
if(check())ans=mid,r=mid-;
else l=mid+;
}
printf("%lld\n",ans);
return ;
}

AT2172 Shik and Travel的更多相关文章

  1. 【AtCoder Grand Contest 007E】Shik and Travel [Dfs][二分答案]

    Shik and Travel Time Limit: 50 Sec  Memory Limit: 512 MB Description 给定一棵n个点的树,保证一个点出度为2/0. 遍历一遍,要求每 ...

  2. AGC007E Shik and Travel 解题报告

    AGC007E Shik and Travel 题目大意:\(n\) 个点的二叉树,每个点要么两个儿子,要么没有儿子,每条边有边权. 你从 \(1\) 号节点出发,走到一个叶子节点.然后每一天,你可以 ...

  3. [AT2172] [agc007_e] Shik and Travel

    题目链接 AtCoder:https://agc007.contest.atcoder.jp/tasks/agc007_e 洛谷:https://www.luogu.org/problemnew/sh ...

  4. AtCoder Grand Contest 007 E:Shik and Travel

    题目传送门:https://agc007.contest.atcoder.jp/tasks/agc007_e 题目翻译 现在有一个二叉树,除了叶子每个结点都有两个儿子.这个二叉树一共有\(m\)个叶子 ...

  5. [AGC007E] Shik and Travel

    题目 给定一棵n节点的 以1为根的 满二叉树 (每个非叶子节点恰好有两个儿子)n−1 条边. 第ii条边连接 i+1号点 和 ai, 经过代价为vi设这棵树有m个叶子节点定义一次合法的旅行为:(1) ...

  6. AtCoder AGC007E Shik and Travel (二分、DP、启发式合并)

    题目链接 https://atcoder.jp/contests/agc007/tasks/agc007_e 题解 首先有个很朴素的想法是,二分答案\(mid\)后使用可行性DP, 设\(dp[u][ ...

  7. [atAGC007E]Shik and Travel

    二分枚举答案,判定答案是否合法 贪心:每一个叶子只能经过一遍,因此叶子的顺序一定是一个dfs序,即走完一棵子树中的所有叶子才会到子树外 根据这个贪心可以dp,设$f[k][l][r]$表示仅考虑$k$ ...

  8. 贪心/构造/DP 杂题选做Ⅲ

    颓!颓!颓!(bushi 前传: 贪心/构造/DP 杂题选做 贪心/构造/DP 杂题选做Ⅱ 51. CF758E Broken Tree 讲个笑话,这道题是 11.3 模拟赛的 T2,模拟赛里那道题的 ...

  9. AtCoder Grand Contest 007

    AtCoder Grand Contest 007 A - Shik and Stone 翻译 见洛谷 题解 傻逼玩意 #include<cstdio> int n,m,tot;char ...

随机推荐

  1. Cocoapods在OS X Yosemite上报错的解决方法

    今天升级了Mac OS X 10.10-Yosemite以后运行pod install遇到下面的错误: /System/Library/Frameworks/Ruby.framework/Versio ...

  2. 用EnumMap代替序数索引

    用EnumMap代替序数索引   有时候,会见到利用ordinal方法来索引数组的代码.例如下面这个简化的类,表示一种烹饪用的香草: public class Herb { public enum T ...

  3. 面向对象-mixin设计模式的应用(多继承应用场景)

    什么是设计模式? 设计模式只是一种开发思想.不是什么固定的格式. 前人的好的思想,我们后人拿过来用! mixin设计模式: 1.mixin设计迷失可以在不对类的内容的修改前提下,扩展类的功能(添加父类 ...

  4. springboot2.x 的 RedisCacheManager变化

    springboot2.x 的 RedisCacheManager变化 springboot2.x 的 RedisCacheManager变化 由于最近在学着使用redis做缓存,使用的是spring ...

  5. react native设置容器阴影

    shadowColor:'#eee',shadowOffset:{h:10,w:10},shadowRadius:3,shadowOpacity:0.8,

  6. DBUtils C3P0 阿里巴巴德鲁伊连接池工具的下载

  7. Guard Duty (hard) Codeforces - 958E3 || uva 1411

    https://codeforces.com/contest/958/problem/E3 当没有三点共线时,任意一个这样的点集都是保证可以找到答案的,(考虑任意一种有相交的连线方案,一定可以将其中两 ...

  8. css水平垂直居中块整理

    1.绝对定位+负margin 兼容性很好,但需要指定子块的高度和宽度,以及负margin .wp{ position: relative; width: 200px; height: 200px; b ...

  9. bootstrap-table 基础用法

    1.需要添加的引用. <script src="@Url.Content("~/js/jquery-2.1.1.js")"></script& ...

  10. Java中的变量——通过示例学习Java编程(4)

    作者:CHAITANYA SINGH 来源:https://www.koofun.com/pro/kfpostsdetail?kfpostsid=14&cid= 变量是用来存放可以更改的值的容 ...