#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = (int)2e5 + 5;
struct point{
ll x, y;
point(){ x = y = 0; }
point(ll _x, ll _y) : x(_x), y(_y) {}
bool operator <(const point & other){ return make_pair(x, y) < make_pair(other.x, other.y); }
point operator +(const point &other){ return {x + other.x, y + other.y}; }
point operator -(const point &other){ return {x - other.x, y - other.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
for(auto q:*this)
assert(q < p);
while(size() > 1 and area2(at(size()-2), at(size()-1), p) >= 0)
pop_back();
push_back(p);
}
ll max_dot(point p){
if(empty())
return LLONG_MIN;
int l = 0, r = (int)size() - 1;
while(l < r){
int mid = (l + r)>>1;
assert(mid+1 < (int)size());
assert(mid >= 0);
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){
sz[u] = 1;
for(auto [v, w]:G[u]){
sumW[v] = sumW[u] + w;
dfs(v);
sz[u] += sz[v];
}
}
vector<hull> lights;
hull heavy;
void dfsHLD(int u){
if(u != 1){
dp[u] = LLONG_MIN;
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));
/*
dp[v] = LLONG_MAX;
for(int p:ancestors)
dp[v] = min(dp[v], dot({sumW[p], dp[p]}, {-speed[v], 1});
dp[v] += sumW[v] * speed[v];
dp[v] += start[v];
*/
int bigChild = -1;
for(auto [v, w]:G[u])
if(bigChild == -1 or sz[bigChild] < sz[v])
bigChild = v;
lights.push_back(heavy);
for(auto [v, w]:G[u])
if(v != bigChild){
heavy.clear();
dfsHLD(v);
}
heavy = lights.back();
lights.pop_back();
if(bigChild != -1)
dfsHLD(bigChild);
}
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});
}
for(int i = 2; i <= n; i++)
cin >> start[i] >> speed[i];
dfs(1);
dfsHLD(1);
for(int i = 2; i <= n; i++)
cout << dp[i] << ' ';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Incorrect |
4 ms |
5068 KB |
Output isn't correct |
3 |
Incorrect |
32 ms |
6736 KB |
Output isn't correct |
4 |
Incorrect |
47 ms |
7708 KB |
Output isn't correct |
5 |
Incorrect |
57 ms |
8584 KB |
Output isn't correct |
6 |
Incorrect |
71 ms |
9432 KB |
Output isn't correct |
7 |
Incorrect |
50 ms |
8132 KB |
Output isn't correct |
8 |
Incorrect |
68 ms |
9452 KB |
Output isn't correct |
9 |
Incorrect |
73 ms |
9368 KB |
Output isn't correct |
10 |
Incorrect |
67 ms |
9408 KB |
Output isn't correct |