#include
#include
#include
#include
#include
#include <unordered_map>
#include <unordered_set>
using namespace std;
vector<vector> adj;
vector<vector> cost;
vector visited;
vector dist;
int dfs(int x, int curcost){
visited[x] = true;
int ans = 0;
for(int y : adj[x]){
if(!visited[y])
ans = max(ans, dfs(y, curcost + cost[x][y]) + cost[x][y]);
}
dist[x] = max(ans, curcost);
return ans;
}
void solve(){
int n; cin >> n;
adj.resize(n + 1, vector<int>());
cost.resize(n + 1, vector<int>(n + 1, 0));
for(int i = 1; i < n; ++i){
int x, y, wt;
cin >> x >> y >> wt;
adj[x].push_back(y);
adj[y].push_back(x);
cost[y][x]=wt;
cost[x][y]=wt;
}
visited.resize(n + 1, false);
dist.resize(n + 1, 0);
for(int i = 1; i <= n; ++i){
if(!visited[i])
dfs(i, 0);
cout << dist[i] << " ";
}
}
int main(){
int t; cin >> t;
while(t–){
solve();
cout<<"\n";
}
return 0;
}[quote="t_r_y_736, post:1, topic:40137, full:true"]
#include
#include
#include
#include
#include
#include <unordered_map>
#include <unordered_set>
using namespace std;
vector<vector> adj;
vector<vector> cost;
vector visited;
vector dist;
int dfs(int x, int curcost){
visited[x] = true;
int ans = 0;
for(int y : adj[x]){
if(!visited[y])
ans = max(ans, dfs(y, curcost + cost[x][y]) + cost[x][y]);
}
dist[x] = max(ans, curcost);
return ans;
}
void solve(){
int n; cin >> n;
adj.resize(n + 1, vector<int>());
cost.resize(n + 1, vector<int>(n + 1, 0));
for(int i = 1; i < n; ++i){
int x, y, wt;
cin >> x >> y >> wt;
adj[x].push_back(y);
adj[y].push_back(x);
cost[y][x]=wt;
cost[x][y]=wt;
}
visited.resize(n + 1, false);
dist.resize(n + 1, 0);
for(int i = 1; i <= n; ++i){
if(!visited[i])
dfs(i, 0);
cout << dist[i] << " ";
}
}
int main(){
int t; cin >> t;
while(t–){
solve();
cout<<"\n";
}
return 0;
}