Untitled
unknown
c_cpp
a year ago
1.4 kB
25
Indexable
//OMI_14
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define faster \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
using namespace std;
void DFS(vector<vector<pair<int,int>>>& a,int vertex,vector<bool>& vis,vector<ll>& height){
vis[vertex]=true;
for(auto child:a[vertex]){
if(!vis[child.first]){
DFS(a,child.first,vis,height);
height[vertex]=max(height[vertex],(ll)height[child.first]+child.second);
}
}
}
// unordered_map<ll, ll> mp;
// mp.reserve(1024);
// mp.max_load_factor(1);
int main() {
faster;
// freopen("input.txt","r",stdin);
// freopen("output1.txt","w",stdout);
int j=0;
int t;
cin>>t;
while (t--)
{
int n;
cin>>n;
vector<bool>vis(n+1,false);
vector<vector<pair<int,int>>>a(n+1);
vector<ll>h(n+1,0);
for (int i = 0; i < n-1; i++)
{
ll x,y,z;
cin>>x>>y>>z;
a[x].push_back({y,z});
a[y].push_back({x,z});
}
cout << "Case " << j + 1 << ": ";
j++;
DFS(a,0,vis,h);
sort(a.begin(),a.end());
reverse(a.begin(),a.end());
cout<<h[0]+h[1]<<"\n";
}
// fclose(stdin);
// fclose(stdout);
return 0;
}
Editor is loading...
Leave a Comment