-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathAgitated Chandan
100 lines (70 loc) · 2.09 KB
/
Agitated Chandan
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
// Sample code to perform I/O:
cin >> name; // Reading input from STDIN
cout << "Hi, " << name << ".\n"; // Writing output to STDOUT
// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
*/
// Write your code here
#include<bits/stdc++.h>
using namespace std;
map<int, vector< pair<int,int> > >mp;
pair<int,int> bfs(int root,int n)
{
map<int,bool>visited; //visited[root]=true;
for(int i=1;i<=n;i++)
visited[i]=false;
visited[root]=true;
queue<int>q; q.push(root);
map<int,int>distance;
for(int i=1;i<=n;i++)
distance[i]=0;
int maxi=INT_MIN,maxi_node=root; distance[root]=0;
while(!q.empty())
{
int node=q.front();
q.pop();
for(auto x:mp[node])
{
if(!visited[x.first])
{
visited[x.first]=true;
distance[x.first]=distance[node]+x.second;
if(distance[x.first]>maxi)
{
maxi=distance[x.first];
maxi_node=x.first;
}
q.push(x.first);
}
}
}
return make_pair(maxi_node,maxi);
}
int main()
{
int t; cin>>t;
while(t--)
{
int n; cin>>n;
for(int i=1;i<n;i++)
{
int a,b,c; cin>>a>>b>>c;
mp[a].push_back(make_pair(b,c));
mp[b].push_back(make_pair(a,c));
}
pair<int,int>p=bfs(1,n);
pair<int,int>ans=bfs(p.first,n);
int max_distance=ans.second;
int cost=0;
if(max_distance>10000)
cost=10000;
else if(max_distance>1000)
cost=1000;
else if(max_distance>100)
cost=100;
else
cost=0;
cout<<cost<<" "<<max_distance<<endl;
mp.clear();
}
}