- #include <bits/stdc++.h>
- using namespace std;
- #define N 30010
- #define dbg printf("comes here\n");
- vector<int> adj[N];
- vector<int> weight[N];
- int n;
- bool visited[N];
- int dis[N];
- int node1, node2, di;
- void show(){
- for(int i = 0; i <n; i++){
- for(int j = 0; j <= adj[i].size(); j++) cout<<adj[i][j]<<" ";
- cout<<endl;
- for(int j = 0; j <= weight[i].size(); j++) cout<<weight[i][j]<<" ";
- cout<<endl;
- }
- }
- void clr(){
- node1 = node2 = -1;
- di = 0;
- for(int i = 0; i <= n; i++){
- adj[i].clear();
- weight[i].clear();
- visited[i] = false;
- dis[i] = INT_MAX;
- }
- }
- void clrsome(){
- di = 0;
- for(int i = 0; i <= n; i++) {dis[i] = INT_MAX; visited[i] = false;}
- }
- void bfs(int source){
- list<int>q;
- q.push_back(source);
- visited[source]= true;
- dis[source] = 0;
- while(!q.empty()){
- int from = q.front();
- visited[from] = true;
- q.pop_front();
- for(int i = 0; i <adj[from].size(); i++){
- int to = adj[from][i];
- if(visited[to]) continue;
- q.push_back(to);
- dis[to] = min(dis[to],dis[from] + weight[from][i]);
- if(dis[to]>= di) {node1 = to; di = dis[to];}
- }
- }
- }
- int main(){
- int ts;
- scanf("%d", &ts);
- for(int c = 1; c <= ts; c++){
- scanf("%d", &n);
- clr();
- for(int i = 0; i <n-1; i++){
- int a, b, w;
- scanf("%d%d%d", &a, &b, &w);
- adj[a].push_back(b);
- adj[b].push_back(a);
- weight[a].push_back(w);
- weight[b].push_back(w);
- }
- //show();
- bfs(0);
- int first = node1;
- //cout<<"here to";
- //for(int i = 0; i <n; i++) cout<<dis[i]<<" ";
- clrsome();
- bfs(first);
- printf("Case %d: %d\n", c, di);
- //for(int i = 0; i <n; i++) cout<<dis[i]<<" ";
- //cout<<"distance = "<<di;
- }
- return 0;
- }
Friday 28 July 2017
Lightoj 1094 - Farthest Nodes in a Tree
Subscribe to:
Post Comments (Atom)
Most Featured Post
Lightoj 1159 - Batman
http://lightoj.com/volume_showproblem.php?problem=1159 problem analysis: First i thought of this as if s1, s2 and s3 are those three str...
-
Problem link: Problem Analysis: It is actually a basic Bisection problem , as we can see here we can not actually find a formula fo...
-
http://lightoj.com/volume_showproblem.php?problem=1382 Problem analysis: This is a rare problem i wrote about so far. After much strugg...
No comments:
Post a Comment