#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define ep insert
#define endl '\n'
#define elif else if
#define pow pwr
#define sqrt sqrtt
#define int long long
#define ll long long
#define y1 YONE
typedef unsigned long long ull;
using namespace std;
const int N=1e5+5;
int n,k,tin[N],sz[N],par[N],t,v[N],tree[N],lx,rx,ans[N],seg[4*N+5],lazy[N],val,dis[N];
vector<pair<int,int>> adj[N];
bool vis[N];
void dfs(int x,int p){
par[x]=p;
sz[x]=1;
t++;
tin[x]=t;
tree[t]=x;
for (auto u:adj[x]){
if (p==u.F) continue;
v[u.F]=u.S;
dis[u.F]=dis[x]+u.S;
dfs(u.F,x);
sz[x]+=sz[u.F];
}return;
}
void build(int x,int l,int r){
if (l==r){
seg[x]=dis[tree[l]];
return;
}int mid=(l+r)/2;
build((x<<1),l,mid);
build((x<<1)+1,mid+1,r);
seg[x]=max(seg[(x<<1)],seg[(x<<1)+1]);
return;
}
void push(int x){
seg[(x<<1)]+=lazy[x];
seg[(x<<1)+1]+=lazy[x];
lazy[(x<<1)]+=lazy[x];
lazy[(x<<1)+1]+=lazy[x];
lazy[x]=0;
return;
}
void edit(int x,int l,int r){
if (l>rx || r<lx) return;
if (l>=lx && r<=rx){
seg[x]+=val;
lazy[x]+=val;
return;
}int mid=(l+r)/2;
push(x);
edit((x<<1),l,mid);
edit((x<<1)+1,mid+1,r);
seg[x]=max(seg[(x<<1)],seg[(x<<1)+1]);
return;
}
void getans(int x,int p){
lx=1,rx=n,val=v[x];
edit(1,1,n);
lx=tin[x],rx=tin[x]+sz[x]-1,val=-2*v[x];
edit(1,1,n);
ans[x]=seg[1];
for (auto u:adj[x]){
if (u.F==p) continue;
getans(u.F,x);
}
lx=1,rx=n,val=-v[x];
edit(1,1,n);
lx=tin[x],rx=tin[x]+sz[x]-1,val=+2*v[x];
edit(1,1,n);
return;
}
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin>>n>>k;
for (int i=1;i<n;i++){
int x,y,w;
cin>>x>>y>>w;
adj[x].pb({y,w});
adj[y].pb({x,w});
}
dfs(1,0);
build(1,1,n);
getans(1,0);
for (int i=1;i<=n;i++) cout<<ans[i]<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
8536 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
8536 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
8536 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
8536 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
76 ms |
20560 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
8536 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |