#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pbds tree<pair<int, int>, null_type, less<pair<int, int>>,rb_tree_tag, tree_order_statistics_node_update>
using namespace __gnu_pbds;*/
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (PGCD)
ll lcm(ll a , ll b) {return (a * b) / gcd(a , b);} // least common multiple (PPCM)
int dx[8] = {1, 0, 0, -1, 1, 1, -1, -1};
int dy[8] = {0, 1, -1, 0, 1, -1, -1, 1};
#define endl "\n"
#define ss second
#define ff first
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define vi vector<int>
#define vii vector<pair<int,int>>
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define vdd vector<pdd>
#define speed ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
////////////////////Only Clear Code//////////////////////////
void usaco_problem(){
freopen("hopscotch.in", "r", stdin);
freopen("hopscotch.out", "w", stdout);
}
void init(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
}
const int mx = 2e5+5;
const int LOG = 25;
const int inf = 1e9+5;
const ll mod = 1e9+7;
vii tree[mx];
bool vis[mx];
ll dis1[mx], dis2[mx];
int bfs1(int x){
memset(vis, 0, sizeof vis);
priority_queue <pll, vector<pll>, greater<pll>> q;
q.push({0, x});
int ans = 0;
while(!q.empty()){
ll node = q.top().ss, dis = q.top().ff;
q.pop();
ans = node;
if(vis[node])continue;
vis[node] = 1;
for(pii adj : tree[node]){
if(!vis[adj.ff]){
q.push({dis + adj.ss, adj.ff});
}
}
}
return ans;
}
void bfs(int x){
memset(vis, 0, sizeof vis);
queue<int> q;
dis1[x] = 0;
q.push(x);
int ans = 0;
while(!q.empty()){
ll node = q.front();
q.pop();
ans = node;
if(vis[node])continue;
vis[node] = 1;
for(pii adj : tree[node]){
if(!vis[adj.ff]){
dis1[adj.ff] = dis1[node] + adj.ss;
q.push(adj.ff);
}
}
}
}
void bfs2(int x){
memset(vis, 0, sizeof vis);
queue<int> q;
dis2[x] = 0;
q.push(x);
int ans = 0;
while(!q.empty()){
ll node = q.front();
q.pop();
ans = node;
if(vis[node])continue;
vis[node] = 1;
for(pii adj : tree[node]){
if(!vis[adj.ff]){
dis2[adj.ff] = dis2[node] + adj.ss;
q.push(adj.ff);
}
}
}
}
void run_case(){
int n, k;cin >> n >> k;
for(int i = 0; i < n-1;i++){
int x, y, w;cin >> x >> y >> w;
tree[x].pb({y, w});
tree[y].pb({x, w});
}
int a = bfs1(1);
int b = bfs1(a);
//cout << a << " " << b << endl;
bfs(a);
bfs2(b);
for(int i = 1; i <= n;i++){
cout << max(dis1[i], dis2[i]) << endl;
}
}
int main(){
speed;
int t;
//cin >> t;
t = 1;
while(t--){
run_case();
}
}
/*
NEVER GIVE UP!
DOING SMTHNG IS BETTER THAN DOING NTHNG!!!
Your Guide when stuck:
- Continue keyword only after reading the whole input
- Don't use memset with testcases
- Check for corner cases(n=1, n=0)
- Check where you declare n(Be careful of declaring it globally and in main)
*/
Compilation message
Main.cpp: In function 'void bfs(int)':
Main.cpp:81:9: warning: variable 'ans' set but not used [-Wunused-but-set-variable]
81 | int ans = 0;
| ^~~
Main.cpp: In function 'void bfs2(int)':
Main.cpp:102:9: warning: variable 'ans' set but not used [-Wunused-but-set-variable]
102 | int ans = 0;
| ^~~
Main.cpp: In function 'void usaco_problem()':
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("hopscotch.in", "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("hopscotch.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'void init()':
Main.cpp:40:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:42:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
5204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
5204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
5204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
5204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
11740 KB |
Output is correct |
2 |
Correct |
61 ms |
13980 KB |
Output is correct |
3 |
Correct |
78 ms |
13272 KB |
Output is correct |
4 |
Correct |
79 ms |
13792 KB |
Output is correct |
5 |
Correct |
59 ms |
14004 KB |
Output is correct |
6 |
Correct |
91 ms |
13824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
5204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |