题解
深搜带上 \(sum_a\) ,然后把经过的 \(sum_b\) 放入栈里, 二分查找
code
#define ll long long
#include<bits/stdc++.h>
using namespace std;
inline void read(ll &x) {
x = 0;
ll flag = 1;
char c = getchar();
while(c < '0' || c > '9'){
if(c == '-')flag = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
x *= flag;
}
inline void write(ll x)
{
if(x < 0){
putchar('-');
x = -x;
}
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
struct node
{
ll to,blue,red;
};
vector<node> G[200005];
vector<ll> st;
ll ans[200006]={0};
void dfs(ll now,ll sum)
{
ans[now]=upper_bound(st.begin(),st.end(),sum)-st.begin();//找到最后一个小于等于sum的值
for(auto next:G[now])
{
ll to=next.to,a=next.blue,b=next.red;
if(st.size())st.push_back(st.back()+b);
else st.push_back(b);
dfs(to,sum+a);
st.pop_back();
}
}
int main()
{
ll t;
read(t);
while(t--)
{
ll n;
read(n);
for(ll i=2;i<=n;i++)
{
ll x,y,w;
read(x); read(y); read(w);
G[x].push_back({i,y,w});
}
dfs(1,0);
G[1].clear();
for(ll i=2;i<=n;i++)
{
write(ans[i]);
putchar(' ');
G[i].clear();
}
puts("");
}
return 0;
}
标签:read,ll,back,next,Prefixes,Path,st,sum
From: https://www.cnblogs.com/pure4knowledge/p/18073042