//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
struct line{
ll a,b;
ll operator()(const ll x){
return a*x+b;
}
};
const int mxn=1e5+5;
vector<pair<int,int>> adj[mxn];
ll S[mxn];
ll V[mxn];
line li[mxn];
ll depth[mxn];
ll dp[mxn];
void dfs0(int v,int p){
for(auto u:adj[v]){
if(u.f==p) continue;
depth[u.f]=depth[v]+u.s;
dfs0(u.f,v);
}
}
bool check(int a,int b,int c){
line l1=li[a];
line l2=li[b];
line l3=li[c];
return (long double)(l2.b-l3.b)/(l3.a-l2.a)<(long double)(l1.b-l2.b)/(l2.a-l1.a);
}
void dfs(int v,int p,deque<int> vec){
int l=0,r=vec.size()-1;
while(l<r){
int mid=(l+r)/2;
if(li[vec[mid]](-V[v])<=li[vec[mid+1]](-V[v])){
r=mid;
}
else{
l=mid+1;
}
}
dp[v]=S[v]+depth[v]*V[v]+li[vec[l]](-V[v]);
li[v]={depth[v],dp[v]};
while(vec.size()>1 and check(v,vec[0],vec[1])){
vec.pop_front();
}
vec.push_front(v);
for(auto u:adj[v]){
if(u.f==p) continue;
dfs(u.f,v,vec);
}
deque<int>().swap(vec);
}
int main() {_
int n;
cin>>n;
for(int i=0;i<n-1;i++){
int d,u,v;
cin>>u>>v>>d;
adj[u].push_back({v,d});
adj[v].push_back({u,d});
}
for(int i=2;i<=n;i++){
cin>>S[i]>>V[i];
}
dfs0(1,0);
deque<int> tmp;
tmp.push_back(0);
dfs(1,0,tmp);
for(int i=2;i<=n;i++){
cout<<dp[i]<<' ';
}
return 0;
}
//maybe its multiset not set
Compilation message
harbingers.cpp: In function 'void setIO(std::string)':
harbingers.cpp:12:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
harbingers.cpp:13:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
5 ms |
5844 KB |
Output is correct |
3 |
Runtime error |
62 ms |
65536 KB |
Execution killed with signal 9 |
4 |
Runtime error |
71 ms |
65536 KB |
Execution killed with signal 9 |
5 |
Runtime error |
103 ms |
65536 KB |
Execution killed with signal 9 |
6 |
Runtime error |
109 ms |
65536 KB |
Execution killed with signal 9 |
7 |
Runtime error |
95 ms |
41280 KB |
Memory limit exceeded |
8 |
Runtime error |
99 ms |
65536 KB |
Execution killed with signal 9 |
9 |
Runtime error |
87 ms |
65536 KB |
Execution killed with signal 9 |
10 |
Runtime error |
78 ms |
65536 KB |
Execution killed with signal 9 |