#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define vi vector<int>
#define ve vector
#define ll long long
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define onbit __builtin_popcount
#define ii pair<int,int>
#define vvi vector<vi>
#define vii vector<ii>
#define gii greater<ii>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF 1e18
#define eps 1e-7
#define eps1 1e-2
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MAX_A 1e5+5
using namespace std;
using namespace __gnu_pbds;
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll MOD = 1e9+9;
const int nax = 2e5+5;
const int MAX_VAL = 1e6;
double PI=3.14159265359;
int arx[8]={1,1,0,-1,-1,-1, 0, 1};
int ary[8]={0,1,1, 1, 0,-1,-1,-1};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
int tin[nax];
int tout[nax];
int timer=-1;
vii adj[nax];
ll res=0;
ll tab[nax];
ll segtree[nax*4];
ll lazy[nax*4];
int n,k;
void euler(int x,int p)
{
timer++;
tin[x]=timer;
tab[x]=res;
for(auto u:adj[x])
{
if (u.fi==p) continue;
res+=u.se;
euler(u.fi,x);
res-=u.se;
}
tout[x]=timer;
}
void build(int pos,int l,int r)
{
if (l>r) return;
if (l==r)
{
segtree[pos]=tab[r];
lazy[pos]=0;
return;
}
int mid=(r+l)/2;
build(pos*2+1,l,mid);
build(pos*2+2,mid+1,r);
segtree[pos]=max(segtree[pos*2+1],segtree[pos*2+2]);
}
void push(int pos)
{
lazy[pos*2+1]+=lazy[pos];
segtree[pos*2+1]+=lazy[pos];
lazy[pos*2+2]+=lazy[pos];
segtree[pos*2+2]+=lazy[pos];
lazy[pos]=0;
}
void update(int pos,int l,int r,int left,int right,int value)
{
if (left>right) return;
if (l==left&&r==right)
{
segtree[pos]+=value;
lazy[pos]+=value;
return;
}
push(pos);
int mid=(r+l)/2;
update(pos*2+1,l,mid, left , min(mid,right) ,value);
update(pos*2+2,mid+1,r, max(mid+1,left) , right ,value);
segtree[pos]=max(segtree[pos*2+1],segtree[pos*2+2]);
}
ll sum[nax];
void dfs(int x,int p)
{
sum[x]=segtree[0];
for(auto u:adj[x])
{
if (u.fi==p) continue;
//cout <<u.fi<<endl;
update(0,0,n-1,0,n-1,u.se);
update(0,0,n-1,tin[u.fi],tout[u.fi],-2*u.se);
dfs(u.fi,x);
update(0,0,n-1,0,n-1,-u.se);
update(0,0,n-1,tin[u.fi],tout[u.fi],2*u.se);
}
}
int main(){
optimise;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
//setIO("redistricting");
cin>>n>>k;
for (int i = 0; i < n-1; ++i)
{
ll x,y,z;
cin>>x>>y>>z;
x--;y--;
adj[x].pb({y,z});
adj[y].pb({x,z});
}
euler(0,-1);
build(0,0,n-1);
dfs(0,-1);
for (int i = 0; i < n; ++i)
{
cout << sum[i]<<endl;
}
}
Compilation message
Main.cpp: In function 'void setIO(std::string)':
Main.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:34:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:114:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
114 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:115:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |