#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = (int)2e5 + 5;
typedef pair<ll,ll> point;
#define x first
#define y second
point operator +(point a, point b){ return {a.x + b.x, a.y + b.y}; }
point operator -(point a, point b){ return {a.x - b.x, a.y - b.y}; }
ll dot(const point &a, const point &b){ return a.x * b.x + a.y * b.y; }
ll cross(const point &a, const point &b){ return a.x * b.y - a.y * b.x; }
ll area2(point a, point b, point c){ return cross(b - a, c - a); }
struct hull : vector<point>{
void add_point(point p){ // p is bigger than all the previous ones
while(size() > 1 and area2(at(size()-2), at(size()-1), p) >= 0)
pop_back();
push_back(p);
}
ll max_dot(point p){
assert(!empty());
int l = 0, r = (int)size() - 1;
while(l < r){
int mid = (l + r)>>1;
if(dot(at(mid), p) <= dot(at(mid + 1), p))
l = mid + 1;
else
r = mid;
}
assert(l < (int)size());
assert(l >= 0);
return dot(at(l), p);
}
};
vector<pair<int,ll>> G[maxn];
ll speed[maxn], start[maxn], dp[maxn], sumW[maxn];
int n, sz[maxn];
point get_point(int u){
return {sumW[u], -dp[u]};
}
void dfs(int u, int p){
sz[u] = 1;
for(auto [v, w]:G[u]){
if(v == p)
continue;
sumW[v] = sumW[u] + w;
dfs(v, u);
sz[u] += sz[v];
}
}
vector<hull> lights;
hull heavy;
void dfsHLD(int u, int p){
if(u != 1){
dp[u] = -1e18;
for(auto h:lights){
dp[u] = max(dp[u], h.max_dot({speed[u], 1}));
}
if(!heavy.empty())
dp[u] = max(dp[u], heavy.max_dot({speed[u], 1}));
dp[u] = sumW[u] * speed[u] + start[u] - dp[u];
}
heavy.push_back(get_point(u));
int bigChild = -1;
for(auto [v, w]:G[u]){
if(v == p) continue;
if(bigChild == -1 or sz[bigChild] < sz[v])
bigChild = v;
}
lights.push_back(heavy);
for(auto [v, w]:G[u])
if(v != bigChild and v != p){
heavy.clear();
dfsHLD(v, u);
}
heavy = lights.back();
lights.pop_back();
if(bigChild != -1)
dfsHLD(bigChild, u);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
for(int i = 1; i < n; i++){
int u, v, w;
cin >> u >> v >> w;
G[u].push_back({v, w});
G[v].push_back({u, w});
}
for(int i = 2; i <= n; i++)
cin >> start[i] >> speed[i];
dfs(1,-1);
dfsHLD(1,-1);
for(int i = 2; i <= n; i++)
cout << dp[i] << ' ';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Incorrect |
12 ms |
5580 KB |
Output isn't correct |
3 |
Execution timed out |
1084 ms |
13280 KB |
Time limit exceeded |
4 |
Execution timed out |
1088 ms |
16488 KB |
Time limit exceeded |
5 |
Execution timed out |
1086 ms |
19812 KB |
Time limit exceeded |
6 |
Execution timed out |
1078 ms |
22948 KB |
Time limit exceeded |
7 |
Execution timed out |
1093 ms |
14008 KB |
Time limit exceeded |
8 |
Execution timed out |
1098 ms |
18520 KB |
Time limit exceeded |
9 |
Execution timed out |
1075 ms |
19448 KB |
Time limit exceeded |
10 |
Execution timed out |
1091 ms |
19040 KB |
Time limit exceeded |