Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const int maxx = 3e5+10;
- vector<int>g[maxx],cost[maxx];
- int d[maxx],edges;
- bool vis[maxx];
- void bfs(int root)
- {
- for(int i=0; i<=edges; i++)
- {
- vis[i]=false;
- }
- queue<int>q;
- q.push(root);
- d[root]=0;
- vis[root]=true;
- while(!q.empty())
- {
- int u = q.front();
- q.pop();
- for(int i=0; i<g[u].size(); i++)
- {
- int v = g[u][i];
- if(!vis[v])
- {
- d[v] = d[u]+cost[u][i];
- q.push(v);
- vis[v]=true;
- }
- }
- }
- }
- void clr()
- {
- for(int i=0; i<=edges; i++)
- {
- g[i].clear();
- cost[i].clear();
- }
- }
- int main()
- {
- int t,cas=1;
- cin>>t;
- while(t--)
- {
- cin>>edges;
- clr();
- for(int i=0; i<=edges; i++)
- {
- g[i].clear();
- cost[i].clear();
- }
- for(int i=1; i<edges; i++)
- {
- int u,v,w;
- cin>>u>>v>>w;
- g[u].push_back(v);
- g[v].push_back(u);
- cost[u].push_back(w);
- cost[v].push_back(w);
- }
- bfs(1);
- int m =0,sn=0;
- for(int i=0; i<edges; i++)
- {
- if(m<d[i])
- {
- sn = i;
- m = d[i];
- vis[i] = false;
- }
- }
- bfs(sn);
- m=0;
- for(int i=0; i<edges; i++)
- {
- if(m<d[i])
- {
- m = d[i];
- }
- }
- cout<<m<<endl;
- }
- }
Add Comment
Please, Sign In to add comment